制造平滑动画的Tween.js库

原文链接:http://learningthreejs.com/blog/2011/08/17/tweenjs-for-smooth-animation/

本篇介绍的Tween.js是使用tweening engine的非常简单快速的方法。它提供了完全成熟的补间动画。我们开始吧~!

首先来看个demo,这个demo包含了你会用到的基本参数,通过它你也能对tweening有个大概认识。

tweening是什么?

从维基百科来看,tweening就是inbetweening的缩写。我不知道为什么觉得很好笑也许inbetweening不是一个好的起始词。现在我们回到正题,什么是tween?就是指从源点到目标点之间的插值。tween会简单地生成位于区间内的值,问题是那些值放在哪里。这是通过简单的函数来完成的,你可以在这里看到这些函数。

首先引入库文件

下载地址:https://github.com/sole/tween.js

<script type='text/javascript' src='tween.js'></script>

接下来制造一个Tween

我们的例子同demo一样,完成在坐标轴上来回移动一个球体的效果,x坐标轴从0到400,y坐标轴从300到50.

首先我们定义原始的position和target值。position变量的值会被tween.js不断更新直到等于target值。我们创建一个tween对象来帮助我们完成这件事。

var position = { x : 0, y: 300 };
var target = { x : 400, y: 50 };
var tween = new TWEEN.Tween(position).to(target, 2000);

注意到最后的2000了么?它表示tween会持续2s。为了使得3D对象动起来,我们需要告知它每一次的修改。这个通过onUpdate()函数完成。你如果想在tween结束后告知,可以使用onComplete()函数。

tween.onUpdate(function(){
    mesh.position.x = position.x;
    mesh.position.y = position.y;
});

想要调整它么?

让我们做更多的自定义吧~!延迟tween开始的时间500ms怎么样?

tween.delay(500)

很明显你也可以改变ease函数,从 这里选一个吧~!

tween.easing(TWEEN.Easing.Elastic.InOut)

开始运行

现在我们需要告诉库参数已经配置好了,可以开始运行了。

tween.start();

然后我们需要定期更新我们的tweens,这行代码同渲染循环结合的很好。

TWEEN.update();

完成啦~现在你可以疯狂地把tweening用在任何地方了!像我们这里用到的位置,以及颜色,大小,任何地方都可以~!

为动画链接Tweens

我们demo中的球不停地来回移动,是怎么做到的?我么需要链接tweens。它们将会依次执行。chain()提供了简单的方式来制造长效多样的动画。

/ after tweenHead, do tweenBack
tweenHead.chain(tweenBack);
// And after tweenBack, do tweenHead, so it is cycling
tweenBack.chain(tweenHead);

所以tween.js是一个小型库提供了简单的动画方式。你能够在 这里看到demo的源码。

Have fun~!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值