Category Archives: Graphics

Random Walk

Actionscript:
  1. var xp:Number=Math.random() * stage.stageWidth;
  2. var yp:Number=Math.random() * stage.stageHeight;
  3. graphics.lineStyle(0,0x000000);
  4. graphics.moveTo(xp, yp);
  5. addEventListener(Event.ENTER_FRAME, onLoop);
  6. function onLoop(evt:Event):void {
  7.     xp+=Math.random()*10-5;
  8.     yp+=Math.random()*10-5;
  9.     graphics.lineTo(xp, yp);
  10. }

Nothing special here, but its good to know that this technique has a name... and that it's NOT Brownian Motion... more here.

Also posted in motion | Tagged , , | Leave a comment

drawPath() Circle Segment

drawPath() is flash 10 player only

Actionscript:
  1. [SWF(width=400,height=400,backgroundColor=0xEFEFEF,frameRate=30)]
  2.  
  3. const TWO_PI:Number = Math.PI * 2;
  4.  
  5. var resolution:Number = 50;
  6. var step:Number = TWO_PI / resolution;
  7. var maxIndex:int = 0;
  8.  
  9. var coords:Vector.<Number> = new Vector.<Number>();
  10. var drawCommands:Vector.<int> = new Vector.<int>();
  11.  
  12. // populate vectors
  13. for (var i:Number = 0; i <TWO_PI + step; i += step){
  14.     coords.push(100 * Math.cos(i));
  15.     coords.push(100 * Math.sin(i));
  16.     drawCommands.push(GraphicsPathCommand.LINE_TO);
  17. }
  18.  
  19. var circleSegment:Shape = new Shape();
  20. circleSegment.x = circleSegment.y = 200;
  21. addChild(circleSegment);
  22.  
  23. addEventListener(Event.ENTER_FRAME, onLoop);
  24. function onLoop(evt:Event):void {
  25.     with (circleSegment.graphics) {
  26.         clear();
  27.         beginFill(0x000000);
  28.        
  29.         // count by two, up to coords.length (drawCommands is exactly half the length of coords)
  30.         maxIndex =  Math.ceil((mouseX / stage.stageWidth)  * drawCommands.length) * 2;
  31.  
  32.         drawPath(drawCommands, coords.slice(0, maxIndex));
  33.     }
  34. }

The new additions to the Drawing API are great. This is just a quick test done with drawPath() to create a circle segment - this technique could be used to draw a pie chart.

The old way would have required lots of calls to the Graphics.lineTo() method. Take a look:

Actionscript:
  1. const TWO_PI:Number = Math.PI * 2;
  2.  
  3. var resolution:Number = 50;
  4. var step:Number = TWO_PI / resolution;
  5. var maxAngle:Number;
  6.  
  7. var circleSegment:Shape = new Shape();
  8. circleSegment.x = circleSegment.y = 200;
  9. addChild(circleSegment);
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12. function onLoop(evt:Event):void {
  13.     with (circleSegment.graphics) {
  14.         clear();
  15.         beginFill(0x000000);
  16.        
  17.         // count up to (TWO_PI + step), we add step to close the circle
  18.         maxAngle =  (TWO_PI + step) *  (mouseX / stage.stageWidth);
  19.  
  20.         for (var i:Number = 0; i <maxAngle; i += step) {
  21.             var xp:Number = 100 * Math.cos(i);
  22.             var yp:Number = 100 * Math.sin(i);
  23.             lineTo(xp, yp);
  24.         }
  25.     }
  26. }

If your interested in learning more about the FP10 Drawing API features, you can read more here.

Also posted in Vector | Tagged , , , | Leave a comment

Isometric Box

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. for (var i:int = 0; i<100; i++){
  4.     makeBoxSegment(200, 200 - i, i * 2);
  5. }
  6.  
  7. function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
  8.     var isoBox:Sprite = Sprite(addChild(new Sprite()));
  9.     with (isoBox) scaleY = .5, y = yp, x = xp;
  10.     var box:Shape = Shape(isoBox.addChild(new Shape()));
  11.     box.rotation = 45;
  12.     with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
  13.     isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
  14.     return isoBox;
  15. }
  16.  
  17. function onRotate(evt:Event):void {
  18.     evt.currentTarget.getChildAt(0).rotation = mouseX;
  19. }

An isometric box that rotates with the mouseX.

Also posted in DisplayObject | Tagged , , , | 2 Comments

Recursive Tree

Actionscript:
  1. var branches:int = 0;
  2. var maxBranches:int = 400;
  3.  
  4. graphics.lineStyle(0,0x000000);
  5.  
  6. makeBranch(300,350,100,-45,45);
  7.      
  8. function makeBranch(xp:Number, yp:Number, leng:Number, min:Number, max:Number):void {
  9.  
  10.     var endX:Number, endY:Number;
  11.     var theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI / 180;
  12.      
  13.     endX = xp + leng * Math.cos(theta);
  14.     endY = yp + leng * Math.sin(theta);
  15.  
  16.     graphics.moveTo(xp, yp);
  17.     graphics.lineTo(endX, endY);
  18.    
  19.     if (branches <maxBranches) {
  20.         var newLength:Number = leng*.7;
  21.         setTimeout(makeBranch, 0, endX, endY, newLength, -90, 0);
  22.         setTimeout(makeBranch, 0, endX, endY, newLength, 0, 90);
  23.     }
  24.     branches+=2;
  25. }

Draws a tree using recursion.

Also posted in functions | Tagged , , , | Leave a comment