码迷,mamicode.com
首页 > 其他好文 > 详细

动画原理——曲线

时间:2015-01-26 18:45:40      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

书籍名称:HTML5-Animation-with-JavaScript

书籍源码:https://github.com/lamberta/html5-animation


1.使用quadraticCurveTo,

  • 开始点:moveTo(20,20)
  • 控制点:quadraticCurveTo(20,100,200,20)的前两个参数,
  • 结束点:quadraticCurveTo(20,100,200,20)的后两个参数

图示如下:

技术分享

2.曲线经过的点

如果你想让曲线经过一个点,可以利用下面这个公式来计算。其中xt,yt是我们希望曲线经过的目标点,而x0,y0,x2,y2分别为两个端点。通过这个公式可以计算出控制点x1,y1

x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;

03-curve-through-point.html代码:

技术分享
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Curve Through Point</title>
    <link rel="stylesheet" href="../include/style.css">
  </head>
  <body>
    <header>
      Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
    </header>
    <canvas id="canvas" width="400" height="400"></canvas>
    <aside>Move mouse on canvas element.</aside>
    
    <script src="../include/utils.js"></script>
    <script>
    window.onload = function () {
      var canvas = document.getElementById(‘canvas‘),
          context = canvas.getContext(‘2d‘),
          mouse = utils.captureMouse(canvas),
          x0 = 100,
          y0 = 200,
          x2 = 300,
          y2 = 200;
        
      canvas.addEventListener(‘mousemove‘, function () {
        context.clearRect(0, 0, canvas.width, canvas.height);

        var x1 = mouse.x * 2 - (x0 + x2) / 2,
            y1 = mouse.y * 2 - (y0 + y2) / 2;

        //curve through mouse
        context.beginPath();
        context.moveTo(x0, y0);
        context.quadraticCurveTo(x1, y1, x2, y2);
        context.stroke();
      }, false);
    };
    </script>
  </body>
</html>
View Code

3.多重曲线

动画原理——曲线

标签:

原文地址:http://www.cnblogs.com/winderby/p/4250834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!