GreenSock


TweenFilterLite (AS3 Version) – Easily Tween Filters & Image Effects

Posted in Tweening by jack on the October 18th, 2007

IMPORTANT: TweenFilterLite has been rendered obsolete by the new plugin architecture in TweenLite and TweenMax! Read all about the exciting update and watch a video demonstration at the official announcement page.

USAGE

Every call to TweenFilterLite.to() or TweenFilterLite.from() returns a TweenFilterLite instance which you can optionally keep track of in order to have more control. Here are a few examples of how to create instances:

Actionscript:
  1. import gs.*;
  2. TweenFilterLite.to(mc, 1, {x:200}); //automatically garbage collects
  3. var myTween:TweenFilterLite = TweenFilterLite.to(mc, 1, {x:200}); //stores a reference to the TweenFilterLite instance, but you're responsible for making sure it becomes eligible for garbage collection when you're finished with it (you can simply set your variable to null, or if it is still in progress, call TweenFilterLite.removeTween(myTween) first)
  4. var myTween:TweenFilterLite = new TweenFilterLite(mc, 1, {x:200}); //identical to the previous line - it just looks more object-oriented.

Each TweenFilterLite instance has the following properties:

  • target : Object (changing target after the tween begins has no effect on the tween)
  • duration : Number
  • timeScale : Number - Multiplier for controlling the speed of the tween. 0.5 = half speed, 1 = normal speed, 2 = double speed, etc. (This value is combined with the globalTimeScale to give you lots of control)

INSTANCE METHODS

