ActionScript: Filling a Shape with a Gradient

by Elis Frugalo.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Use the beginGradientFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime.

In a gradient fill, there is a graded change in colors. Flash supports linear gradients, in which one color fades into the next from left to right. Flash also supports radial gradients, in which the colors radiate out from a center point. You can initiate a gradient-filled shape using beginGradientFill( ) in the same way you initiate a solid-filled shape with beginFill( ). The difference is that the call to beginGradientFill( ) requires a more complex set of parameters:

gradientType
Either "linear" for a linear gradient, or "radial" for a radial gradient.

colors
An array of RGB values for the colors to use in the gradient. They are displayed in the gradient from left to right in a linear gradient, or from the center outward in a radial gradient.

alphas
An array of alpha values that correspond to the colors in the colors parameter array.

ratios
An array whose elements are numbers corresponding to the colors and alphas elements. The values in the ratios array indicate the point within the gradient at which each color is pure. The range of values for the ratios should be from 0 (leftmost point in a linear fill, or innermost point in a radial fill) to 255 (rightmost or outermost).

matrix
An object with the following properties:

matrixType
This value should always be "box".

x
The x coordinate of the bottom-left corner of the gradient.

y
The y coordinate of the bottom-left corner of the gradient.

width
The width of the gradient in pixels.

height
The height of the gradient in pixels.

r
The rotation of the gradient in radians (not degrees).

Here is an example that uses a linear gradient to fill a rectangle:

// Include the drawing methods, which are needed for the drawRectangle(  ) method.
   #include "DrawingMethods.as"
// Define the width and height of the rectangle to be drawn and filled.
   rectWidth  = 100;
   rectHeight = 200;
// Create an empty clip into which we will draw the shape.
   _root.createEmptyMovieClip("shape_mc", 1);
   shape_mc.lineStyle(3, 0, 100);
// Create a colors array with RGB values for blue, green, and red.
   colors = [0x0000FF, 0x00FF00, 0xFF0000];
// Create an alphas array in which the colors are 100% opaque.
   alphas = [100, 100, 100];
// Create a ratios array where pure blue is at the left edge of the gradient, pure
   // green is in the center, and pure red at the right edge.
   ratios = [0, 127.5, 255];
// Create the matrix object. Set the x and y coordinates so that the bottom-left 
   // corner of the gradient lines up with the bottom-left corner of the rectangle. Set
   // the width and height of the gradient to match the rectangle.
   matrix = {matrixType: "box", x: -rectWidth/2, y: -rectHeight/2, w: rectWidth, 
   h: rectHeight, r:0};
// Call beginGradientFill(  ) so that the rectangle will be 
   // filled with a linear gradient.
   shape_mc.beginGradientFill("linear", colors, alphas, ratios, matrix);
// Draw the rectangle with rounded corners (requires DrawingMethods.as).
   shape_mc.drawRectangle(rectHeight, rectWidth, 10);
// End the fill.
   shape_mc.endFill(  );

Note that the endFill( ) method is used to end a drawing operation begun with either beginFill( ) or beginGradientFill( ).

Here is an example of a radial, gradient fill used to fill an ellipse:

// Include the drawing methods, which are needed for the drawEllipse(  ) method.
   #include "DrawingMethods.as"
// Define the width and height of the ellipse to be drawn and filled.
   ellipseWidth  = 100;
   ellipseHeight = 200;
_root.createEmptyMovieClip("shape_mc", 1);
   shape_mc.lineStyle(3, 0x000000, 100);
// Create colors, alphas, and ratios arrays for white and black, both 100% opaque. 
   // Pure white starts in the center and grades into pure black at the outside edge.
   colors = [0xFFFFFF, 0x000000];
   alphas = [100, 100];
   ratios = [0, 255];
// Define the matrix object.
   matrix = {matrixType: "box", x: -ellipseWidth/2, y: -ellipseHeight/2, 
   w: ellipseWidth, h: ellipseHeight, r:0};
// Begin the radial fill.
   shape_mc.beginGradientFill("radial", colors, alphas, ratios, matrix);
// Draw the ellipse (requires DrawingMethods.as).
   shape_mc.drawEllipse(ellipseWidth/2, ellipseHeight/2);
// End the fill.
   shape_mc.endFill(  );
 
Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.