这里的tweenjs不是依托于createjs的tewwnjs,而是一系列缓动算法集合。因为本身是算法,可以用在各个业务场景中,这也正是总结学习它的价值所在。tweenjs代码详情:

 /*
* Tween.js
* t: current time(当前时间);
* b: beginning value(初始值);
* c: change in value(变化量);
* d: duration(持续时间)。
* you can visit 'http://easings.net/zh-cn' to get effect
*/
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-) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / ) < ) return c / * t * t + b;
return -c / * ((--t) * (t-) - ) + 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 - ) * t * t + ) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / ) < ) return c / * t * t*t + b;
return c / *((t -= ) * t * t + ) + 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 - ) * t * t*t - ) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / ) < ) return c / * t * t * t * t + b;
return -c / * ((t -= ) * t * t*t - ) + 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 - ) * t * t * t * t + ) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / ) < ) return c / * t * t * t * t * t + b;
return c / *((t -= ) * t * t * t * t + ) + b;
}
},
Sine: {
easeIn: function(t, b, c, d) {
return -c * Math.cos(t/d * (Math.PI/)) + c + b;
},
easeOut: function(t, b, c, d) {
return c * Math.sin(t/d * (Math.PI/)) + b;
},
easeInOut: function(t, b, c, d) {
return -c / * (Math.cos(Math.PI * t/d) - ) + b;
}
},
Expo: {
easeIn: function(t, b, c, d) {
return (t==) ? b : c * Math.pow(, * (t/d - )) + b;
},
easeOut: function(t, b, c, d) {
return (t==d) ? b + c : c * (-Math.pow(, - * t/d) + ) + b;
},
easeInOut: function(t, b, c, d) {
if (t==) return b;
if (t==d) return b+c;
if ((t /= d / ) < ) return c / * Math.pow(, * (t - )) + b;
return c / * (-Math.pow(, - * --t) + ) + b;
}
},
Circ: {
easeIn: function(t, b, c, d) {
return -c * (Math.sqrt( - (t /= d) * t) - ) + b;
},
easeOut: function(t, b, c, d) {
return c * Math.sqrt( - (t = t/d - ) * t) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / ) < ) return -c / * (Math.sqrt( - t * t) - ) + b;
return c / * (Math.sqrt( - (t -= ) * t) + ) + b;
}
},
Elastic: {
easeIn: function(t, b, c, d, a, p) {
var s;
if (t==) return b;
if ((t /= d) == ) return b + c;
if (typeof p == "undefined") p = d * .;
if (!a || a < Math.abs(c)) {
s = p / ;
a = c;
} else {
s = p / ( * Math.PI) * Math.asin(c / a);
}
return -(a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
},
easeOut: function(t, b, c, d, a, p) {
var s;
if (t==) return b;
if ((t /= d) == ) return b + c;
if (typeof p == "undefined") p = d * .;
if (!a || a < Math.abs(c)) {
a = c;
s = p / ;
} else {
s = p/(*Math.PI) * Math.asin(c/a);
}
return (a * Math.pow(, - * t) * Math.sin((t * d - s) * ( * Math.PI) / p) + c + b);
},
easeInOut: function(t, b, c, d, a, p) {
var s;
if (t==) return b;
if ((t /= d / ) == ) return b+c;
if (typeof p == "undefined") p = d * (. * 1.5);
if (!a || a < Math.abs(c)) {
a = c;
s = p / ;
} else {
s = p / ( *Math.PI) * Math.asin(c / a);
}
if (t < ) return -. * (a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
return a * Math.pow(, - * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p ) * . + c + b;
}
},
Back: {
easeIn: function(t, b, c, d, s) {
if (typeof s == "undefined") s = 1.70158;
return c * (t /= d) * t * ((s + ) * t - s) + b;
},
easeOut: function(t, b, c, d, s) {
if (typeof s == "undefined") s = 1.70158;
return c * ((t = t/d - ) * t * ((s + ) * t + s) + ) + b;
},
easeInOut: function(t, b, c, d, s) {
if (typeof s == "undefined") s = 1.70158;
if ((t /= d / ) < ) return c / * (t * t * (((s *= (1.525)) + ) * t - s)) + b;
return c / *((t -= ) * t * (((s *= (1.525)) + ) * t + s) + ) + b;
}
},
Bounce: {
easeIn: function(t, b, c, d) {
return c - Tween.Bounce.easeOut(d-t, , c, d) + b;
},
easeOut: function(t, b, c, d) {
if ((t /= d) < ( / 2.75)) {
return c * (7.5625 * t * t) + b;
} else if (t < ( / 2.75)) {
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .) + b;
} else if (t < (2.5 / 2.75)) {
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .) + b;
} else {
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .) + b;
}
},
easeInOut: function(t, b, c, d) {
if (t < d / ) {
return Tween.Bounce.easeIn(t * , , c, d) * . + b;
} else {
return Tween.Bounce.easeOut(t * - d, , c, d) * . + c * . + b;
}
}
}
}
Math.tween = Tween;

具体每种算法的操作实例,可以看大神张鑫旭的博客实例:http://www.zhangxinxu.com/study/201612/how-to-use-tween-js.html

