tween.js 中文使用指南

tween.js 是一个用于创建平滑动画的库,提供了多种控制动画的方法,如 start, stop, update, chain, repeat, yoyo 等。用户可以通过控制补间对象实现动画的启动、停止和更新,还可以设置缓动函数、回调函数,以及控制补间组。此外,tween.js 支持相对值、数组值的动画,和改变缓动功能以增强动画效果。为了提高性能,推荐在主循环中使用 requestAnimationFrame 调用 TWEEN.update,并尽量避免在 onUpdate 回调中执行复杂操作。" 80146384,7339566,进程间通信与文件系统详解,"['操作系统', '进程通信', '文件管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tween.js 英文使用指南 首先来看个例子: hello,tween.js

补间(动画)(来自 in-between)是一个概念,允许你以平滑的方式更改对象的属性。你只需告诉它哪些属性要更改,当补间结束运行时它们应该具有哪些最终值,以及这需要多长时间,补间引擎将负责计算从起始点到结束点的值。

例如,position对象拥有x和y两个坐标:

var position = { x: 100, y: 0 }

如果你想将x坐标的值从100变成200,你应该这么做:

// 首先为位置创建一个补间(tween)
var tween = new TWEEN.Tween(position);

// 然后告诉 tween 我们想要在1000毫秒内以动画的形式移动 x 的位置
tween.to({ x: 200 }, 1000);

一般来说这样还不够,tween 已经被创建了,但是它还没被激活(使用),你需要这样启动:

// 启动
tween.start();

最后,想要成功的完成这种效果,你需要在主函数中调用TWEEN.update,如下使用:

animate();

function animate() {
    requestAnimationFrame(animate);
    // [...]
    TWEEN.update();
    // [...]
}

这样在更新每帧的时候都会运行补间动画;经过 1秒后 (1000 毫秒) position.x将会变成 200。

除非你在控制台中打印出 x 的值,不然你看不到它的变化。你可能想要使用 onUpdate 回调:

tween.onUpdate(function(object) {
    console.log(object.x);
});
tips:你可能在这里获取不到 object.x ,具体的见我提的这个 issue

这个函数将会在动画每次更新的时候被调用;这种情况发生的频率取决于很多因素 - 例如,计算机或设备的速度有多快(以及如何繁忙)。

到目前为止,我们只使用补间动画向控制台输出值,但是您可以将它与 three.js 对象结合:

var tween = new TWEEN.Tween(cube.position)
        .to({ x: 100, y: 100, z: 100 }, 10000)
        .start();

animate();

function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();

    threeRenderer.render(scene, camera);
}

在这种情况下,因为three.js渲染器将在渲染之前查看对象的位置,所以不需要使用明确的onUpdate回调。

你可能也注意到了一些不同的地方:tween.js 可以链式调用! 每个tween函数都会返回tween实例,所以你可以重写下面的代码:

var tween = new TWEEN.Tween(position);
tween.to({ x: 200 }, 1000);
tween.start();

改成这样:

var tween = new TWEEN.Tween(position)
    .to({ x: 200 }, 1000)
    .start();

在将会看到很多例子,所以熟悉它是很好的!比如 04-simplest 这个例子。

tween.js的动画

Tween.js 不会自行运行。你需要显式的调用 update 方法来告诉它何时运行。推荐的方法是在主动画循环中执行这个操作。使用 requestAnimationFrame 调用此循环以获得最佳的图形性能。

比如之前这个例子:

animate();

function animate() {
    requestAnimationFrame(animate)
/*! * @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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值