📄 animation.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*/dojo.provide("dojo.lfx.Animation");dojo.require("dojo.lang.func");/* Animation package based on Dan Pupius' work: http://pupius.co.uk/js/Toolkit.Drawing.js*/dojo.lfx.Line = function(/*int*/ start, /*int*/ end){ // summary: dojo.lfx.Line is the object used to generate values // from a start value to an end value this.start = start; this.end = end; if(dojo.lang.isArray(start)){ /* start: Array end: Array pId: a */ var diff = []; dojo.lang.forEach(this.start, function(s,i){ diff[i] = this.end[i] - s; }, this); this.getValue = function(/*float*/ n){ var res = []; dojo.lang.forEach(this.start, function(s, i){ res[i] = (diff[i] * n) + s; }, this); return res; // Array } }else{ var diff = end - start; this.getValue = function(/*float*/ n){ // summary: returns the point on the line // n: a floating point number greater than 0 and less than 1 return (diff * n) + this.start; // Decimal } }}dojo.lfx.easeDefault = function(/*Decimal?*/ n){ // summary: Returns the point for point n on a sin wave. if(dojo.render.html.khtml){ // the cool kids are obviously not using konqueror... // found a very wierd bug in floats constants, 1.5 evals as 1 // seems somebody mixed up ints and floats in 3.5.4 ?? // FIXME: investigate more and post a KDE bug (Fredrik) return (parseFloat("0.5")+((Math.sin( (n+parseFloat("1.5")) * Math.PI))/2)); }else{ return (0.5+((Math.sin( (n+1.5) * Math.PI))/2)); }}dojo.lfx.easeIn = function(/*Decimal?*/ n){ // summary: returns the point on an easing curve // n: a floating point number greater than 0 and less than 1 return Math.pow(n, 3);}dojo.lfx.easeOut = function(/*Decimal?*/ n){ // summary: returns the point on the line // n: a floating point number greater than 0 and less than 1 return ( 1 - Math.pow(1 - n, 3) );}dojo.lfx.easeInOut = function(/*Decimal?*/ n){ // summary: returns the point on the line // n: a floating point number greater than 0 and less than 1 return ( (3 * Math.pow(n, 2)) - (2 * Math.pow(n, 3)) );}dojo.lfx.IAnimation = function(){ // summary: dojo.lfx.IAnimation is an interface that implements // commonly used functions of animation objects}dojo.lang.extend(dojo.lfx.IAnimation, { // public properties curve: null, duration: 1000, easing: null, repeatCount: 0, rate: 25, // events handler: null, beforeBegin: null, onBegin: null, onAnimate: null, onEnd: null, onPlay: null, onPause: null, onStop: null, // public methods play: null, pause: null, stop: null, connect: function(/*Event*/ evt, /*Object*/ scope, /*Function*/ newFunc){ // summary: Convenience function. Quickly connect to an event // of this object and save the old functions connected to it. // evt: The name of the event to connect to. // scope: the scope in which to run newFunc. // newFunc: the function to run when evt is fired. if(!newFunc){ /* scope: Function newFunc: null pId: f */ newFunc = scope; scope = this; } newFunc = dojo.lang.hitch(scope, newFunc); var oldFunc = this[evt]||function(){}; this[evt] = function(){ var ret = oldFunc.apply(this, arguments); newFunc.apply(this, arguments); return ret; } return this; // dojo.lfx.IAnimation }, fire: function(/*Event*/ evt, /*Array*/ args){ // summary: Convenience function. Fire event "evt" and pass it // the arguments specified in "args". // evt: The event to fire. // args: The arguments to pass to the event. if(this[evt]){ this[evt].apply(this, (args||[])); } return this; // dojo.lfx.IAnimation }, repeat: function(/*int*/ count){ // summary: Set the repeat count of this object. // count: How many times to repeat the animation. this.repeatCount = count; return this; // dojo.lfx.IAnimation }, // private properties _active: false, _paused: false});dojo.lfx.Animation = function( /*Object*/ handlers, /*int*/ duration, /*dojo.lfx.Line*/ curve, /*function*/ easing, /*int*/ repeatCount, /*int*/ rate){ // summary // a generic animation object that fires callbacks into it's handlers // object at various states // handlers: { handler: Function?, onstart: Function?, onstop: Function?, onanimate: Function? } dojo.lfx.IAnimation.call(this); if(dojo.lang.isNumber(handlers)||(!handlers && duration.getValue)){ // no handlers argument: rate = repeatCount; repeatCount = easing; easing = curve; curve = duration; duration = handlers; handlers = null; }else if(handlers.getValue||dojo.lang.isArray(handlers)){ // no handlers or duration: rate = easing; repeatCount = curve; easing = duration; curve = handlers; duration = null; handlers = null; } if(dojo.lang.isArray(curve)){ /* curve: Array pId: a */ this.curve = new dojo.lfx.Line(curve[0], curve[1]); }else{ this.curve = curve; } if(duration != null && duration > 0){ this.duration = duration; } if(repeatCount){ this.repeatCount = repeatCount; } if(rate){ this.rate = rate; } if(handlers){ dojo.lang.forEach([ "handler", "beforeBegin", "onBegin", "onEnd", "onPlay", "onStop", "onAnimate" ], function(item){ if(handlers[item]){ this.connect(item, handlers[item]); } }, this); } if(easing && dojo.lang.isFunction(easing)){ this.easing=easing; }}dojo.inherits(dojo.lfx.Animation, dojo.lfx.IAnimation);dojo.lang.extend(dojo.lfx.Animation, { // "private" properties _startTime: null, _endTime: null, _timer: null, _percent: 0, _startRepeatCount: 0, // public methods play: function(/*int?*/ delay, /*bool?*/ gotoStart){ // summary: Start the animation. // delay: How many milliseconds to delay before starting. // gotoStart: If true, starts the animation from the beginning; otherwise, // starts it from its current position. if(gotoStart){ clearTimeout(this._timer); this._active = false; this._paused = false; this._percent = 0; }else if(this._active && !this._paused){ return this; // dojo.lfx.Animation } this.fire("handler", ["beforeBegin"]); this.fire("beforeBegin"); if(delay > 0){ setTimeout(dojo.lang.hitch(this, function(){ this.play(null, gotoStart); }), delay); return this; // dojo.lfx.Animation } this._startTime = new Date().valueOf(); if(this._paused){ this._startTime -= (this.duration * this._percent / 100); } this._endTime = this._startTime + this.duration; this._active = true; this._paused = false; var step = this._percent / 100; var value = this.curve.getValue(step); if(this._percent == 0 ){ if(!this._startRepeatCount){ this._startRepeatCount = this.repeatCount; } this.fire("handler", ["begin", value]); this.fire("onBegin", [value]); } this.fire("handler", ["play", value]); this.fire("onPlay", [value]); this._cycle(); return this; // dojo.lfx.Animation }, pause: function(){ // summary: Pauses a running animation. clearTimeout(this._timer); if(!this._active){ return this; /*dojo.lfx.Animation*/} this._paused = true; var value = this.curve.getValue(this._percent / 100); this.fire("handler", ["pause", value]); this.fire("onPause", [value]); return this; // dojo.lfx.Animation }, gotoPercent: function(/*Decimal*/ pct, /*bool?*/ andPlay){ // summary: Sets the progress of the animation. // pct: A percentage in decimal notation (between and including 0.0 and 1.0). // andPlay: If true, play the animation after setting the progress. clearTimeout(this._timer); this._active = true; this._paused = true; this._percent = pct; if(andPlay){ this.play(); } return this; // dojo.lfx.Animation }, stop: function(/*bool?*/ gotoEnd){ // summary: Stops a running animation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -