package { import flash.display.GradientType; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Matrix; [SWF(width=550, height=400, backgroundColor=0xFFFFFF)] /** * Demonstrates the drawing of a radial gradient fill that updates as the mouse moves. */ public class graphic_flex_image_effects_01_Flex_DrawingGradientFills extends Sprite { /** * Constructor. This establishes the MOUSE_MOVE listener and draws the background initially. */ public function graphic_flex_image_effects_01_Flex_DrawingGradientFills() { stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); drawBackground(); } /** * Draws the background with a radial gradient. */ private function drawBackground():void { var colors:Array = [0xFFFF00, 0xFF0000, 0x000000]; var alphas:Array = [1, 1, 1]; var ratios:Array = [50, 100, 255]; var matrix:Matrix = new Matrix(); // the offset of the gradient, which moves its center, is determined by the mouse position matrix.createGradientBox(200, 200, 0, stage.mouseX-100, stage.mouseY-100); graphics.clear(); graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix); graphics.lineTo(stage.stageWidth, 0); graphics.lineTo(stage.stageWidth, stage.stageHeight); graphics.lineTo(0, stage.stageHeight); graphics.lineTo(0, 0); graphics.endFill(); } /** * Handler for when the mouse is moved. This redraws the gradient. * * @param event The MouseEvent caused by the movement. */ private function onStageMouseMove(event:MouseEvent):void { drawBackground(); event.updateAfterEvent(); } } }