当然,以上这些算法仅仅是一个状态,需要配合时间上的变化,才能实现缓动,这里使用的是requestAnimationFrame,具体代码好吧,也是拿来主义

 (function() {
var lastTime = ;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = ; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
} if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(, - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
}; if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());

最后是简单的实例应用,很简单,

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>使用tweenjs</title>
<style type="text/css">
div {
width: 100px;
height: 100px;
border: 1px solid red;
text-align: center;
line-height: 100px;
position: absolute;
}
</style>
</head> <body>
<div id="test">
这是测试
</div>
<script type="text/javascript" src="RequestAnimationFrame.js"></script>
<script type="text/javascript" src="tween.js"></script>
<script type="text/javascript">
var DOM=document.getElementById("test");
var t = ,//开始时间
b = ,//开始位置
c = ,//变化值
d = ;//持续时间
var step = function() {
var value = Tween.Bounce.easeOut(t, b, c, d);
DOM.style.left = value + 'px'; t++;
if (t <= d) {
// 继续运动
requestAnimationFrame(step);
} else {
// 动画结束
}
};
step();
</script>
</body> </html>

具体使用中,需要参数以及控制好结束条件即可。

tweenjs缓动算法使用小实例的更多相关文章

  1. 做h5动画会用到的一个很好的缓动算法库

    http://www.zhangxinxu.com/wordpress/2016/12/how-use-tween-js-animation-easing/

  2. window.requestAnimationFrame与Tween.js配合使用实现动画缓动效果

    window.requestAnimationFrame 概述 window.requestAnimationFrame()这个方法是用来在页面重绘之前,通知浏览器调用一个指定的函数,以满足开发者操作 ...

  3. 《JavaScript 实战》:Tween 算法及缓动效果

    Flash 做动画时会用到 Tween 类,利用它可以做很多动画效果,例如缓动.弹簧等等.我这里要教大家的是怎么利用 Flash 的 Tween 类的算法,来做js的Tween算法,并利用它做一些简单 ...

  4. 软件项目技术点(1)——Tween算法及缓动效果

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 Tween算法及缓动效果 软件里在切换步序时需要有过渡动画效果,从当前位置的画面缓动到目标位置的画面.动画效果可重新查看文章系列第一篇 ...

  5. 利用tween.js算法生成缓动效果

    在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等 ...

  6. 网站建设中前端常用的jQuery+easing缓动的动画

    网站建设中前端人员利用jQuery实现动画再简单不过了,只是要实现更酷的效果还需要插件来帮忙,easing就是一款帮助jQuery实现缓动动画的插件,经过试用,效果很不错! 下载该插件:jquery. ...

  7. 回到顶部缓动效果代码 --- tween动画函数库

    function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...

  8. Windows Phone开发(42):缓动动画

    原文:Windows Phone开发(42):缓动动画 前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame.EasingDoubleKeyF ...

  9. JS实现缓动动画效果

    原理如下: 假设要从数值A变化到数值B,如果是线性运动,则每次移动距离是一样:如果是缓动,每次移动距离不一样.那如何才能不一样呢?很简单,按比例移动就可以. 例如:每次移动剩余距离的一半. 对吧,超容 ...

随机推荐

  1. ruby中的三目操作符和include?操作

    三目操作符:口?口:口 问号前面的是布尔型的判断,true的话执行第二个方块的语句,false的话执行第三个方块的语句例如:value =(nil ? 0 : 1)p value=>1 .inc ...

  2. C Mingw gcc printf 刷新缓冲行

    C Mingw gcc printf 刷新缓冲行 参考:https://stackoverflow.com/questions/13035075/printf-not-printing-on-cons ...

  3. R tutorial

    http://www.clemson.edu/economics/faculty/wilson/R-tutorial/Introduction.html https://www.youtube.com ...

  4. 【BZOJ4753】最佳团体(分数规划,动态规划)

    [BZOJ4753]最佳团体(分数规划,动态规划) 题面 BZOJ Description JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号.每个候选人都由一 ...

  5. android stadio 编译报错:download fastutil-7.2.0.jar

    在Ubuntu上面,新安装的stadio,第一次编译项目的时候, 一直开在下载 fastutil-7.2.0.jar 原因是需要FQ.那么改一下你的buil.gradle buildscript { ...

  6. MySQL高级-showProfile

    一.Show Profile是什么? 是MySql提供可以用来分析当前会话中语句执行的资源消耗情况,可以用于SQL的调优的测量. 默认情况下,参数处于关闭状态,并保存最近15次的运行结果. 二.分析步 ...

  7. eclipse+tomcat配置远程debug调整

    由于开发环境与真实服务器环境存在差异,有时开发时明明正常的逻辑,部署之后就会出现各种各样的问题,通过日志邮不能明确定位到问题的时候,可以采用远程debug调试来定位问题.下面就介绍一下具体的配置步骤: ...

  8. 吴裕雄 python 机器学习——层次聚类AgglomerativeClustering模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  9. WEB安全基础之sql注入基础

    1.基础sql语句 注释 单行注释# %23--+ --加空格多行注释/**/ SELECT(VERSION()) SELECT(USER()) SELECT(database()) 查数据库 SEL ...

  10. 【view】 视图组件说明

    view 是视图容器,可用于包裹其它组件或文本内容. 原型: <view hover-class="[String]" hover-stop-propagation=&quo ...