draw gradient dynamically with AS3

Found this gem of an example for drawing dynamic gradients:

/****************************
Import Classes
****************************/
import flash.display.*;
import flash.geom.*;
 
/****************************
Define Variables
****************************/
//Type of Gradient we will be using
var fType:String = GradientType.LINEAR;
//Colors of our gradient in the form of an array
var colors:Array = [ 0xF1F1F1, 0x666666 ];
//Store the Alpha Values in the form of an array
var alphas:Array = [ 1, 1 ];
//Array of color distribution ratios.  
//The value defines percentage of the width where the color is sampled at 100%
var ratios:Array = [ 0, 255 ];
//Create a Matrix instance and assign the Gradient Box
var matr:Matrix = new Matrix();
    matr.createGradientBox( 200, 20, 0, 0, 0 );
//SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
var sprMethod:String = SpreadMethod.PAD;
//Start the Gradietn and pass our variables to it
var sprite:Sprite = new Sprite();
//Save typing + increase performance through local reference to a Graphics object
var g:Graphics = sprite.graphics;
    g.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
    g.drawRect( 0, 0, 400, 200 );
 
addChild( sprite );

src: http://snipplr.com/view/7050/as3-creating-a-gradient-rectangle/ (if URL still works, look at it there!)

Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