簡(jiǎn)單的緩動(dòng)

實(shí)現(xiàn)緩動(dòng)的策略:
  • 為運(yùn)動(dòng)確定一個(gè)比例系數(shù),這是一個(gè)小于1且大于0的小數(shù)

  • 確定目標(biāo)點(diǎn)

  • 計(jì)算出物體與目標(biāo)點(diǎn)的距離- 計(jì)算速度,速度 = 距離 x 比例系數(shù)

  • 用當(dāng)前位置加上速度來(lái)計(jì)算新的位置

  • 重復(fù)第三步到第五步,直到物體到達(dá)目標(biāo)

程序?qū)崿F(xiàn):

首先確定一個(gè)比例系數(shù),比如設(shè)置為0.05,用變量easing表示:

var easing = 0.05;

接著確定目標(biāo)點(diǎn),在這里我們使用canvas元素的中心點(diǎn):

var targetX = canvas.width / 2,

      targetY = canvas.height / 2;

然后計(jì)算物體到目標(biāo)點(diǎn)的距離。假設(shè)這個(gè)物體是一個(gè)小球,我們用ball來(lái)表示,小球的位置坐標(biāo)為(x, y),目標(biāo)點(diǎn)減去小球所在位置得到距離:

var dx = targetX - ball.x,

      dy = targetY - ball.y;

速度就是距離乘以系數(shù):

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, // 比例系數(shù)
            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();  
}

至此我們已經(jīng)學(xué)習(xí)完了簡(jiǎn)單的緩動(dòng)運(yùn)動(dòng)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 看了很多視頻、文章,最后卻通通忘記了,別人的知識(shí)依舊是別人的,自己卻什么都沒獲得。此系列文章旨在加深自己的印象,因...
    DCbryant閱讀 835評(píng)論 0 2
  • # @姿羽-160201-《如何閱讀一本書》序言及第一章 ### 四個(gè)收獲 1. 序言可以這么讀:永誠(chéng)一上來(lái)就說(shuō)從...
    姿羽閱讀 294評(píng)論 0 0
  • 今天兩件大事。 一是閨蜜結(jié)婚了,穿著婚禮的她簡(jiǎn)直美成了一朵花。我覺得求婚和婚禮都是她想要的樣子,看得出,付出了很多...
    三月在南方閱讀 331評(píng)論 0 0
  • 原名《雍.雅》 第一百七十四章 一餐陽(yáng)世飯不易 四季陰間人好難 這是一個(gè)有意思的年代,神奇又幽默,讓人不愿相信卻又...
    何來(lái)雍雅閱讀 383評(píng)論 2 4
  • 問自己5個(gè)問題: 1.你喜歡現(xiàn)在的自己?jiǎn)幔?超愛?,F(xiàn)在的我在為自己的未來(lái)而努力,這樣的自己再好不過了。 2.你有一...
    歌唄lrf閱讀 272評(píng)論 0 0

友情鏈接更多精彩內(nèi)容