TweenFilterLite(target:Object, duration:Number, variables:Object):TweenFilterLite
  • Description: Constructor. Property values are tweened from whatever they are at the time the tween begins to whatever you define in the variables parameter. (unless you use "runBackwards:true" which is the same as using TweenFilterLite.from())
  • Parameters:
    1. target : Object - Target object whose properties we're tweening
    2. duration : Number - Duration (in seconds) of the tween
    3. variables : Object - An object containing the end values of all the properties you'd like to have tweened (or if you're using the TweenFilterLite.from() method, these variables would define the BEGINNING values). Pass in one object for each filter (named appropriately, like blurFilter, glowFilter, colorMatrixFilter, etc.) Filter objects can contain any number of properties specific to that filter, like blurX, blurY, contrast, color, distance, colorize, brightness, highlightAlpha, etc.
      Special Properties:

      • delay : Number - The number of seconds you'd like to delay before the tween begins. This is very useful when sequencing tweens
      • ease : Function - You can specify a function to use for the easing with this variable. For example, gs.easing.Elastic.easeOut. The Default is Regular.easeOut. To build a custom ease, check out the GreenSock CustomEase Builder tool.
      • easeParams : Array - An Array of extra parameter values to feed the easing equation. This can be useful when you use an equation like Elastic and want to control extra parameters like the amplitude and period. Most easing equations, however, don't require extra parameters so you won't need to pass in any easeParams.
      • autoAlpha : Number - Use it instead of the alpha property to gain the additional feature of toggling the visible property to false when alpha reaches 0. It will also toggle visible to true before the tween starts if the value of autoAlpha is greater than zero.
      • visible : Boolean - To set a DisplayObject's "visible" property at the end of the tween, use this special property.
      • volume : Number - Tweens the volume of any object with a soundTransform property (like MovieClips, NetStreams, etc.)
      • tint : Number - To change a DisplayObject's tint, set this to the hex value of the color you'd like the DisplayObject to end up at (or begin at if you're using TweenLite.from()). An example hex value would be 0xFF0000.
      • removeTint : Boolean - If you'd like to remove the tint that's applied to a DisplayObject, pass true for this special property.
      • frame : Number - Use this to tween a MovieClip to a particular frame.
      • timeScale : Number - Multiplier for controlling the speed of the tween. 0.5 = half speed, 1 = normal speed, 2 = double speed, etc. (hint: if you want to gradually speed up or slow down a tween, you could get fancy and use another tween to tween the timeScale propety!) NOTE: There is also a static TweenMax.globalTimeScale property that affects ALL TweenMax and TweenFilterLite tweens (not TweenLite though)
      • roundProps : Array - If you'd like the inbetween values in a tween to always get rounded to the nearest integer, use the roundProps special property. Just pass in an Array containing the property names that you'd like rounded. For example, if you're tweening the x, y, and alpha properties of mc and you want to round the x and y values (not alpha) every time the tween is rendered, you'd do: TweenFilterLite.to(mc, 2, {x:300, y:200, alpha:0.5, roundProps:["x","y"]});
      • onStart : Function - If you'd like to call a function as soon as the tween begins, pass in a reference to it here. This can be useful when there's a delay and you want something to happen just as the tween begins.
      • onStartParams : Array - An array of parameters to pass the onStart function.
      • onUpdate : Function - If you'd like to call a function every time the property values are updated (on every frame during the time the tween is active), pass a reference to it here.
      • onUpdateParams : Array - An array of parameters to pass the onUpdate function (this is optional)
      • onComplete : Function - If you'd like to call a function when the tween has finished, use this.
      • onCompleteParams : Array - An array of parameters to pass the onComplete function (this is optional)
      • persist : Boolean - if true, the TweenFilterLite instance will NOT automatically be removed from the rendering queue and made eligible for garbage collector when it is complete. However, it is still eligible to be overwritten by new tweens even if persist is true. By default, it is false.
      • renderOnStart : Boolean - If you're using TweenFilterLite.from() (or runBackwards:true) with a delay and want to prevent the tween from rendering until it actually begins, set this special property to true. By default, it's false which causes TweenFilterLite.from() to render its values immediately, even before the delay has expired.
      • runBackwards : Boolean - Flips the start and end values in a tween. The from() method automatically sets this value to true.
      • overwrite : Number - Controls how other tweens of the same object are handled when this tween is created. You can set a default value to be used with ALL tweens in your SWF using the OverwriteManager's init() method, or control them individually with this overwrite property. Here are the options:
        1. 0 (NONE): No tweens are overwritten. This is the fastest mode, but you need to be careful not to create any
          tweens with overlapping properties, otherwise they'll conflict with each other.
        2. 1 (ALL): (this is the default unless OverwriteManager.init() has been called) All tweens of the same object are completely overwritten immediately when the tween is created.
          TweenFilterLite.to(mc, 1, {x:100, y:200});
          TweenFilterLite.to(mc, 1, {x:300, delay:2, overwrite:1}); //immediately overwrites the previous tween
        3. 2 (AUTO): (used by default if OverwriteManager.init() has been called) Searches for and overwrites only individual overlapping properties in tweens that are active when the tween begins.
          TweenFilterLite.to(mc, 1, {x:100, y:200});
          TweenFilterLite.to(mc, 1, {x:300, overwrite:2}); //only overwrites the "x" property in the previous tween
        4. 3 (CONCURRENT): Overwrites all tweens of the same object that are active when the tween begins.
          TweenFilterLite.to(mc, 1, {x:100, y:200});
          TweenFilterLite.to(mc, 1, {x:300, delay:2, overwrite:3}); //does NOT overwrite the previous tween because the first tween will have finished by the time this one begins.
      • blurFilter : Object - To apply a BlurFilter, pass an object with one or more of the following properties:
        • blurX
        • blurY
        • quality
      • glowFilter : Object - To apply a GlowFilter, pass an object with one or more of the following properties:
        • alpha
        • blurX
        • blurY
        • color
        • strength
        • quality
        • inner
        • knockout
      • colorMatrixFilter : Object - To apply a ColorMatrixFilter, pass an object with one or more of the following properties:
        • colorize
        • amount
        • contrast
        • brightness
        • saturation
        • hue
        • threshold
        • relative
        • matrix
      • dropShadowFilter : Object - To apply a DropShadowFilter, pass an object with one or more of the following properties:
        • alpha
        • angle
        • blurX
        • blurY
        • color
        • distance
        • strength
        • quality
      • bevelFilter : Object - To apply a BevelFilter, pass an object with one or more of the following properties:
        • angle
        • blurX
        • blurY
        • distance
        • highlightAlpha
        • highlightColor
        • shadowAlpha
        • shadowColor
        • strength
        • quality

STATIC METHODS

TweenFilterLite.to(target:Object, duration:Number, variables:Object):TweenFilterLite
  • Description:Creates a new TweenFilterLite instance for you, but shields you from having to create a variable to store it in which can make garbage collection more difficult.
    Actionscript:
    1. var myTween:TweenFilterLite = new TweenFilterLite(mc, 1, {x:300});
    2. TweenFilterLite.to(mc, 1, {x:300}); //exactly the same as the previous line, but handles garbage collection automatically.

  • Parameters: Same as Constructor (see above)
TweenFilterLite.from(target:Object, duration:Number, variables:Object):TweenFilterLite
  • Description: Exactly the same as TweenFilterLite.to(), but instead of tweening the properties from where they're at currently to whatever you define, this tweens them the opposite way - from where you define TO where ever they are now (when the method is called). This is handy for when things are set up on the stage the way the should end up and you just want to tween them to where they are.
  • Parameters: Same as TweenFilterLite.to(). (see above)
TweenFilterLite.delayedCall(delay:Number, onComplete:Function, onCompleteParams:Array):TweenLite
  • Description: Provides an easy way to call any function after a specified number of seconds. Any number of parameters can be passed to that function when it's called too.
  • Parameters:
    1. delay : Number - Number of seconds before the function should be called.
    2. onComplete : Function - The function to call
    3. onCompleteParams : Array - [optional] An array of parameters to pass the onComplete function when it's called.
TweenFilterLite.killTweensOf(target:Object, complete:Boolean):Void
  • Description: Provides an easy way to kill all tweens of a particular Object/DisplayObject. You can optionally force it to immediately complete (which will also call the onComplete function if you defined one)
  • Parameters:
    1. target : Object - Any/All tweens of this Object will be killed.
    2. complete : Function - If true, the tweens for this object will immediately complete (go to the ending values and call the onComplete function if you defined one).
TweenFilterLite.killDelayedCallsTo(function:Function):Void
  • Description: Provides an easy way to kill all delayed calls to a particular function (ones that were instantiated using the TweenFilterLite.delayedCall() method).
  • Parameters:
    1. function : Function - Any/All delayed calls to this function will be killed.
TweenFilterLite.setGlobalTimeScale(scale:Number):void
  • Description: Speed up or slow down ALL TweenMax and TweenFilterLite tweens. Each tween's timeScale property and the TweenFilterLite.globalTimeScale are cumulative, giving you lots of control. So, for example, if an individual tween has a timeScale of 0.5 and you set the globalTimeScale to 0.5 as well, the tween will appear to run at 0.25 speed. If you want the globalTimeScale to gradually change, you can even tween it using TweenLite (which isn't affected by globalTimeScale), like this: TweenLite.to(TweenFilterLite, 1, {globalTimeScale:0.5});
  • Parameters:
    1. scale : Number - Multiplier for controlling the speed of the tweens. 0.5 = half speed, 1 = normal speed, 2 = double speed, etc.

