var
Animation
=
Class.create();
Animation.prototype
=
{
/*
------------------------------------------------------------------------
| 用途:
| 构造函数
|
| 参数:
| element 将要实现动画效果的元素
| fps 每秒播放帧数
------------------------------------------------------------------------
*/
initialize:
function
(element, fps)
{
this
.element
=
$(element);
this
.interval
=
Math.round(
1000
/
fps);
this
.isPlaying
=
false
;
this
.currentFrame
=
1
;
//
创建一个用于存储中间状态的临时对象
this
.temp
=
{ }
;
}
,
/*
------------------------------------------------------------------------
| 用途:
| 子类覆盖该方法,实现自定义的动画补间
------------------------------------------------------------------------
*/
_createTweens:
function
(original, transformed, frames)
{ }
,
/*
------------------------------------------------------------------------
| 用途:
| 创建动画补间
|
| 参数:
| original 开始状态
| transformed 结束状态
| frames 动画帧数
------------------------------------------------------------------------
*/
createTweens:
function
(original, transformed, frames)
{
if
(
this
.isPlaying)
{
this
.stop();
}
this
._createTweens(original, transformed, frames);
this
.original
=
original;
this
.transformed
=
transformed;
this
.frames
=
frames;
//
将开始状态拷贝到临时对象
Object.extend(
this
.temp, original);
}
,
/*
------------------------------------------------------------------------
| 用途:
| 判断临时对象状态是否超出结束状态
|
| 参数:
| prop 状态属性名称
------------------------------------------------------------------------
*/
_isOverstep:
function
(prop)
{
if
(
this
.original[prop]
<
this
.transformed[prop])
{
return
this
.temp[prop]
>
this
.transformed[prop];
}
return
this
.temp[prop]
<
this
.transformed[prop];
}
,
_prepare:
function
()
{ }
,
_draw:
function
(frame)
{ }
,
_drawFrame:
function
()
{
if
(
this
.isPlaying)
{
if
(
this
.currentFrame
<
this
.frames)
{
this
._prepare();
this
._draw(
this
.temp);
this
.currentFrame
++
;
}
else
{
//
最后一帧绘制结束状态
this
._draw(
this
.transformed);
this
.stop();
}
}
}
,
_play:
function
()
{ }
,
play:
function
()
{
if
(
!
this
.isPlaying)
{
this
._play();
this
.isPlaying
=
true
;
this
.timer
=
setInterval(
this
._drawFrame.bind(
this
),
this
.interval);
}
}
,
_stop:
function
()
{ }
,
stop:
function
()
{
if
(
this
.isPlaying)
{
this
._stop();
//
回到开始状态
this
.isPlaying
=
false
;
this
.currentFrame
=
1
;
Object.extend(
this
.temp,
this
.original);
clearInterval(
this
.timer);
}
}
,
_pause:
function
()
{ }
,
pause:
function
()
{
if
(
this
.isPlaying)
{
this
._pause();
this
.isPlaying
=
false
;
clearInterval(
this
.timer);
}
}
}
var
ShapeAnimation
=
Class.create();
ShapeAnimation.prototype
=
Object.extend(
new
Animation(),
{
/*
------------------------------------------------------------------------
| 用途:
| 覆盖父类的空白实现,计算每帧的变化量
------------------------------------------------------------------------
*/
_createTweens:
function
(original, transformed, frames)
{
this
.xSpan
=
Math.round((transformed.x
-
original.x)
/
frames);
this
.ySpan
=
Math.round((transformed.y
-
original.y)
/
frames);
this
.wSpan
=
Math.round((transformed.w
-
original.w)
/
frames);
this
.hSpan
=
Math.round((transformed.h
-
original.h)
/
frames);
}
,
/*
------------------------------------------------------------------------
| 用途:
| 覆盖父类的空白实现,计算当前的状态。如果超出结束状态,保持结束状态不变
------------------------------------------------------------------------
*/
_prepare:
function
()
{
this
.temp.x
=
this
._isOverstep('x')
?
this
.transformed.x :
this
.temp.x
+
this
.xSpan;
this
.temp.y
=
this
._isOverstep('r')
?
this
.transformed.y :
this
.temp.y
+
this
.ySpan;
this
.temp.w
=
this
._isOverstep('w')
?
this
.transformed.w :
this
.temp.w
+
评论