tweenjs缓动算法使用小实例
这里的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缓动算法使用小实例的更多相关文章
- 做h5动画会用到的一个很好的缓动算法库
http://www.zhangxinxu.com/wordpress/2016/12/how-use-tween-js-animation-easing/
- window.requestAnimationFrame与Tween.js配合使用实现动画缓动效果
window.requestAnimationFrame 概述 window.requestAnimationFrame()这个方法是用来在页面重绘之前,通知浏览器调用一个指定的函数,以满足开发者操作 ...
- 《JavaScript 实战》:Tween 算法及缓动效果
Flash 做动画时会用到 Tween 类,利用它可以做很多动画效果,例如缓动.弹簧等等.我这里要教大家的是怎么利用 Flash 的 Tween 类的算法,来做js的Tween算法,并利用它做一些简单 ...
- 软件项目技术点(1)——Tween算法及缓动效果
AxeSlide软件项目梳理 canvas绘图系列知识点整理 Tween算法及缓动效果 软件里在切换步序时需要有过渡动画效果,从当前位置的画面缓动到目标位置的画面.动画效果可重新查看文章系列第一篇 ...
- 利用tween.js算法生成缓动效果
在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等 ...
- 网站建设中前端常用的jQuery+easing缓动的动画
网站建设中前端人员利用jQuery实现动画再简单不过了,只是要实现更酷的效果还需要插件来帮忙,easing就是一款帮助jQuery实现缓动动画的插件,经过试用,效果很不错! 下载该插件:jquery. ...
- 回到顶部缓动效果代码 --- tween动画函数库
function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...
- Windows Phone开发(42):缓动动画
原文:Windows Phone开发(42):缓动动画 前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame.EasingDoubleKeyF ...
- JS实现缓动动画效果
原理如下: 假设要从数值A变化到数值B,如果是线性运动,则每次移动距离是一样:如果是缓动,每次移动距离不一样.那如何才能不一样呢?很简单,按比例移动就可以. 例如:每次移动剩余距离的一半. 对吧,超容 ...
随机推荐
- 视频网站数据MapReduce清洗及Hive数据分析
一.需求描述 利用MapReduce清洗视频网站的原数据,用Hive统计出各种TopN常规指标: 视频观看数 Top10 视频类别热度 Top10 视频观看数 Top20 所属类别包含这 Top20 ...
- Flask后台管理
管理后台主页 需求 为后台主页提供专门的视图函数 需要带入当前管理员用户相关信息以便在界面进行展示 代码实现 在 modules/admin/views.py 文件中添加视图函数 @admin_blu ...
- 最小化的测试套件minimal_test的使用
1:需要包含文件文#include <boost/test/minimal_test.hpp> 2:minimal_test内部实现了main(), 因此无需自己编写main()函数, 只 ...
- [原创]用python检测LVS real server状态实现HTTP高可用
import httplib import os import time def check_http(i): try: conn=httplib.HTTPConnection(i, 80, time ...
- Java设计模式(11)——结构型模式之享元模式(Flyweight)
一.概述 概念 避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类). UML简图 单纯享元模式 角色 抽象享元角色(Flyweight):定义享元子类公共接口 具体享元角色(Co ...
- treetable--.net webform中树形table表管理的应用
好记性不如烂笔头. treetable 官网:http://ludo.cubicphuse.nl/jquery-treetable/#api 今天要做一个关于table的树节点展示,还有编辑,删除功能 ...
- 「日常训练」Case of Matryoshkas(Codeforces Round #310 Div. 2 C)
题意与分析(CodeForces 556C) 为了将所有\(n\)个娃娃编号递增地串在一起(原先是若干个串,每个串是递增的), 我们有两种操作: 拆出当前串中最大编号的娃娃(且一定是最右边的娃娃). ...
- hdu1517A Multiplication Game(巴什博弈变形)
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- Allure--自动化测试报告生成
之前尝试使用过testNG自带的测试报告.优化过reportNG的测试报告,对这两个报告都不能满意.后经查找资料,发现有个神器: Allure(已经有allure2了,笔者使用的就是allure2), ...
- Selenium自动化测试第二天(下)
如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...