tween補(bǔ)間動(dòng)畫與封裝函數(shù)

[Toc]

translateX與left的取舍

  • 為了讓一個(gè)div盒子運(yùn)動(dòng)起來,可以通過修改translateX使它運(yùn)動(dòng),也可以通過定位并修改left使它水平運(yùn)動(dòng).

  • timeline-frames-macbook1.png

左上方的圖片是通過改變?cè)豻op/left進(jìn)行動(dòng)畫的幀率,而右上方則是調(diào)用translate函數(shù)的幀率铣口。我們可以明顯看出左圖的每一幀都執(zhí)行了相對(duì)較長(zhǎng)時(shí)間的paint砰盐,在每一幀內(nèi)涮母,cpu都需要計(jì)算該元素的其他樣式,特別是相對(duì)需要復(fù)雜計(jì)算的盒陰影湖饱,漸變,圓角等樣式,最后都需要將這些樣式應(yīng)用到該元素內(nèi)廊谓。從這個(gè)角度看,如果對(duì)于較為老舊的移動(dòng)設(shè)備進(jìn)行相對(duì)復(fù)雜的動(dòng)畫麻削,那么效果肯定不理想蒸痹。
而通過調(diào)用 translate,會(huì)啟動(dòng)硬件加速呛哟,即在 GPU 層對(duì)該元素進(jìn)行渲染叠荠。這樣,CPU 就會(huì)相對(duì)解放出來進(jìn)行其他的計(jì)算扫责,GPU 對(duì)樣式的計(jì)算相對(duì)較快榛鼎,且保證較大的幀率。我們可以通過 2d 和 3d 的 transform 來啟用 GPU 計(jì)算。

  • 簡(jiǎn)要總結(jié):因?yàn)閠ranslateX會(huì)使用GPU對(duì)元素進(jìn)行渲染,解放CPU的算力,因此相較于直接修改left,使用translateX對(duì)硬件更友好.

補(bǔ)間動(dòng)畫的代碼

    var Tween = {
        Linear: function (t, b, c, d) {
            return c * t / d + b;
        },
        Quad: {
            easeIn: function (t, b, c, d) {
                return c * (t /= d) * t + b;
            },
            easeOut: function (t, b, c, d) {
                return -c * (t /= d) * (t - 2) + b;
            },
            easeInOut: function (t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t + b;
                return -c / 2 * ((--t) * (t - 2) - 1) + b;
            }
        },
        Cubic: {
            easeIn: function (t, b, c, d) {
                return c * (t /= d) * t * t + b;
            },
            easeOut: function (t, b, c, d) {
                return c * ((t = t / d - 1) * t * t + 1) + b;
            },
            easeInOut: function (t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
                return c / 2 * ((t -= 2) * t * t + 2) + b;
            }
        },
        Quart: {
            easeIn: function (t, b, c, d) {
                return c * (t /= d) * t * t * t + b;
            },
            easeOut: function (t, b, c, d) {
                return -c * ((t = t / d - 1) * t * t * t - 1) + b;
            },
            easeInOut: function (t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
                return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
            }
        },
        Quint: {
            easeIn: function (t, b, c, d) {
                return c * (t /= d) * t * t * t * t + b;
            },
            easeOut: function (t, b, c, d) {
                return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
            },
            easeInOut: function (t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
                return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
            }
        },
        Sine: {
            easeIn: function (t, b, c, d) {
                return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
            },
            easeOut: function (t, b, c, d) {
                return c * Math.sin(t / d * (Math.PI / 2)) + b;
            },
            easeInOut: function (t, b, c, d) {
                return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
            }
        },
        Expo: {
            easeIn: function (t, b, c, d) {
                return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
            },
            easeOut: function (t, b, c, d) {
                return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
            },
            easeInOut: function (t, b, c, d) {
                if (t == 0) return b;
                if (t == d) return b + c;
                if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
                return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
            }
        },
        Circ: {
            easeIn: function (t, b, c, d) {
                return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
            },
            easeOut: function (t, b, c, d) {
                return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
            },
            easeInOut: function (t, b, c, d) {
                if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
                return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
            }
        },
        Elastic: {
            easeIn: function (t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d) == 1) return b + c;
                if (!p) p = d * .3;
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
            },
            easeOut: function (t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d) == 1) return b + c;
                if (!p) p = d * .3;
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
            },
            easeInOut: function (t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d / 2) == 2) return b + c;
                if (!p) p = d * (.3 * 1.5);
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
                return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
            }
        },
        Back: {
            easeIn: function (t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                return c * (t /= d) * t * ((s + 1) * t - s) + b;
            },
            easeOut: function (t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
            },
            easeInOut: function (t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
                return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
            }
        },
        Bounce: {
            easeIn: function (t, b, c, d) {
                return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
            },
            easeOut: function (t, b, c, d) {
                if ((t /= d) < (1 / 2.75)) {
                    return c * (7.5625 * t * t) + b;
                } else if (t < (2 / 2.75)) {
                    return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
                } else if (t < (2.5 / 2.75)) {
                    return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
                } else {
                    return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
                }
            },
            easeInOut: function (t, b, c, d) {
                if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
                else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
            }
        }
    }