EXAMPLES

As a simple example, you could tween the blur of clip_mc from where it's at now to 20 over the course of 1.5 seconds by:

Actionscript:
  1. import gs.TweenFilterLite;
  2. TweenFilterLite.to(clip_mc, 1.5, {blurFilter:{blurX:20, blurY:20}});

If you want to get more advanced and tween the clip_mc MovieClip over 5 seconds, changing the saturation to 0, delay starting the whole tween by 2 seconds, and then call a function named "onFinishTween" when it has completed and pass in a few arguments to that function (a value of 5 and a reference to the clip_mc), you'd do so like:

Actionscript:
  1. import gs.*;
  2. import gs.easing.*;
  3. TweenFilterLite.to(clip_mc, 5, {colorMatrixFilter:{saturation:0}, delay:2, onComplete:onFinishTween, onCompleteParams:[5, clip_mc]});
  4. function onFinishTween(argument1:Number, argument2:MovieClip):Void {
  5.     trace("The tween has finished! argument1 = " + argument1 + ", and argument2 = " + argument2);
  6. }

If you have a MovieClip on the stage that already has the properties you'd like to end at, and you'd like to start with a colorized version (red: 0xFF0000) and tween to the current properties, you could:

Actionscript:
  1. import gs.*;
  2. TweenFilterLite.from(clip_mc, 5, {colorMatrixFilter:{colorize:0xFF0000}});

Need Help?

Feel free to post your question on the forums. You'll increase your chances of getting a prompt answer if you include a simplified FLA file (and any class files) that clearly demonstrates the problem and provide a brief explanation.

Author: Jack Doyle, (e-mail: jack -at- greensock.com)
Copyright 2009, GreenSock (This work is subject to the terms here.)

