/** * VERSION: 1.02 * DATE: 2010-03-01 * AS3 (AS2 is also available) * UPDATES AND DOCUMENTATION AT: http://www.TweenNano.com **/ package com.greensock { import flash.display.*; import flash.events.*; import flash.utils.*; /** * TweenNano is a super-lightweight (1.6k in AS3 and 2k in AS2) version of TweenLite * and is only recommended for situations where you absolutely cannot afford the extra 3.1k (4.7k total) that the normal * TweenLite engine would cost and your project doesn't require any plugins. Normally, it is much better to use * TweenLite because of the additional flexibility it provides via plugins and its compatibility with TimelineLite and TimelineMax. * TweenNano can do everything TweenLite can do with the following exceptions: * * *
* SPECIAL PROPERTIES: *

* * Any of the following special properties can optionally be passed in through the vars object (the third parameter): * * * * EXAMPLES:

* * Tween the the MovieClip "mc" to an alpha value of 0.5 (50% transparent) and an x-coordinate of 120 * over the course of 1.5 seconds like so:

* * * import com.greensock.TweenNano;

* TweenNano.to(mc, 1.5, {alpha:0.5, x:120}); *


* * To tween the "mc" MovieClip's alpha property to 0.5, its x property to 120 using the Back.easeOut easing * function, delay starting the whole tween by 2 seconds, and then call a function named "onFinishTween" when it * has completed (it will have a duration of 5 seconds) and pass a few parameters to that function (a value of * 5 and a reference to the mc), you'd do so like:

* * * import com.greensock.TweenNano;
* import com.greensock.easing.Back;

* * TweenNano.to(mc, 5, {alpha:0.5, x:120, ease:Back.easeOut, delay:2, onComplete:onFinishTween, onCompleteParams:[5, mc]});
* function onFinishTween(param1:Number, param2:MovieClip):void {
* trace("The tween has finished! param1 = " + param1 + ", and param2 = " + param2);
* } *


* * If you have a MovieClip on the stage that is already in it's end position and you just want to animate it into * place over 5 seconds (drop it into place by changing its y property to 100 pixels higher on the screen and * dropping it from there), you could:

* * * import com.greensock.TweenNano;
* import com.greensock.easing.Elastic;

* * TweenNano.from(mc, 5, {y:"-100", ease:Elastic.easeOut}); *


* * NOTES:

* * * Copyright 2010, GreenSock. All rights reserved. This work is subject to the terms in http://www.greensock.com/terms_of_use.html or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership. * * @author Jack Doyle, jack@greensock.com */ public class TweenNano { /** @private **/ protected static var _time:Number; /** @private **/ protected static var _frame:uint; /** @private Holds references to all our tweens based on their targets (an Array for each target) **/ protected static var _masterList:Dictionary = new Dictionary(false); /** @private A reference to the Shape that we use to drive all our ENTER_FRAME events. **/ protected static var _shape:Shape = new Shape(); /** @private Indicates whether or not the TweenNano class has been initted. **/ protected static var _tnInitted:Boolean; /** @private **/ protected static var _reservedProps:Object = {ease:1, delay:1, useFrames:1, overwrite:1, onComplete:1, onCompleteParams:1, runBackwards:1, immediateRender:1, onUpdate:1, onUpdateParams:1}; /** Duration of the tween in seconds (or in frames if "useFrames" is true). **/ public var duration:Number; /** Stores variables (things like "alpha", "y" or whatever we're tweening, as well as special properties like "onComplete"). **/ public var vars:Object; /** @private Start time in seconds (or frames for frames-based tweens) **/ public var startTime:Number; /** Target object whose properties this tween affects. This can be ANY object, not just a DisplayObject. **/ public var target:Object; /** @private Indicates whether or not the tween is currently active **/ public var active:Boolean; /** @private Flagged for garbage collection **/ public var gc:Boolean; /** Indicates that frames should be used instead of seconds for timing purposes. So if useFrames is true and the tween's duration is 10, it would mean that the tween should take 10 frames to complete, not 10 seconds. **/ public var useFrames:Boolean; /** @private result of _ease(this.time, 0, 1, this.duration). Usually between 0 and 1, but not always (like with Elastic.easeOut). **/ public var ratio:Number = 0; /** @private Easing method to use which determines how the values animate over time. Examples are Elastic.easeOut and Strong.easeIn. Many are found in the fl.motion.easing package or com.greensock.easing. **/ protected var _ease:Function; /** @private Indicates whether or not init() has been called (where all the tween property start/end value information is recorded) **/ protected var _initted:Boolean; /** @private Contains parsed data for each property that's being tweened (property name, start, and change) **/ protected var _propTweens:Array; /** * Constructor * * @param target Target object whose properties this tween affects. This can be ANY object, not just a DisplayObject. * @param duration Duration in seconds (or in frames if "useFrames" is true) * @param vars An object containing the end values of the properties you're tweening, like {x:100, y:50}. It can also contain special properties like "onComplete", "ease", "delay", etc. */ public function TweenNano(target:Object, duration:Number, vars:Object) { if (!_tnInitted) { _time = getTimer() * 0.001; _frame = 0; _shape.addEventListener(Event.ENTER_FRAME, updateAll, false, 0, true); _tnInitted = true; } this.vars = vars; this.duration = duration; this.active = Boolean(duration == 0 && this.vars.delay == 0 && this.vars.immediateRender != false); this.target = target; if (typeof(this.vars.ease) != "function") { _ease = TweenNano.easeOut; } else { _ease = this.vars.ease; } _propTweens = []; this.useFrames = Boolean(vars.useFrames == true); var delay:Number = this.vars.delay || 0; this.startTime = (this.useFrames) ? _frame + delay : _time + delay; var a:Array = _masterList[target]; if (a == null || int(this.vars.overwrite)) { _masterList[target] = [this]; } else { a[a.length] = this; } if (this.vars.immediateRender == true || this.active) { renderTime(0); } } /** * @private * Initializes the property tweens, determining their start values and amount of change. * Also triggers overwriting if necessary and sets the _hasUpdate variable. */ public function init():void { for (var p:String in this.vars) { if (!(p in _reservedProps)) { _propTweens[_propTweens.length] = [p, this.target[p], (typeof(this.vars[p]) == "number") ? this.vars[p] - this.target[p] : Number(this.vars[p])]; //[property, start, change] } } if (this.vars.runBackwards) { var pt:Array; var i:int = _propTweens.length; while (i--) { pt = _propTweens[i]; pt[1] += pt[2]; pt[2] = -pt[2]; } } _initted = true; } /** * Renders the tween at a particular time (or frame number for frames-based tweens) * WITHOUT changing its startTime, meaning if the tween is in progress when you call * renderTime(), it will not adjust the tween's timing to continue from the new time. * The time is based simply on the overall duration. For example, if a tween's duration * is 3, renderTime(1.5) would render it at the halfway finished point. * * @param time time (or frame number for frames-based tweens) to render. */ public function renderTime(time:Number):void { if (!_initted) { init(); } var pt:Array, i:int = _propTweens.length; if (time >= this.duration) { time = this.duration; this.ratio = 1; } else if (time <= 0) { this.ratio = 0; } else { this.ratio = _ease(time, 0, 1, this.duration); } while (i--) { pt = _propTweens[i]; this.target[pt[0]] = pt[1] + (this.ratio * pt[2]); } if (this.vars.onUpdate) { this.vars.onUpdate.apply(null, this.vars.onUpdateParams); } if (time == this.duration) { complete(true); } } /** * Forces the tween to completion. * * @param skipRender To skip rendering the final state of the tween, set skipRender to true. */ public function complete(skipRender:Boolean=false):void { if (!skipRender) { renderTime(this.duration); return; } kill(); if (this.vars.onComplete) { this.vars.onComplete.apply(null, this.vars.onCompleteParams); } } /** Kills the tween, stopping it immediately. **/ public function kill():void { this.gc = true; this.active = false; } //---- STATIC FUNCTIONS ------------------------------------------------------------------------- /** * Static method for creating a TweenNano instance which can be more intuitive for some developers * and shields them from potential garbage collection issues that could arise when assigning a * tween instance to a variable that persists. The following lines of code all produce exactly * the same result:

* * var myTween:TweenNano = new TweenNano(mc, 1, {x:100});
* TweenNano.to(mc, 1, {x:100});
* var myTween:TweenNano = TweenNano.to(mc, 1, {x:100});
* * @param target Target object whose properties this tween affects. This can be ANY object, not just a DisplayObject. * @param duration Duration in seconds (or frames if "useFrames" is true) * @param vars An object containing the end values of the properties you're tweening, like {x:100, y:50}. It can also contain special properties like "onComplete", "ease", "delay", etc. * @return TweenNano instance */ public static function to(target:Object, duration:Number, vars:Object):TweenNano { return new TweenNano(target, duration, vars); } /** * Static method for creating a TweenNano instance that tweens in the opposite direction * compared to a TweenNano.to() tween. In other words, you define the START values in the * vars object instead of the end values, and the tween will use the current values as * the end values. This can be very useful for animating things into place on the stage * because you can build them in their end positions and do some simple TweenNano.from() * calls to animate them into place. NOTE: By default, immediateRender * is true in from() tweens, meaning that they immediately render their starting state * regardless of any delay that is specified. You can override this behavior by passing * immediateRender:false in the vars object so that it will wait to * render until the tween actually begins. To illustrate the default behavior, the following code * will immediately set the alpha of mc to 0 and then wait 2 seconds * before tweening the alpha back to 1 over the course of 1.5 seconds:

* * TweenNano.from(mc, 1.5, {alpha:0, delay:2}); * * @param target Target object whose properties this tween affects. This can be ANY object, not just a DisplayObject. * @param duration Duration in seconds (or frames if "useFrames" is true) * @param vars An object containing the start values of the properties you're tweening like {x:100, y:50}. It can also contain special properties like "onComplete", "ease", "delay", etc. * @return TweenNano instance */ public static function from(target:Object, duration:Number, vars:Object):TweenNano { vars.runBackwards = true; if (!("immediateRender" in vars)) { vars.immediateRender = true; } return new TweenNano(target, duration, vars); } /** * Provides a simple way to call a function after a set amount of time (or frames). You can * optionally pass any number of parameters to the function too. For example:

* * TweenNano.delayedCall(1, myFunction, ["param1", 2]);
* function myFunction(param1:String, param2:Number):void {
* trace("called myFunction and passed params: " + param1 + ", " + param2);
* }
* * @param delay Delay in seconds (or frames if "useFrames" is true) before the function should be called * @param onComplete Function to call * @param onCompleteParams An Array of parameters to pass the function. * @param useFrames If the delay should be measured in frames instead of seconds, set useFrames to true (default is false) * @return TweenNano instance */ public static function delayedCall(delay:Number, onComplete:Function, onCompleteParams:Array=null, useFrames:Boolean=false):TweenNano { return new TweenNano(onComplete, 0, {delay:delay, onComplete:onComplete, onCompleteParams:onCompleteParams, useFrames:useFrames, overwrite:0}); } /** * @private * Updates active tweens and activates those whose startTime is before the _time/_frame. * * @param e ENTER_FRAME Event */ public static function updateAll(e:Event=null):void { _frame++; _time = getTimer() * 0.001; var ml:Dictionary = _masterList, a:Array, tgt:Object, i:int, t:Number, tween:TweenNano; for (tgt in ml) { a = ml[tgt]; i = a.length; while (i--) { tween = a[i]; t = (tween.useFrames) ? _frame : _time; if (tween.active || (!tween.gc && t >= tween.startTime)) { tween.renderTime(t - tween.startTime); } else if (tween.gc) { a.splice(i, 1); } } if (a.length == 0) { delete ml[tgt]; } } } /** * Kills all the tweens of a particular object, optionally forcing them to completion too. * * @param target Object whose tweens should be immediately killed * @param complete Indicates whether or not the tweens should be forced to completion before being killed. */ public static function killTweensOf(target:Object, complete:Boolean=false):void { if (target in _masterList) { if (complete) { var a:Array = _masterList[target]; var i:int = a.length; while (i--) { if (!TweenNano(a[i]).gc) { TweenNano(a[i]).complete(false); } } } delete _masterList[target]; } } /** @private **/ private static function easeOut(t:Number, b:Number, c:Number, d:Number):Number { return -1 * (t /= d) * (t - 2); } } }