美文网首页
简单的缓动

简单的缓动

作者: 做一个胆小鬼 | 来源:发表于2017-12-04 15:10 被阅读0次
实现缓动的策略:
  • 为运动确定一个比例系数,这是一个小于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();  
}

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

相关文章

  • 简单的缓动

    实现缓动的策略: 为运动确定一个比例系数,这是一个小于1且大于0的小数 确定目标点 计算出物体与目标点的距离- 计...

  • 缓动框架

    缓动框架 在body中创建一个按钮 设置样式 程序运行起来的时候书写算法 运行之后的效果

  • 缓动框架

    封装框架遇到的两个问题 原有的方法:div .style .width:这个方法比较固定,不能用变量或者字符串的形...

  • 缓动函数

    链接 欢迎大家访问我的个人网站四度空间 Fuse? 很多时候,设计师设计一个特效动画出来,用静态的UE是很难向开发...

  • Tween缓动

    效果图: https://easings.net[https://easings.net]

  • DoTween

    doTween使用 :SetEase(缓动枚举)----dotween自带缓动枚举 onComplete-----...

  • 流动之美·提升用户体验的交互动效(四)

    Point 1. Easy Curves 缓动曲线 以下图为例,大家可以观察到基础缓动曲线的速度变化,更多的缓动...

  • 动效设置基础:缓动

    欢迎关注我的微信公众号:侠侠说! 先放结论:进入屏幕用减速(ease-out);离开屏幕用加速(ease-in);...

  • 缓动框架封装

  • 42、缓动效果

    动画中的三次贝塞尔曲线可以定义动画的进度,设置相应的值可以使动画突破动画本身的范围 可以看到元素的缩放动画,会使元...

网友评论

      本文标题:简单的缓动

      本文链接:https://www.haomeiwen.com/subject/rzrxixtx.html