函數(shù)說明

  1. Linear:線性勻速運(yùn)動(dòng)效果者娱;
  2. Quadratic:二次方的緩動(dòng)(t^2)抡笼;
  3. Cubic:三次方的緩動(dòng)(t^3);
  4. Quartic:四次方的緩動(dòng)(t^4)肺然;
  5. Quintic:五次方的緩動(dòng)(t^5)蔫缸;
  6. Sinusoidal:正弦曲線的緩動(dòng)(sin(t));
  7. Exponential:指數(shù)曲線的緩動(dòng)(2^t)际起;
  8. Circular:圓形曲線的緩動(dòng)(sqrt(1-t^2))拾碌;
  9. Elastic:指數(shù)衰減的正弦曲線緩動(dòng);
  10. Back:超過范圍的三次方緩動(dòng)((s+1)t^3 – st^2)街望;
  11. Bounce:指數(shù)衰減的反彈緩動(dòng)校翔。

參數(shù)說明

/*
 * t: current time(當(dāng)前時(shí)間);
 * b: beginning value(初始值)灾前;
 * c: change in value(變化量)防症;
 * d: duration(持續(xù)時(shí)間)。
*/

補(bǔ)間動(dòng)畫的使用

  • 使一個(gè)盒子從{left:500px}處開始,5000ms內(nèi)水平線性勻速運(yùn)動(dòng)-300px

    •         let oD1 = document.querySelector('#d1')
      
              function Linear(t, b, c, d) {
                  return c * t / d + b
              }
              let time = 0
              let timer = setInterval(() => {
                  time += 1000 / 60
                  let left = Linear(time, 0, -400, 5000)
                  oD1.style.transform = 'translateX(' + left + 'px)'
                  if (time >= 5000) {
                      clearInterval(timer)
                      timer = null
                  }
              }, 1000 / 60)
      
    • 可以使用requestAnimationFrame代替setInterval

補(bǔ)間動(dòng)畫的封裝

/* 
    示例:
    TweenAnimation(oWrapper,'translateX',0,500,5000,60,'linear')

*/
/**
 * @description: 
 * @param {*} el 對(duì)象
 * @param {*} style 屬性字符串
 * @param {*} startPos 起始位置
 * @param {*} endPos 終點(diǎn)位置
 * @param {*} duration 持續(xù)時(shí)間
 * @param {*} frame 幀數(shù)
 * @param {*} type 動(dòng)畫類型
 * @param {*} callback 回調(diào)函數(shù)
 * @return {*} 沒有返回值
 */
function tweenAnimation(el, style, startPos, displacement, duration, frame, type, callback) {
    var Tween = {
        // 復(fù)制上面的補(bǔ)間動(dòng)畫在此處
    }
    // 初始化參數(shù)
    el = (typeof el === 'object') ? el : document.querySelector(el)
    let fn = null
    if (/(\w+)\.?(\w+)?/g.test(type)) {
        if (RegExp.$2) {
            fn = Tween[RegExp.$1][RegExp.$2] ? Tween[RegExp.$1][RegExp.$2] : Tween.Linear
        } else {
            fn = Tween[RegExp.$1] ? Tween[RegExp.$1] : Tween.Linear
        }
    }
    frame = typeof frame === 'number' ? frame : 60
    // 設(shè)定timer的儲(chǔ)存變量
    if (el[style] === undefined) {
        el[style] = {}
    }
    // 設(shè)定interval
    let dis
    let time = 0

    function animation() {
        time += (1000 / frame)
        dis = fn(time, startPos, displacement, duration)
        transformCSS(el, style, dis)
        if (time < duration) {
            el[style].requestID = requestAnimationFrame(animation, 1000 / frame)
        }
    }
    el[style].requestID = requestAnimationFrame(animation, 1000 / frame)
}
  • 通過將requestAnimationFrame的請(qǐng)求ID保存在el[style].requestID中,達(dá)到可以隨時(shí)取消補(bǔ)間動(dòng)畫的效果(通過cancaelAnimationFrame取消)

參考博客

如何使用Tween.js各類原生動(dòng)畫運(yùn)動(dòng)緩動(dòng)算法


10.30

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末哎甲,一起剝皮案震驚了整個(gè)濱河市蔫敲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌炭玫,老刑警劉巖奈嘿,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異吞加,居然都是意外死亡裙犹,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門衔憨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叶圃,“玉大人,你說我怎么就攤上這事践图〔艄冢” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵平项,是天一觀的道長(zhǎng)赫舒。 經(jīng)常有香客問我,道長(zhǎng)闽瓢,這世上最難降的妖魔是什么接癌? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮扣讼,結(jié)果婚禮上缺猛,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好荔燎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布耻姥。 她就那樣靜靜地躺著,像睡著了一般有咨。 火紅的嫁衣襯著肌膚如雪琐簇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天座享,我揣著相機(jī)與錄音婉商,去河邊找鬼。 笑死渣叛,一個(gè)胖子當(dāng)著我的面吹牛丈秩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播淳衙,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蘑秽,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了箫攀?” 一聲冷哼從身側(cè)響起肠牲,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎靴跛,沒想到半個(gè)月后埂材,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡汤求,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了严拒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扬绪。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖裤唠,靈堂內(nèi)的尸體忽然破棺而出挤牛,到底是詐尸還是另有隱情,我是刑警寧澤种蘸,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布墓赴,位于F島的核電站,受9級(jí)特大地震影響航瞭,放射性物質(zhì)發(fā)生泄漏诫硕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一刊侯、第九天 我趴在偏房一處隱蔽的房頂上張望章办。 院中可真熱鬧,春花似錦、人聲如沸藕届。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽休偶。三九已至梁厉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間踏兜,已是汗流浹背词顾。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留庇麦,地道東北人计技。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像山橄,于是被迫代替她去往敵國(guó)和親垮媒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354