Category Archives: Uncategorized

Sierpiński Glitch Texture #1

Actionscript:
  1. [SWF(frameRate=60, backgroundColor=0x000000, width=500, height=500)]
  2. var canvas:BitmapData = new BitmapData(500,500,false, 0x000000);
  3. addChild(new Bitmap(canvas));
  4. var clone:BitmapData = new BitmapData(500,500,false, 0x000000);
  5. var canvasRect:Rectangle = canvas.rect;
  6. var w:int = canvas.width;
  7. var w2:Number = 1/w;
  8. var w10:Number = 1/(w * 80);
  9. var convert:Number = Math.PI/180;
  10. var size:int = canvas.width * canvas.height;
  11. var pix:Vector.<uint> = new Vector.<uint>(size, true);
  12. var gray:ColorMatrixFilter = new ColorMatrixFilter([1, 0.55, 0.55, 0,0,0.55, 0.9, 0.55, 0,0,0.55, 0.55, 0.550,0, 0,0,0,1,0]);
  13. var m:Matrix = new Matrix();
  14. m.scale(1,-1);
  15. m.translate(0,canvas.height);
  16. var sin:Number = 0, cos:Number = 0;
  17. var dx:Number = 0, dy:Number = 0;
  18. var pnt:Point = new Point();
  19. var blur:BlurFilter = new BlurFilter(10,10,1);
  20. addEventListener(Event.ENTER_FRAME, onLoop);
  21. function onLoop(evt:Event):void {
  22.     canvas.lock();
  23.     dx += (mouseX * 10 - 3000 - dx) / 8;
  24.     dy += (mouseY * 4 - dy) / 8;
  25.     for (var i:int = 0; i<size; i++){
  26.         var xp:int = i % w;
  27.         var yp:int = int(i * w2);
  28.         var xp2:int = xp <<1;
  29.         var t:Number;
  30.         t = ((yp|xp) * (xp + dx) *w10) % 6.14687;
  31.        
  32.         //compute sine
  33.         // technique from http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/
  34.         // by Michael Baczynski
  35.         if (t<0) {
  36.             sin=1.27323954*t+.405284735*t*t;
  37.         } else {
  38.             sin=1.27323954*t-0.405284735*t*t;
  39.         }
  40.         // compute cosine
  41.         t = (xp2 + dy) * convert % 6.28;
  42.         t+=1.57079632;
  43.         if (t>3.14159265) {
  44.             t-=6.28318531;
  45.         }
  46.         if (t<0) {
  47.             cos=1.27323954*t+0.405284735*t*t;
  48.         } else {
  49.             cos=1.27323954*t-0.405284735*t*t;
  50.         }
  51.         var c1:int = 31 * (sin - cos);
  52.         if (c1 <0) c1 = 256 - c1;
  53.         c1 = (c1 <<3 | c1) ;
  54.         pix[i] = c1 <<15 | c1 <<8 | c1;
  55.     }
  56.     canvas.setVector(canvasRect, pix);
  57.     clone.copyPixels(canvas, canvasRect, pnt);
  58.     canvas.draw(clone, m, null, BlendMode.SUBTRACT);
  59.     clone.copyPixels(canvas, canvasRect, pnt);
  60.     clone.applyFilter(clone, canvasRect, pnt, blur);
  61.     canvas.draw(clone, null, null, BlendMode.ADD);
  62.     canvas.unlock();
  63. }

More strange real-time texture generation. This one is particularly glitchy looking with hints of Sierpiński.


Check out the swf at wonderfl.net...

Posted in Uncategorized | Tagged , , | 4 Comments

ActionSnippet Auto-Post

I'm going out of town for a week or so - the site will be on auto-post (QuickBox2D joint demos in the pipeline)... so apologies if your comments aren't approved in a timely fashion.

Posted in Uncategorized | Leave a comment

Funny Parameterized Factory

Actionscript:
  1. package{
  2.  
  3.     import flash.utils.getDefinitionByName;
  4.    
  5.     public class Creator{
  6.        
  7.         public function Creator():void{
  8.             // classes we can create
  9.              ClassA;
  10.              ClassB;
  11.         }
  12.  
  13.         public function create(type:String):ISomeInterface{
  14.              var ClassRef:Class = getDefinitionByName(type) as Class;
  15.              return ISomeInterface(new ClassRef());
  16.         }
  17.     }
  18. }

Actionscript:
  1. package{
  2.     public interface ISomeInterface{
  3.         function sayHi():void;
  4.     }
  5. }

Actionscript:
  1. package{
  2.    
  3.     public class ClassA implements ISomeInterface{
  4.        
  5.         public function ClassA(){
  6.             trace("new ClassA()");
  7.         }
  8.        
  9.         public function sayHi():void{
  10.             trace("hello I am a ClassA instance");
  11.         }
  12.     }
  13.      
  14. }

Actionscript:
  1. package{
  2.    
  3.     public class ClassB implements ISomeInterface{
  4.        
  5.         public function ClassB(){
  6.             trace("new ClassB()");
  7.         }
  8.        
  9.         public function sayHi():void{
  10.             trace("hello I am a ClassB instance");
  11.         }
  12.     }
  13.      
  14. }

... and lastly on the timeline:

Actionscript:
  1. var creator:Creator = new Creator();
  2.  
  3. var a:ISomeInterface = creator.create("ClassA");
  4. a.sayHi();
  5.  
  6. var b:ISomeInterface = creator.create("ClassB");
  7. b.sayHi();
  8.  
  9. /*outputs
  10.  
  11. new ClassA()
  12. hello I am a ClassA instance
  13. new ClassB()
  14. hello I am a ClassB instance
  15.  
  16. */

Unfortunately this has some downsides compared to a switch or if statement parameterized factory. Still fun though...

Posted in Uncategorized | Tagged , , | Comments closed

Private Square Bracket Syntax

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite
  4.    
  5.     public class Test extends Sprite {
  6.        
  7.         private var _test:String = "private square brace syntax";
  8.         public function Test(){
  9.             trace(this["_test"]);
  10.         }
  11.     }
  12. }

It's important to note that square bracket syntax works with private vars... tomorrow I'll post a class I wrote that makes use of this technique to preserve encapsulation in an interesting way...

(auto-post didn't post this one yesterday for some reason... weird wordpress bug maybe?)

Posted in Uncategorized | Tagged , | 1 Comment