// Petri Leskinen, leskinen[dot]petri[at]luukku.com // July 2008, Espoo, Finland package { import flash.display.Sprite; import flash.display.BitmapData; import flash.geom.Point; import flash.geom.Rectangle; import flash.display.GraphicsPathCommand; import flash.events.Event; import flash.events.MouseEvent ; public class GraphicsRadial extends Sprite { public var color:uint = 0x00; public var n:int =256; // length of the curve public var perlinData:BitmapData; public var po:Array= [new Point(), new Point(), new Point() ]; private var _commands:Vector. = new Vector.(); private var _dataUint:Vector.; private var _dataVector:Vector.; public var paused:Boolean = false; private var i:int; public function GraphicsRadial(_color:uint = 0x00) { this.color = _color; // init the vector '_commands' : // first command MOVE_TO, all other LINE_TO _commands.push(GraphicsPathCommand.MOVE_TO); for (i=1 ; i!=n; i++) { _commands.push(GraphicsPathCommand.LINE_TO); } // bitmap of size path's length x 1, so it'll be fast perlinData = new BitmapData(n,1,false); // start animation upDate(new Event(Event.RENDER)); addEventListener(Event.ENTER_FRAME, upDate); // mouseClick = pause motion addEventListener(MouseEvent.CLICK, mouseClicked); } private function upDate(e:Event):void { // animate perlinNoise po[0].x += 0.3; po[0].y += 0.01; po[1].x -= 0.12; po[1].y += 0.01; po[2].x += 0.012510; perlinData.perlinNoise(n/8,1,3,13, true, // stiched true, // fractal noise 4,false, // only the blue channel's needed po); // count the trigs only once var angle:Number = 360.0/n *Math.PI/180.0; var sina:Number = Math.sin(angle); var cosa:Number = Math.cos(angle); var r:Number; var rot:Array = [1.0,0.0]; // rot[0]= cosine, rot[1]= sine _dataVector = new Vector.(); // count the new curve for (i=0; i!=n; i++) { // polar coordinate, // radius by perlinNoise's blue channel // angle runs from 0 to 360 degrees r = (perlinData.getPixel(i,0)&0xFF)-128.0; _dataVector.push( r*rot[0], r*rot[1] ); // x,y = r*cos,r*sin // rotate to next angle // | cos sin | // | -sin cos | rot = [ cosa*rot[0]-sina*rot[1],sina*rot[0]+cosa*rot[1] ]; } // draw the graphics with (this.graphics) { clear(); beginFill(color,1.0); lineStyle(0.0,0,0.0); // no contours drawPath(_commands, _dataVector); endFill(); } } private function mouseClicked(e:MouseEvent):void { // mouse click stops the motion if (paused) { addEventListener(Event.ENTER_FRAME, upDate); } else { removeEventListener(Event.ENTER_FRAME, upDate); } paused = !paused; } } }