tween.js简介

tween.js简介

引言

我以前是做unity的,在unity中经常使用Dotween来做一些简单的动画,学习threejs后,有些相关的业务也需要做一些简单的动画,我就想着看js中有没有类似于dotween的,查了查发现了tweenjs。这里对tweenjs做一些简单的介绍,加深我的学习。

@tweenjs/tween.js - npm (npmjs.com)

tweenjs 是使用 JavaScript 中的一个简单的补间动画库,支持数字、对象的属性和 CSS 样式属性的赋值,并允许链接补间动画和行动结合起来,创造出复杂的序列。

tweenjs 以平滑的方式修改元素的属性值,需要传递给 tween 要修改的值、动画结束时的最终值和动画花费时间(duration),之后 tween 引擎就可以计算从开始动画点到结束动画点之间值,从而产生平滑的动画效果。和dotween的效果一样,就是使用方法稍微有点区别。

安装

# yarn
yarn add @tweenjs/tween.js
# npm
npm install @tweenjs/tween.js --save

使用

添加引用

import Tween from '@tweenjs/tween.js';

方法简介

开始和停止

start和stop方法来控制tween动画的开始和停止。

let position = {x:100,y:100,z:100};
let testTween = new TWEEN.Tween(position);
testTween.start(); //开始
testTween.stop(); //停止

start方法可以接收一个时间参数,如果使用了时间参数,表示动画将等待该时间之后执行,等同于delay。

动画组合

chain

重复执行

repeat方法接收一个用于描述重复多少次的参数,无穷的话参数传递Infinity。

testTween.repeat(10); //重复执行10次然后停止
testTween.repeat(Infinity); //动画一直执行

yoyo方法只在repeat时起作用,只运行一次时没有效果。传递参数boolean,当为true时,动画效果类似于yoyo球效果,动画在开始或结束处向反方向运行。

testTween.yoyo(true); //启用yoyo效果

比如某一个动画是从1变动到100,如果重复的话,到100之后会从1重新开始,如果使用yoyo,则到100之后会从100缓动到1,再从1缓动到100。

延迟

delay方法用来控制动画的延时。

testTween.delay(5000); //延时5S执行
全局方法
方法介绍
TWEEN.update该方法用于所有被激活的tweens,传递的参数是时间,如果没有传递参数则使用当前时间。
TWEEN.getAll该方法用户获取当前所有被激活的tweens,返回一个数组。
TWEEN.removeAll该方法删除所有的tweens。
TWEEN.add该方法向已经激活的tween数组中添加一个tween对象,参数为tween对象。
TWEEN.remove该方法从已经激活的tween数组中移除一个tween对象,参数为tween对象。

easing

函数简介
Linear线性匀速运动效果
Quadratic二次方的缓动
Cubic三次方的缓动
Quartic四次方的缓动
Quintic五次方的缓动
Sinusoidal正弦曲线的缓动
Exponential指数曲线的缓动
Circular圆形曲线的缓动
Elastic指数衰减的正弦曲线缓动
Back超过范围的三次方的缓动
Bounce指数衰减的反弹缓动

类型

类型简介
In加速,先慢后快
Out减速,先快后慢
InOut前半段加速,后半段减速

具体表现效果如下:

Unity Dotween曲线介绍 Ease曲线 Ease图表 Ease效果示例_众里寻春秋的博客-CSDN博客_ease曲线

img

使用方法

.easing(TWEEN.Easing.easing函数.easing类型)
//例子
//.easing(TWEEN.Easing.Quadratic.Out);

回调函数

函数介绍
onStart动画开始时触发
onStop动画stop时触发
onUpdatetween每次更新时触发
onComplete动画全部结束时触发
testTween.onStart(function (object) {
    console.log("start");
}).onUpdate(function (object) {
    console.log("update");
});

完整的例子

