Category Archives: misc

Random Walk Texture

Actionscript:
  1. package {
  2.  
  3.     [SWF(width=1000,height=1000)]
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     public class RandomWalkTexture extends Sprite {
  7.  
  8.         private var _canvas:BitmapData;
  9.         private var _populationNum:int=100;
  10.         private var _movers:Vector.<Mover>;
  11.         public function RandomWalkTexture() {
  12.             var sw:Number=stage.stageWidth;
  13.             var sh:Number=stage.stageHeight;
  14.             scaleX=scaleY=.25;
  15.             _canvas=new BitmapData(sw*4,sh*4,false,0x000000);
  16.             addChild(new Bitmap(_canvas));
  17.            
  18.             _movers = new Vector.<Mover>();
  19.             for (var i:int = 0; i<_populationNum; i++) {
  20.                 _movers[i]=new Mover(_canvas,sw*1.5+Math.random()*sw,sh*1.5+Math.random()*sh);
  21.             }
  22.             addEventListener(Event.ENTER_FRAME, onRun);
  23.         }
  24.         private function onRun(evt:Event):void {
  25.             for (var i:int = 0; i<200; i++) {
  26.                 for (var j:int = 0; j<_populationNum; j++) {
  27.                     _movers[j].run();
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. import flash.display.BitmapData;
  35. class Mover {
  36.     public var x:Number;
  37.     public var y:Number;
  38.     public var velX:Number;
  39.     public var velY:Number;
  40.     public var speed:Number;
  41.     private var _canvas:BitmapData;
  42.  
  43.     public function Mover(canvas:BitmapData, xp:Number, yp:Number) {
  44.         _canvas=canvas;
  45.         x=xp;
  46.         y=yp;
  47.         velX=0;
  48.         velY=0;
  49.         speed=Math.random()*5-2.5;
  50.     }
  51.     public function run():void {
  52.         x+=velX;
  53.         y+=velY;
  54.         _canvas.setPixel(x, y, 0xFFFFFF);
  55.         var dir:Number=int(Math.random()*4);
  56.         if (dir==0) {
  57.             velX=0;
  58.             velY=- speed;
  59.         } else if (dir == 1) {
  60.             velX=0;
  61.             velY=speed;
  62.         } else if (dir == 2) {
  63.             velX=- speed;
  64.             velY=0;
  65.         } else if (dir == 3) {
  66.             velX=speed;
  67.             velY=0;
  68.         }
  69.     }
  70. }

This snippet is meant to be run as a document class. Nothing special here... this is just something I found laying around - the actual bitmap being drawn is rather big, so I recommending right clicking (control clicking on mac) on the swf and to zoom in and out.

Here are a few images:




Posted in misc | Tagged , | 5 Comments

Dynamic Shapes

Actionscript:
  1. // build some functions:
  2. var redGradient:Function = sl(16, add(100, mult(5)));
  3.  
  4. // grid positioning
  5. var xPos:Function = add(50, mult(30, cInt(div(4))));
  6. var yPos:Function = add(50, mult(30, mod(4)));
  7.  
  8. // create some shapes:
  9. var shapes:Array = createShapes(this, 22, [["beginFill", 0xCCCCCC], ["drawCircle", 0, 0, 10], ["endFill"], ["lineStyle",1, redGradient], ["drawRect", -5, -5, 10, 10]], {x:yPos, y:xPos, rotation:mult(10)});
  10.                                                                  
  11. function createShapes(par:DisplayObjectContainer,  num:Number,
  12.                                          funcs:Array, props:Object):Array {
  13.     var shapes:Array = [];
  14.     for (var i:int = 0; i<num; i++){
  15.         shapes[i] = par.addChild(new Shape());
  16.         for (var j:int = 0; j<funcs.length; j++) {
  17.             var a:Array = funcs[j].concat();
  18.             for (var k:int = 0; k<a.length; k++){
  19.                 if (a[k] is Function){
  20.                     a[k] = a[k](i);
  21.                 }
  22.             }
  23.             for (var key:String in props){
  24.                 var v:* = props[key];
  25.                 if (v is Function){
  26.                   shapes[i][key] = v(i);
  27.                 }else{
  28.                   shapes[i][key] = v;
  29.                 }
  30.             }
  31.             shapes[i].graphics[a[0]].apply(shapes[i].graphics, a.slice(1));
  32.         }
  33.     }
  34.     return shapes;
  35. }
  36.  
  37. // function building blocks
  38. const F:Function = function(a:*):*{return a};
  39.  
  40. function cInt(f:Function=null):Function{
  41.     if (f == null) f = F;
  42.     return function(n:Number):Number {
  43.         return int(f(n));
  44.     }
  45. }
  46.  
  47. function mod(m:Number, f:Function=null):Function{
  48.     if (f == null) f = F;
  49.     return function(n:Number):Number {
  50.         return f(n) % m;
  51.     }
  52. }
  53.  
  54. function div(d:Number, f:Function=null):Function{
  55.     if (f == null) f = F;
  56.     return function(n:Number):Number {
  57.         return f(n) / d;
  58.     }
  59. }
  60.  
  61. function mult(scalar:Number, f:Function=null):Function{
  62.     if (f == null) f = F;
  63.      return function(n:Number):Number {
  64.         return f(n) * scalar;
  65.     }
  66. }
  67.  
  68. function add(off:Number, f:Function=null):Function{
  69.      if (f == null) f = F;
  70.      return function(n:Number):Number {
  71.         return f(n) + off;
  72.     }
  73. }
  74. // shift left
  75. function sl(amount:int, f:Function=null):Function{
  76.      if (f == null) f = F;
  77.      return function(n:Number):Number {
  78.         return f(n) <<amount
  79.     }
  80. }

This is an unusual snippet I wrote a couple weeks back.... not sure where I was going with this really... has some interesting ideas in it.

Posted in misc | Tagged , | Leave a comment

Relative Positioning

Actionscript:
  1. var size:Array = [1, 1.5, .5, 1, .4, 1, 1, 1, .2, 1.1]
  2. var boxes:Array = new Array();
  3. var spacing:Number = 4;
  4. var container:Sprite = Sprite(addChild(new Sprite()));
  5. container.x = container.y = 100;
  6.  
  7. for (var i:int = 0; i<size.length; i++){
  8.     var box:Sprite = makeBox();
  9.     var prev:int = i - 1;
  10.     box.scaleX= box.scaleY = size[i];
  11.     if (i == 0){
  12.         box.y = 10;
  13.     }else{
  14.         // here's the trick
  15.         // if you animate the height property you need to do this again and again:
  16.         box.y = boxes[prev].y + boxes[prev].height/2+ box.height/2 + spacing
  17.     }
  18.     boxes.push(box);
  19. }
  20.  
  21. function makeBox():Sprite{
  22.     var box:Sprite = Sprite(container.addChild(new Sprite()));
  23.     with (box.graphics) beginFill(0xFF0000), drawRect(-50,-10, 100, 20);
  24.     return box;
  25. }

Sometimes you need to position a bunch of Sprites or MovieClips that are different sizes - and you want to keep the spacing between them the same. This snippet shows a simple example of this.

For more info you could also do this tutorial that I wrote on learningactionscript3.com

Also posted in UI | Tagged , | Leave a comment

Shapevent Log (not a snippet)

Recently launched a new project over at shapevent.com. If your enjoy sketchbooks, drawings and interactive ugliness... have a look here:

Shapevent Log

Many of the techniques covered in the code on this website will be used in shapevent log entries... no rss feed yet, but I'll have one up for it in the next few days....

Posted in misc | Leave a comment