package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BitmapDataChannel; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; [SWF(width=500, height=500, backgroundColor=0x000000)] /** * Tests the different settings available in BitmapData's perlinNoise() method. */ public class graphic_flex_image_effects_04_Flex_PerlinNoiseTest extends Sprite { // these constants correspond to the parameters available in perlinNoise(); // configure them as you see fit to test the different settings private static const BASE_X:Number = 200; private static const BASE_Y:Number = 200; private static const NUM_OCTAVES:Number = 2; private static const RANDOM_SEED:Number = Number(new Date()); private static const STITCH:Boolean = false; private static const FRACTAL:Boolean = true; private static const CHANNELS:uint = BitmapDataChannel.RED; private static const GRAYSCALE:Boolean = false; private static const OFFSET_X_RATE:Number = 2; private static const OFFSET_Y_RATE:Number = 5; private var _bitmapData:BitmapData; private var _offsets:Array; /** * Constructor. Creates the bitmap data and properties needed to animate the Perlin noise and sets * up an ENTER_FRAME listener to animate the noise. */ public function graphic_flex_image_effects_04_Flex_PerlinNoiseTest() { _bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight); // octave offset positions, with length based on the number of octaves _offsets = []; for (var i:uint = 0; i < NUM_OCTAVES; i++) { _offsets.push(new Point()); } makePerlinNoise(); addChild(new Bitmap(_bitmapData)); addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame); } /** * Reapplies the Perlin noise to the bitmap data. */ private function makePerlinNoise():void { _bitmapData.perlinNoise( BASE_X, BASE_Y, NUM_OCTAVES, RANDOM_SEED, STITCH, FRACTAL, CHANNELS, GRAYSCALE, _offsets ); } /** * Handler for each new frame in the movie. This repositions the offsets and reapplies the noise. * * @param event Event dispatched by this sprite. */ private function onSpriteEnterFrame(event:Event):void { var point:Point; var direction:int; for (var i:uint = 0; i < NUM_OCTAVES; i++) { point = _offsets[i] as Point; // even loop iterations will move in a negative direction, odd loops will be positive direction = (i%2==0) ? -1 : 1; // higher octaves will move more quickly than lower octaves point.x += OFFSET_X_RATE/(NUM_OCTAVES-i)*direction; point.y += OFFSET_Y_RATE/(NUM_OCTAVES-i)*direction; } makePerlinNoise(); } } }