let position = {x:100,y:100,z:100};
let testTween = new TWEEN.Tween(position);
testTween.to({x:200,y:200,z:200}, 1000);
testTween.onStart(function () {
    console.log("start");
}).onUpdate(function (object) {
    console.log("update", object);
}).onComplete(function(){
    console.log("complete");
});
testTween.easing(TWEEN.Easing.Quadratic.Out);
testTween.yoyo(true);
testTween.repeat(10);
testTween.start(); //开始

参考文档

Unity Dotween曲线介绍 Ease曲线 Ease图表 Ease效果示例_众里寻春秋的博客-CSDN博客_ease曲线

如果有写的不对的地方,欢迎各位大佬批评指正。

  • 14
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
/*! * @license TweenJS * Visit http://createjs.com/ for documentation, updates and examples. * * Copyright (c) 2011-2015 gskinner.com, inc. * * Distributed under the terms of the MIT license. * http://www.opensource.org/licenses/mit-license.html * * This notice shall be included in all copies or substantial portions of the Software. */ this.createjs=this.createjs||{},createjs.extend=function(a,b){"use strict";function c(){this.constructor=a}return c.prototype=b.prototype,a.prototype=new c},this.createjs=this.createjs||{},createjs.promote=function(a,b){"use strict";var c=a.prototype,d=Object.getPrototypeOf&&Object;.getPrototypeOf(c)||c.__proto__;if(d){c[(b+="_")+"constructor"]=d.constructor;for(var e in d)c.hasOwnProperty(e)&&"function"==typeof d[e]&&(c[b+e]=d[e])}return a},this.createjs=this.createjs||{},createjs.deprecate=function(a,b){"use strict";return function(){var c="Deprecated property or method '"+b+"'. See docs for info.";return console&&(console.warn?console.warn(c):console.log(c)),a&&a.apply(this,arguments)}},this.createjs=this.createjs||{},function(){"use strict";function Event(a,b,c){this.type=a,this.target=null,this.currentTarget=null,this.eventPhase=0,this.bubbles=!!b,this.cancelable=!!c,this.timeStamp=(new Date).getTime(),this.defaultPrevented=!1,this.propagationStopped=!1,this.immediatePropagationStopped=!1,this.removed=!1}var a=Event.prototype;a.preventDefault=function(){this.defaultPrevented=this.cancelable&&!0},a.stopPropagation=function(){this.propagationStopped=!0},a.stopImmediatePropagation=function(){this.immediatePropagationStopped=this.propagationStopped=!0},a.remove=function(){this.removed=!0},a.clone=function(){return new Event(this.type,this.bubbles,this.cancelable)},a.set=function(a){for(var b in a)this[b]=a[b];return this},a.toString=function(){return"[Event (type="+this.type+")]"},createjs.Event=Event}(),this.createjs=this.createjs||{},function(){"use strict";function EventDispatcher(){this._listeners=null,this._captureListeners=null
在使用three.js时,可以结合tween.js来实现动画效果。tween.js是一个用于创建平滑过渡动画的JavaScript库,可以让你轻松地在three.js场景中添加动画效果。 下面是一个使用three.jstween.js创建动画效果的示例: ```javascript // 创建场景、相机和渲染器 var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建一个立方体 var geometry = new THREE.BoxGeometry(1, 1, 1); var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); var cube = new THREE.Mesh(geometry, material); scene.add(cube); // 创建一个tween动画 var tween = new TWEEN.Tween(cube.position) .to({ x: 2, y: 2, z: 2 }, 2000) // 设置目标位置和动画时间 .easing(TWEEN.Easing.Quadratic.Out) // 设置动画缓动函数 .onUpdate(function () { // 在动画更新时执行的操作 cube.rotation.x += 0.01; cube.rotation.y += 0.01; }) .start(); // 开始动画 // 渲染循环 function animate() { requestAnimationFrame(animate); TWEEN.update(); // 更新tween动画 renderer.render(scene, camera); } animate(); ``` 这个示例中,我们创建了一个立方体,并使用tween.js创建了一个动画,使立方体从初始位置平滑地移动到目标位置。在动画更新时,我们还可以执行其他操作,比如旋转立方体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡定九号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值