简单的缓动

实现缓动的策略:
  • 为运动确定一个比例系数,这是一个小于1且大于0的小数

  • 确定目标点

  • 计算出物体与目标点的距离- 计算速度,速度 = 距离 x 比例系数

  • 用当前位置加上速度来计算新的位置

  • 重复第三步到第五步,直到物体到达目标

程序实现:

首先确定一个比例系数,比如设置为0.05,用变量easing表示:

var easing = 0.05;

接着确定目标点,在这里我们使用canvas元素的中心点:

var targetX = canvas.width / 2,

      targetY = canvas.height / 2;

然后计算物体到目标点的距离。假设这个物体是一个小球,我们用ball来表示,小球的位置坐标为(x, y),目标点减去小球所在位置得到距离:

var dx = targetX - ball.x,

      dy = targetY - ball.y;

速度就是距离乘以系数:

var vx = dx * easing,

      vy = dy * easing;

ball.x += vx;

ball.y += vy;

完整代码如下:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Easing</title>
  </head>
  <body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script src="js/ball.js"></script>
    <script>
      window.onload = function () {
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext('2d'),
            ball = new Ball(),
            easing = 0.05, // 比例系数
            targetX = canvas.width / 2,
            targetY = canvas.height / 2;

            (function drawFrame () {
              window.requestAnimationFrame(drawFrame, canvas);
              context.clearRect(0, 0, canvas.width, canvas.height);

              var vx = (targetX - ball.x) * easing,
                  vy = (targetY - ball.y) * easing;

              ball.x += vx;
              ball.y += vy;

              ball.draw(context);
            } ())
      }
    </script>
  </body>
</html>

javascript部分:


// ball.js 

var utils = {   
     parseColor: function (color, toNumber) {  
         if (toNumber === true) {  
             if (typeof color === 'number') { 
                  return (color | 0);  
             }  
             if (typeof color === 'string' && color[0] === '#') { 
                 color = color.slice(1);  
             }  
            return window.parseInt(color, 16);  
         }  
         else { 
            if (typeof color === 'number') {  
                color = '#' + ('0000' + (color | 0).toString(16)).substr(-6);  
            } 
            return color; 
        } 
    }  
};

function Ball (radius, color) {    
    color = color || '#ff0000';    
    this.radius = radius; 
    this.color = utils.parseColor(color); 
    this.x = 0; 
    this.y = 0;
    this.rotation = 0; 
    this.vx = 0; 
    this.vy = 0; 
    this.scaleX = 1; 
    this.scaleY = 1; 
    this.lineWidth = 1;  
}  
Ball.prototype.draw = function (context) {    
    context.save(); 
    context.translate(this.x, this.y); 
    context.rotate(this.rotation); 
    context.scale(this.scaleX, this.scaleY); 
    context.lineWidth = this.lineWidth; 
    context.fillStyle = this.color; 
    context.beginPath(); 
    context.arc(0, 0, this.radius, 0, (Math.PI * 2), true); 
    context.closePath(); 
    context.fill(); 
   if (this.lineWidth > 0) {  
       context.stroke(); 
   } 
   context.restore();  
}

至此我们已经学习完了简单的缓动运动。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 4,101评论 0 2
  • # @姿羽-160201-《如何阅读一本书》序言及第一章 ### 四个收获 1. 序言可以这么读:永诚一上来就说从...
    姿羽阅读 1,729评论 0 0
  • 今天两件大事。 一是闺蜜结婚了,穿着婚礼的她简直美成了一朵花。我觉得求婚和婚礼都是她想要的样子,看得出,付出了很多...
    三月在南方阅读 1,895评论 0 0
  • 原名《雍.雅》 第一百七十四章 一餐阳世饭不易 四季阴间人好难 这是一个有意思的年代,神奇又幽默,让人不愿相信却又...
    何来雍雅阅读 2,773评论 2 4
  • 问自己5个问题: 1.你喜欢现在的自己吗? 超爱。现在的我在为自己的未来而努力,这样的自己再好不过了。 2.你有一...
    歌呗lrf阅读 1,684评论 0 0

友情链接更多精彩内容