<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tween.js &#8211; 萧邦的博客</title>
	<atom:link href="https://www.zlhlab.top/tag/tween-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.zlhlab.top</link>
	<description>If not me who, If not now when</description>
	<lastBuildDate>Tue, 07 Nov 2017 10:21:08 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.6</generator>
	<item>
		<title>tween.js简易教程</title>
		<link>https://www.zlhlab.top/tween-js%e7%ae%80%e6%98%93%e6%95%99%e7%a8%8b/</link>
		
		<dc:creator><![CDATA[萧邦]]></dc:creator>
		<pubDate>Mon, 16 Oct 2017 13:29:08 +0000</pubDate>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[tween.js]]></category>
		<category><![CDATA[动画]]></category>
		<guid isPermaLink="false">https://www.zlhlab.top/?p=377</guid>

					<description><![CDATA[tween.js是一款可生成平滑动画效果的js动画库。]]></description>
										<content:encoded><![CDATA[<p><span style="font-family: helvetica, arial, sans-serif;">tween.js是一款可生成平滑动画效果的js动画库。相关的动画库插件还有：<a href="http://www.htmleaf.com/jQuery/Image-Effects/201501061112.html" target="_blank">snabbt.js 强大的jQuery动画库插件</a>和<a href="http://www.htmleaf.com/jQuery/Layout-Interface/201501081124.html" target="_blank">Tweene-超级强大的jQuery动画代理插件</a>。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">tween.js允许你以平滑的方式修改元素的属性值。你只需要告诉tween你想修改什么值，以及动画结束时它的最终值是什么，动画花费多少时间等信息，tween引擎就可以计算从开始动画点到结束动画点之间值，来产生平滑的动画效果。例如，假设你有一个对象<code>position</code>，它的坐标为 <code>x</code> 和 <code>y</code>：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">var position = { x: 100, y: 0 }
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">如果你想改变 <code>x</code> 的值从100到200，你只需要这样做：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">// Create a tween for position first
var tween = new TWEEN.Tween(position);
// Then tell the tween we want to animate the x property over 1000 milliseconds
tween.to({ x: 200 }, 1000);
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">到这里只是创建了tween对象，你需要激活它，让它开始动画：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">// And set it to start
tween.start();
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">最后为了平滑动画效果，你需要在同一个循环动画中调用<code>TWEEN.update</code>方法。代码如下：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">animate();
function animate() {
 requestAnimationFrame(animate);
 // […]
 TWEEN.update();
 // […]
}
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">这个动作将会更新所有被激活的tweens，在1秒钟（例如1000ms）<code>position.x</code> 将变为200。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">你也可以使用<code>onUpdate</code>回调函数将结果打印到控制台上。</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tween.onUpdate(function() {
 console.log(this.x);
});
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">这个函数在每次tweens被更新时都被调用。它的出现频次依赖于很多因素-例如：依赖于你的电脑或设备的运行速度。</span></p>
<h3><span style="font-family: helvetica, arial, sans-serif;">tween.js动画</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;">Tween.js本身不会运行，你需要通过<code>update</code>方法明确的告诉它什么时候开始运行。推荐在动画主循环中使用该方法。你可以通过调用<code>requestAnimationFrame</code>方法来获得良好的图像性能。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">来看下面的例子：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">animate();
function animate() {
 requestAnimationFrame(animate);
 // […]
 TWEEN.update();
 // […]
}
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">这里使用无参数调用方式，<code>update</code>方法将明确当前时间，以便于获取上一次动画的执行时间。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">你也可以为<code>update</code>方法明确一个时间：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">TWEEN.update(100);
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">上面语句的意思是说：<code>update</code>的时间=100毫秒。你可以使用这种方法来明确代码中所有随时间变化的函数。例如，动画已经开始，你想所有动画都同步进行，你的<code>animate</code>代码一改像这样：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">var currentTime = player.currentTime;
TWEEN.update(currentTime);
</code></pre>
<h3><span style="font-family: helvetica, arial, sans-serif;">控制tween动画</span></h3>
<h5><span style="font-family: helvetica, arial, sans-serif;">start和stop</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;"><code>Tween.start</code>和<code>Tween.stop</code>分别用于控制tween动画的开始和结束。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">对于已经结束和没有开始的动画，<code>Tween.stop</code>方法不起作用。<code>Tween.start</code>方法同样接收一个时间参数。如果你使用了该参数，tween动画将在延时该时间数后才开始动画。否则它将立刻开始动画。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">update</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">可以通过<code>TWEEN.update</code>方法来执行动画的更新。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">chain</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">如果你想制作多个多行，例如：一个动画在另一个动画结束后开始。可以通过<code>chain</code>方法来使实现。如下的代码，tweenB 在 tweenA 之后开始动画:</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tweenA.chain(tweenB);
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">可以像下面这样制作一个无限循环的动画：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tweenA.chain(tweenB);
tweenB.chain(tweenA);
</code></pre>
<h5><span style="font-family: helvetica, arial, sans-serif;">repeat</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">如果你想制作循环动画可以使用<code>chain</code>来实现，但是更好的方法是使用<code>repeat</code>方法。它接收一个用于描述你想循环多少次的参数：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tween.repeat(10); // repeats 10 times and stops
tween.repeat(Infinity); // repeats forever

</code></pre>
<h5><span style="font-family: helvetica, arial, sans-serif;">yoyo</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">这个函数只在你使用<code>repeat</code>方法是起作用。当它被激活时，tween 的效果类似yoyo效果。该效果是动画会在开始或结束处向反方向反弹。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">delay</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;"><code>delay</code>方法用于控制动画之间的延时。</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tween.delay(1000);
tween.start();
</code></pre>
<h3><span style="font-family: helvetica, arial, sans-serif;">全局方法</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;">以下的方法定义在 TWEEN 的全局对象中，其中大多数方法你都用不上，除了<code>update</code>方法：</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">TWEEN.update(time)</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">该方法用于所有被激活的tweens，如果<code>time</code>没有被指定，将使用当前时间。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">TWEEN.getAll 和 TWEEN.removeAll</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">这两个方法用于胡获取被激活的tweens数组的一个引用，或从数组中删除所有tweens。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">TWEEN.add(tween) 和 TWEEN.remove(tween)</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">用于向被激活的tweens中添加一个tween，或移除一个tween。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">以上方法通常只是在内部使用，一般情况下你了解即可。</span></p>
<h3><span style="font-family: helvetica, arial, sans-serif;">可用的easing函数：TWEEN.Easing</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;">tween.js提供了一些可用的easing函数。可用函数有：Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back 和 Bounce。easing 类型分为: In, Out 和 InOut.</span></p>
<h3><span style="font-family: helvetica, arial, sans-serif;">使用自定义的Easing函数</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;">你不但可以使用tween.js提供的easing函数，还可以自定义easing函数。但必须遵守下面的规则：</span></p>
<ul>
<li><span style="font-family: helvetica, arial, sans-serif;">它必须接收一个参数。</span></li>
<li><span style="font-family: helvetica, arial, sans-serif;">它必须基于输入参数返回一个值。</span></li>
</ul>
<p><span style="font-family: helvetica, arial, sans-serif;">easing函数仅在每个tween每次被更新时调用，而不管有多少属性被改变。结果随后会被用于初始值：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">easedElapsed = easing(k);
for each property:
 newPropertyValue = initialPropertyValue + propertyDelta * easedElapsed;
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">下面是一个使用Math.floor来做easing效果的例子：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">function tenStepEasing(k) {
 return Math.floor(k * 10) / 10;
}
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">你可以在tween 这样使用它：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">tween.easing(tenStepEasing);
</code></pre>
<h3><span style="font-family: helvetica, arial, sans-serif;">回调函数</span></h3>
<p><span style="font-family: helvetica, arial, sans-serif;">另外一个有用的特性是你可以在每次tween循环周期的指定时间点调用自定义的函数。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">例如：假设你想使一些不能直接修改参数的对象执行动画，要访问该对象的参数只能通过setter方法，你可以通过<code>update</code>方法的回调函数来设置新的setter值。</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">var trickyObjTween = new TWEEN.Tween({
 propertyA: trickyObj.getPropertyA(),
 propertyB: trickyObj.getPropertyB()
})
 .to({ propertyA: 100, propertyB: 200 })
 .onUpdate(function() {
 this.setA( this.propertyA );
 this.setB( this.propertyB );
 });
</code></pre>
<p><span style="font-family: helvetica, arial, sans-serif;">或者如果你想确认tween动画开始后某个对象指定状态下的某个值，你可以通过<code>start</code>回调来获取它：</span></p>
<pre class="line-numbers prism-highlight" data-start="1"><code class="language-javascript">var tween = new TWEEN.Tween(obj)
 .to({ x: 100 })
 .onStart(function() {
 this.x = 0;
 });
</code></pre>
<h5><span style="font-family: helvetica, arial, sans-serif;">onStart</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">tween开始动画前的回调函数。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">onStop</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">tween结束动画后的回调函数。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">onUpdate</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">在tween每次被更新后执行。</span></p>
<h5><span style="font-family: helvetica, arial, sans-serif;">onComplete</span></h5>
<p><span style="font-family: helvetica, arial, sans-serif;">在tween动画全部结束后执行。</span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">更多关于tween的资料请参考：<a href="https://github.com/sole/tween.js/blob/master/docs/user_guide.md" target="_blank">https://github.com/sole/tween.js/blob/master/docs/user_guide.md</a></span></p>
<p><span style="font-family: helvetica, arial, sans-serif;">tween的相关资料：<a href="http://learningthreejs.com/blog/2011/08/17/tweenjs-for-smooth-animation/" target="_blank">Tween.js for Smooth Animation</a></span></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