23 Responses to 'TweenFilterLite (AS3 Version) – Easily Tween Filters & Image Effects'

Subscribe to comments with RSS

  1. Robert said,

    on May 30th, 2007 at 8:29 pm

    Jack

    many thanks for sharing your hard work. Having found the MC Tween class to be hugely timesaving and extremely useful I was looking for somthing that could product similar effects on color and brightness properties.

    I’m sure it will become a key tool in future development

    Regards

    Robert

  2. Bryan Bartow said,

    on June 2nd, 2007 at 3:14 am

    Jack, thanks for creating this handy class. I can already tell that it will save me a fair amount of time. I’ll be sure to let you know if I publish anything that uses the class.

  3. Zsotl Popa said,

    on June 14th, 2007 at 2:08 am

    Hello Jack, i just want to say a “thank you” here too, because i’m really amazed about your work and your helpfull mentality, love your tween, its a big help to me.

    regards
    Zsolt

  4. Nate said,

    on July 31st, 2007 at 10:04 am

    Much thanks for this great little package. I’ve been looking for a solid animation package to accompany AS3 projects and I think I’ve finally found it. Kudos on your work!

  5. Sean said,

    on August 30th, 2007 at 10:12 am

    For those of you who want to use this class in Flex … ITS EASY.
    Flex does not allow you to add to the Application stage a MovieClip and this class uses a MovieClip for all of its methods ….

    Solution:

    Simply subclass any class you want ( for example ) and than continue by implementing two public methods in this class as follows:

    public function set quality(i:int):void {}

    public function get quality():int {
    return 1;
    }

    That’s it …

    Now use your class instead of MC.

    Enjoy.

    Sean – HeliHobby.com

  6. blogk said,

    on November 26th, 2007 at 9:45 am

    great class and all, it’s the best in terms of filesize and simplicity, BUT:

    sometimes (and it really is _essentially_ random) it throws an error

    TypeError: Error #1010: A term is undefined and has no properties.
    at gs::TweenFilterLite/render()
    at gs::TweenLite$/executeAll()

    when trying to tween blur filter. it stops execution of the tween and usually occurs when tweening blur filter of more than 40 instances at once.

    and this has been a real pain in the ass!!

  7. jack said,

    on November 26th, 2007 at 4:09 pm

    blogk, after many hours of banging my head against the screen and trying to chase down the rare (and apparently completely random) 1010 error that TweenFilterLite throws from time to time in specific scenarios, I think I’ve figured it out. Actually, I’m almost positive it was caused by a bug in Flash but I whipped together a workaround and implemented it in version 5.5 of the class.

    Thanks for the feedback.

  8. Felixz said,

    on December 23rd, 2007 at 1:24 pm

    I have a problem using TweenFilterLite with textFields…
    TweenFilterLite.to(someTextField,2,{type:”glow”,color:0×0000FF,blurX:20,blurY:10,strength:100});
    I constantly get:
    ArgumentError: Error #2008: Parameter type must be one of the accepted values.
    at flash.text::TextField/set type()
    at gs::TweenFilterLite/render()
    at gs::TweenLite$/executeAll()

  9. jack said,

    on December 26th, 2007 at 1:48 pm

    Felixz, great catch. As of version 5.84, the error is fixed and you can now tween filters on TextFields.

  10. Tony said,

    on March 7th, 2008 at 7:39 pm

    Another quickie, Jack:
    Could you post a quick tut on sound tweening of loaded mp3s (with other tweens running as well)? I’ve messed around with both the loaded sound as well as a SoundChannel, but a desired volume tween doesn’t seem to work. Or is this only possible with the newest version?

    Thanks again.

    – Tony

  11. jack said,

    on March 7th, 2008 at 11:11 pm

    Tony, tweening audio volume can be done two ways:

    1) On a MovieClip that contains audio. This is as simple as:
    TweenLite.to(myMovieClip, 2, {volume:0});

    2) On a SoundChannel object.This is likely what you’d do with the MP3s you loaded in. When you play() your audio, it returns a SoundChannel object, so once you’ve loaded your MP3, it should be as easy as:

    var myChannel = mySound.play();
    TweenLite.to(myChannel, 2, {volume:0});

    Keep in mind, you’re NOT tweening the Sound object – you’re tweening the SoundChannel object.

  12. John said,

    on March 10th, 2008 at 11:32 pm

    How can I set it to loop and change colors ramdonly or using an aray of colors?

    Awesome work.

  13. jack said,

    on March 11th, 2008 at 10:05 am

    John, you could accomplish what you’re after with something like:

    var myColors:Array = [0xFF0000, 0x00FF00, 0x0000FF];
    function tweenColor():void {
    var randomColor = myColors[Math.floor(Math.random() * myColors.length)];
    TweenFilterLite.to(my_mc, 2, {colorMatrixFilter:{colorize:randomColor, amount:2}, onComplete:tweenColor});
    }
    tweenColor();

  14. Biffer said,

    on March 11th, 2008 at 11:07 am

    Hi there i have added an additional class for those who wish to use Events. This is a seperate class – so just use this instead of TweenLite.

    A data object is dispatched with the Event so you can still pass around onStartParams etc in the usual way but just pick them up from the event listener.

    hope this is some good to you event loving types.

    http://biffcom.com/resource/tweenLiteEventMod/TweenLiteEventModExample.zip

    Thanks,

    Biffer Rowley

  15. Tony said,

    on March 13th, 2008 at 2:32 am

    John:
    Or you could just randomize the “hue” color parameter which is handily a 0-360 scale in TweenFilterLite:

    theHue = (Math.round(Math.random() * 360)) ;
    TweenFilterLite.to(my_mc, 2, {colorMatrixFilter:{amount:1, hue:theHue}});

    You can enhance the effect with the brightness parameter as well (-1 to 1 scale). Very cool result with sky images.
    –Tony

  16. Nicolas said,

    on April 3rd, 2008 at 5:11 am

    How i can dublicate effect like flash bulb. It is simple in Tweener.
    Tweener.addTween(_loader,{_color_rb:255, _color_gb:255, _color_bb:255, time:1, transition:”easeOutQuad”});

    I try use brightness with param 3 but result is poor for me.

  17. jack said,

    on April 3rd, 2008 at 11:36 pm

    Nicolas, you can definitely create the effect you’re looking for using the ColorTransformProxy utility class I created for use in conjunction with TweenLite for tweening all the various ColorTransform-related properties, including redOffset, greenOffset, blueOffset, redMultiplier, greenMultiplier, blueMultiplier, tint, and even tintPercent and brightness!. Sign up for Club GreenSock and you’ll get that class as a bonus (as well as a TrasnformMatrixProxy class). See http://blog.greensock.com/club/ for details.

  18. Lauren said,

    on April 20th, 2008 at 3:31 pm

    Thanks for providing this! Fantastic tool.

  19. Erich said,

    on May 8th, 2008 at 12:46 pm

    Jack, thanks for creating these great classes. You’ve saved me hours of time. If you’re ever in Chicago, I owe you a beer.

  20. brroy said,

    on May 12th, 2008 at 10:25 am

    I’ve been looking for a solid animation package to accompany AS3 projects and I think I’ve finally found it. Kudos on your work!

    helipross.com

  21. Cardigan said,

    on May 19th, 2008 at 9:01 am

    Thanks very much for this tool. Is it possible to remove a filter applied with tweenFilterLite?

  22. jack said,

    on May 19th, 2008 at 9:31 am

    Cardigan, there isn’t currently a built-in way to automatically kill a filter after tweening it, but I may add that feature in a future version. Right now, though, you can just use an onComplete function to kill your filter(s). To remove all filters from a DisplayObject, just set its filters property to an empty Array in your custom onComplete function, like:

    my_mc.filters = [];

    Or to kill a specific kind of filter, you could set up an onComplete function like:

    function removeFilterType($object:DisplayObject, $filterType:BitmapFilter):void {
    var f:Array = $object.filters;
    var a:Array = []; //remaining filters
    for (var i:int = f.length – 1; i > -1; i–) {
    if (!(f[i] is $filterType)) {
    a.push(f[i]);
    }
    }
    $object.filters = a;
    }

    and then in your tween, you’d do something like:

    TweenFilterLite.to(my_mc, 1, {blurFilter:{blurX:20}, onComplete:removeFilterType, onCompleteParams:[my_mc, BlurFilter]});

    (don’t forget to import the flash.filters.BlurFilter class)

  23. Jay said,

    on September 25th, 2008 at 6:51 pm

    Thank you so much for sharing that, you are truly a hero of the flash dev world… I feel a donation will be imminent…

Leave a Reply