?? silverna-animations.js
字號:
//這是一個for Silverna的動畫庫,它定義了動畫類的Behaviors
//Animation繼承了Behavior的原型,但是它不需要knobs
core.web.widgets.adorners.animations ={
Animation : null,
MoveAnimation : null,
SlideAnimation : null,
FadeAnimation : null
}
with(core.web.widgets.adorners.animations)
{
Animation = function()
{
core.web.widgets.adorners.behaviors.Behavior.call(this);
this.provider = null;
this.paused = false;
this._frames = 0; //_frames表示楨數(shù), 0表示未開始
this._args = {};
}
Animation.prototype = new core.web.widgets.adorners.behaviors.Behavior();
//freq表示頻率,也就是多少時間刷新一楨,默認(rèn)為100,即0.01秒刷新一楨
Animation.prototype.freq = "100";
//為動畫設(shè)置運算函數(shù),運算函數(shù)用來改變target的某些屬性
Animation.prototype.setProvider = function(provider)
{
var $pointer = this;
this.provider = function(args){
var ret = $pointer._frames && !$pointer.paused;
if(ret){
ret = provider.call($pointer,args);
if(!ret)
$pointer.stop();
else{
$pointer._frames++;
$pointer.dispatchEvent("frameplay", args);
}
}
return ret;
}
}
Animation.prototype.stop = function()
{
var $pointer = this;
$pointer._frames = 0;
$pointer.dispatchEvent("stop", this._args);
}
//actor是一個閉包,用來影響target的實際屬性, args是實際的控制參數(shù)
Animation.prototype.setActor = function(actor, args)
{
this.actor = actor;
this._args = args;
}
//設(shè)置Animation的頻率
Animation.prototype.setFrequency = function(freq)
{
this.freq = freq;
}
Animation.prototype.init = function(provider, actor, args, freq)
{
this.setProvider(provider);
this.setActor(actor, args);
this.setFrequency(freq);
}
Animation.prototype.play = function(actor, args)
{
if(this._frames) return;
this.reset();
this.paused = false;
this._frames = 1;
if(actor != null && args != null)
this.setActor(actor, args);
var delay = Math.floor(1000 / this.freq);
this._args.target = this.panel.el;
this.actor.listen(this.provider, delay, this._args);
this.dispatchEvent("play",this._args);
}
Animation.prototype.pause = function()
{
this.paused = true;
this.dispatchEvent("pause", this._args);
}
Animation.prototype.resume = function()
{
if(!this._frames) return false;
var delay = Math.floor(1000 / this.freq);
if(this.paused)
{
this._args.target = this.panel.el;
this.actor.listen(this.provider, delay, this._args);
this.dispatchEvent("resume",this._args);
this.paused = false;
}
}
Animation.prototype.reset = function()
{
if(this._frames) this.stop();
this.dispatchEvent("reset", this._args);
}
//這是一個和位置有關(guān)的抽象類,它包含MoveAnimation和SildeAnimation兩個子類
PositionAnimation = function()
{
Animation.call(this);
var $pointer = this;
this.coordinateType = "X"; //坐標(biāo)默認(rèn)以X軸為參照,可選值X、Y、XY
this.setProvider(function(evt){
if($pointer.coordinateType == "X"){
var _from = $pointer._from[0];
var _to = $pointer._to[0];
var _r = false;
if(_from > _to){
_r = true;
_from = $pointer._to[0];
_to = $pointer._from[0];
}
evt.y = Math.round($pointer.path(evt.x,evt.y));
_r ? evt.x -- : evt.x ++;
return evt.x >= _from && evt.x < _to;
}
else if ($pointer.coordinateType == "Y"){
var _from = $pointer._from[1];
var _to = $pointer._to[1];
var _r = false;
if(_from > _to){
_r = true;
_from = $pointer._to[1];
_to = $pointer._from[1];
}
evt.x = Math.round($pointer.path(evt.y,evt.x));
_r ? evt.y-- : evt.y++;
return evt.y >= _from && evt.y < _to;
}
else{
var _fromX = $pointer._from[0];
var _toX = $pointer._to[0];
var _fromY = $pointer._from[1];
var _toY = $pointer._to[1];
if(_fromX > _toX)
_fromX = $pointer._to[0], _toX = $pointer._from[0];
if(_fromY > _toY)
_fromY = $pointer._to[1], _toY = $pointer._from[1];
$pointer.path(evt);
return evt.x >= _fromX && evt.x < _toX &&
evt.y >= _fromY && evt.y < _toY;
}
});
}
PositionAnimation.prototype = new Animation();
//setPath用來設(shè)置PositionAnimation的軌跡
//path是一個閉包,用來描述軌跡
//一般情況下它是一個2D的曲線模型
//根據(jù)不同的規(guī)則x、y或者xy作為它的參數(shù)進行調(diào)用,調(diào)用的結(jié)果影響到Privider
PositionAnimation.prototype.setPath = function(path)
{
this.path = path;
}
//setRange用來設(shè)置PositionAnimation的坐標(biāo)范圍,例如[0,0],[100,100],表示一個左上0,0,右下[100,100]的區(qū)域
//允許第一個參數(shù)的坐標(biāo)值大于第二個參數(shù),此時物件將反向運動
PositionAnimation.prototype.setRange = function(_from, _to)
{
if(_from != null) this._from = _from;
if(_to != null) this._to = _to;
}
PositionAnimation.prototype.reverse = function()
{
var temp = this._from;
this._from = this._to;
this._to = temp;
}
//這是一個用來實現(xiàn)移動效果的Animation類型,它能夠影響的屬性是panel的位置
MoveAnimation = function()
{
this._from = [0,0];
this._to = [clientRect().width, clientRect().height];
this.path = function(x, y){return y};
PositionAnimation.call(this);
this.reset = function(){
this.setActor(function(evt){
$html(evt.target).setAbsPosition(evt.x, evt.y)
},{x:this._from[0], y:this._from[1]});
if(this._frames) this.stop();
this.dispatchEvent("reset", this._args);
}
}
MoveAnimation.prototype = new PositionAnimation();
//這是一個用來實現(xiàn)Silde效果的Animation類型,它能夠影響的屬性是panel的大小
SlideAnimation = function()
{
this._from = [0,0];
this._to = [clientRect().width, clientRect().height];
this.path = function(x, y){return y};
PositionAnimation.call(this);
this.reset = function(){
this.setActor(function(evt){
$html(evt.target).resize(evt.x, evt.y)
},{x:this._from[0], y:this._from[1]});
if(this._frames) this.stop();
this.dispatchEvent("reset", this._args);
}
this.setPanel = function(panel)
{
this.panel = panel;
var _s = panel.getSize();
this._from = [_s.width, _s.height];
}
}
SlideAnimation.prototype = new PositionAnimation();
//這是一個和透明度有關(guān)的Animation類型,它能夠影響的屬性是opacity
FadeAnimation = function()
{
Animation.call(this);
var $pointer = this;
this._from = 0;
this._to = 100;
this.setProvider(function(evt){
var _from = $pointer._from;
var _to = $pointer._to;
var _r = 1;
if(_from > _to){
_r = -1;
_from = $pointer._to;
_to = $pointer._from;
}
evt.time = _r * $pointer._frames * $pointer.speed;
var ret = $pointer.painter(evt);
return ret >= _from && ret < _to;
});
this.painter = function(evt)
{
evt.opacity = Math.round($pointer._from + evt.time);
return evt.opacity;
}
this.reset = function(){
this.setActor(function(evt){
$html(evt.target).el.style.filter = "alpha(opacity="+evt.opacity+")";
$html(evt.target).el.style.opacity = evt.opacity;
},{opacity:$pointer._from});
if(this._frames) this.stop();
this.dispatchEvent("reset", this._args);
}
}
FadeAnimation.prototype = new Animation();
//同樣是設(shè)置范圍,這個類型的_from和_to是表示透明度的數(shù)值
FadeAnimation.prototype.setRange = function(_from, _to)
{
if(_from != null) this._from = _from;
if(_to != null) this._to = _to;
}
//Painter是一個閉包,它是一個參量關(guān)于時間的函數(shù)
FadeAnimation.prototype.setPainter = function(painter)
{
this.path = painter;
}
FadeAnimation.prototype.speed = 1;
FadeAnimation.prototype.fadeIn = function()
{
this.setRange(0,100);
this.play();
}
FadeAnimation.prototype.fadeOut = function()
{
this.setRange(100,0);
this.play();
}
//閃動,freq,每秒的頻率
FadeAnimation.prototype.shine = function(freq)
{
this.speed = this.speed || freq * 2;
this.onstop = function(){
this.reverse();
this.play();
}
this.play();
}
FadeAnimation.prototype.reverse = function()
{
var temp = this._from;
this._from = this._to;
this._to = temp;
}
FadeAnimation.prototype.abort = function()
{
this.onstop = null;
this.reset();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -