2021SC@SDUSC
目录
一.clay.animation.Clip类概述
Clip对象能够对动画的剪辑进行操作,其子对象有Blend1DClip,Blend2DClip,当动画创建后,为动画添加剪辑。
二.clay.animation.Clip类的作用
Clip类能够进行创建一个动画的剪辑,暂停剪辑的播放,恢复剪辑的播放,设置剪辑的缓动效果,设置剪辑的循环次数,设置剪辑的播放时间以及插入剪辑等操作。
三.clay.animation.Clip类源码分析
1.new Clip(opts opt)
该函数为Clip类的构造函数,其参数opts,类型为object(对象),参数opts对象内含有的成员变量如下:
变量名 | 变量类型 | 是否必须 | 构造后默认值 | 描述 |
target | object | 是 | opts.target | 目标对象 |
life | number | 否 | 1000 | 动画的生命时长 |
delay | number | 否 | 0 | 剪辑的时长 |
gap | number | 否 | 0 | 剪辑的间隔 |
playbackRate | number | 否 | 1 | 播放的速度 |
loop | boolean | number | 否 | false | boolean表示是否循环,number表示循环的次数 |
easing | string | function | 否 | 缓动效果 | |
onframe | function | 否 | noop | 动画的帧 |
onfinishing | function | 否 | noop | 剪辑结束 |
onrestart | function | 否 | noop | 剪辑开始 |
其源码如下:
var Clip = function (opts) {
opts = opts || {};
/**
* @type {string}
*/
this.name = opts.name || '';
/**
* @type {Object}
*/
this.target = opts.target;
/**
* @type {number}
*/
this.life = opts.life || 1000;
/**
* @type {number}
*/
this.delay = opts.delay || 0;
/**
* @type {number}
*/
this.gap = opts.gap || 0;
/**
* @type {number}
*/
this.playbackRate = opts.playbackRate || 1;
this._initialized = false;
this._elapsedTime = 0;
this._loop = opts.loop == null ? false : opts.loop;
this.setLoop(this._loop);
if (opts.easing != null) {
this.setEasing(opts.easing);
}
/**
* @type {Function}
*/
this.onframe = opts.onframe || noop;
/**
* @type {Function}
*/
this.onfinish = opts.onfinish || noop;
/**
* @type {Function}
*/
this.onrestart = opts.onrestart || noop;
this._paused = false;
};
从源码中可以看到,Clip共有14个成员变量,在构造函数中接收一个object类型的参数,该参数内共有10个成员变量,其中target为必须的成员变量,当将opts赋给构造函数后,将会创建一个Clip对象,并将opts内的成员变量的值赋给Clip对象,若其中一些成员变量为空,则使用Clip的默认值。
2.pause()
该函数能够执行暂停剪辑的操作,从而暂停剪辑。
无参数,可直接调用。
其源代码为:
/**
* Pause the clip.
*/
pause: function () {
this._paused = true;
}
当执行pause()函数后,将本对象的_paused成员变量设置为true,从而实现剪辑的暂停。
3.resume()
该函数能够执行恢复剪辑的操作,从而恢复剪辑。
无参数,可直接调用。
其源代码为:
/**
* Resume the clip.
*/
resume: function () {
this._paused = false;
}
当执行resume()函数后,将本对象的_paused设置为false,取消剪辑的暂停,从而恢复剪辑。
4.setEasing(easing)
该函数能够设置本对象的缓动操作,实现缓动效果,比如淡入淡出等。
参数为easing,表示所设置的缓动效果,其类型为string或function
其源代码为:
/**
* @param {string|Function} easing
*/
setEasing: function (easing) {
if (typeof(easing) === 'string') {
easing = Easing[easing];
}
this.easing = easing;
}
由源代码可以知道,若参数easing为string类型,则获取内置的Easing数组中键为easing的方法,并赋给本对象的easing成员变量,从而使用内置的缓动效果。若easing为function类型,则将该方法赋给本对象的easing成员变量,当执行特定的行为时,能够实现自己设置的缓动效果,easing中保存的是所写的缓动效果函数。
5.setLoop(loop)
该函数能够设置动画的循环,实现循环的开启和关闭,或者设置循环的次数。
参数为loop,表示是否循环或循环的次数,其类型为number或boolean
其源代码为:
/**
* @param {number|boolean} loop
*/
setLoop: function (loop) {
this._loop = loop;
if (loop) {
if (typeof loop === 'number') {
this._loopRemained = loop;
}
else {
this._loopRemained = Infinity;
}
}
}
有源代码可知,在执行setLoop()函数后,将参数loop的值赋给本对象的_loop成员变量,同时判断loop是否为number类型,若为number类型,则将_loopRemained的值设置为loop,表示该循环执行的次数为loop次,若为boolean类型,表示一直开启循环,将Infinity赋给_loopRemained成员变量,表示循环无限多次,即不断循环。因此,若本对象的loop为false,则不循环,若为true,则_loopRemained为Infinity,无限循环,若为数字,则_loopRemained为loop的值,循环loop次。
6.setTime(time) → {string}
该函数能够在指定的时间开始动画
其参数为time,表示开始的时间,类型为number
其源代码为:
/**
* @param {number} time
* @return {string}
*/
setTime: function (time) {
return this.step(time + this._startTime);
}
有源代码可知,该函数调用step()函数,step()函数如下所述,能够在指定时间播放剪辑,因此,setTime()将time和本对象的_startTime相加,赋给函数step(),表示设置当前播放剪辑的时间。
7.step(time) → {string}
该函数能够在指定时间内添加一段剪辑。
其参数为time,表示剪辑添加的时刻,类型为number
其源代码为:
/**
* @param {number} time
* @return {string}
*/
step: function (time, deltaTime, silent) {
if (!this._initialized) {
this._startTime = time + this.delay;
this._initialized = true;
}
if (this._currentTime != null) {
deltaTime = time - this._currentTime;
}
this._currentTime = time;
if (this._paused) {
return 'paused';
}
if (time < this._startTime) {
return;
}
// PENDIGN Sync ?
this._elapse(time, deltaTime);
var percent = Math.min(this._elapsedTime / this.life, 1);
if (percent < 0) {
return;
}
var schedule;
if (this.easing) {
schedule = this.easing(percent);
}
else {
schedule = percent;
}
if (!silent) {
this.fire('frame', schedule);
}
if (percent === 1) {
if (this._loop && this._loopRemained > 0) {
this._restartInLoop(time);
this._loopRemained--;
return 'restart';
}
else {
// Mark this clip to be deleted
// In the animation.update
this._needsRemove = true;
return 'finish';
}
}
else {
return null;
}
}
由其源代码可知,当执行step()函数后,首先会判断是否初始化,若未初始化,即_initialized为false,则进行初始化,将本对象的_startTime设置为本对象的delay时间加上输入的时间time,并将_initialized设置为true,表示已经初始化。
然后,判断本对象的_currentTime是否不为空,若不为空则将deltaTime的值设置为输入的时间time减去本对象的_currentTime的值。
然后设置当前的时间_currentTime为输入的时间time。如果_paused为真,则返回‘pause’,表示本剪辑已暂停,若为假,则继续判断输入的时间time是否小于剪辑开始时间_starttime,若为真,表示输入不正确,返回,若为假,继续进行后面的操作。
接下来执行elapse()函数,参数为输入的时间time和相减后的时间deltaTime,其代码为:
_elapse: function (time, deltaTime) {
this._elapsedTime += deltaTime * this.playbackRate;
}
该函数能够设置本对象的成员变量_elapsedTime的值增加deltaTime与本对象的成员变量playbackRate的乘积,表示为总剪辑添加一段时间的剪辑,这一段时间的剪辑的播放速度即为playbackRate的值。
因此,向剪辑中添加一段时间后,将_elapsedTime与life的商和1对比,取小的值,并赋给创建的局部变量percent,如果percent小于0则返回,表示当前剪辑的百分比,百分比必须为正数。
然后,创建一个局部变量schedule,如果easing缓动效果存在,将目前剪辑部分(当前百分比)的缓动效果赋值给schedule,否则将百分比赋给schedule,表示当前执行的动画,是缓动效果还是播放动画。
下面,如果silent假,则执行fire()函数,其源代码为:
fire: function (eventType, arg) {
var eventName = 'on' + eventType;
if (this[eventName]) {
this[eventName](this.target, arg);
}
}
该函数将eventType参数前加上on字符串,形成一个局部变量eventName,如果本对象有eventName成员变量,则在eventName成员变量中加入本对象的target以及arg参数,而传入为frame,eventType为onframe,arg为schedule,表示onframe中添加键target和值schedule,实现剪辑中添加相应的行为,表示添加剪辑的帧。
最后,判断percent百分比是否为100%,不是则返回null,是的话继续判断是否循环以及剩余循环次数是否为大于0,是的话执行_restartInLoop函数,其代码为:
_restartInLoop: function (time) {
this._startTime = time + this.gap;
this._elapsedTime = 0;
}
该函数接收time参数,将开始时间设置为time和本对象的gap相加,并把剪辑时间_elapsedTime设置为0,表示重新执行该剪辑。因此,结合而看,如果仍有循环次数,则重新执行剪辑,并将剩余的循环次数减1,返回‘restart’,表示重新开启,若循环次数已经完成,则设置成员变量_needsRemove为true,表示需要执行的剪辑已经完成,并返回‘finish’,表示完成,从而结束该函数。 可见,step()函数可在指定的时间上播放一段剪辑。