Monthly Archives: December 2008

Blendmode Emboss

Actionscript:
  1. var canvas:BitmapData = new BitmapData(500, 500, true, 0xFFFFFFFF);
  2. var color:BitmapData= new BitmapData(500, 500, true, 0x81666666);
  3. var over:BitmapData = new BitmapData(500, 500, true, 0xFF000000);
  4. addChild(new Bitmap(canvas, "auto", true));
  5. var circle:Shape = new Shape();
  6. circle.graphics.beginFill(0xFFFFFF,1);
  7. circle.graphics.drawCircle(0,0,50);
  8. var m:Matrix = new Matrix();
  9. m.tx = 0;
  10. m.ty = 1;
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12. function onLoop(evt:Event):void {
  13.     circle.x = mouseX;
  14.     circle.y = mouseY;
  15.     canvas.draw(circle, circle.transform.matrix);
  16.     canvas.copyPixels(color, color.rect, new Point(0,0), null, null, true);
  17.     over.copyPixels(canvas, canvas.rect, new Point(0,0), null, null, true);
  18.     canvas.draw(over, m, null, BlendMode.SCREEN);
  19.     over.applyFilter(over, over.rect, new Point(-2,-2), new BlurFilter(10,10,1));
  20.     canvas.draw(over, m, null, BlendMode.SUBTRACT);
  21. }

Grayscale emboss technique. More info here.

Posted in Uncategorized | Tagged , | Leave a comment

Dynamic Vars Dictionary

Actionscript:
  1. var vars:Dictionary = new Dictionary();
  2.  
  3. var sp:Sprite = new Sprite();
  4.  
  5. // associate variables with a sprite (or any non-dynamic class)
  6. vars[sp] = {posX:100, posY:100, velX:1, velY:1};
  7.  
  8. // read
  9. trace(vars[sp].posX);

I've heard people mention that they wish the sprite class were dynamic... meaning they wish they could add methods and properties to a Sprite instance at runtime. There's no way I know of to do this, however... the dictionary class can be used to associate variables with any non-dynamic class instance... as it does in this the above example.

The dictionary class is similar to an associative array except that instead of using strings for keys, dictionaries use object instances.

Posted in Dictionary, arrays, dynamic | Tagged , | 1 Comment

Transparent copyPixels()

Actionscript:
  1. var canvas:BitmapData=new BitmapData(400,400,true,0x000000);
  2. var eraser:BitmapData=new BitmapData(400,400,true,0x00000000);
  3. addChild(new Bitmap(canvas));
  4. var redOval:Shape = new Shape();
  5. with(redOval.graphics) lineStyle(0,0), beginFill(0xFF0000), drawEllipse(0,0,10,50);
  6.  
  7. // draw a background to emphasize transparency
  8. createBackground();
  9.  
  10. addEventListener(Event.ENTER_FRAME, onLoop);
  11.  
  12. function onLoop(evt:Event):void {
  13.     // clear canvas so that it is completely transparent
  14.     canvas.copyPixels(eraser, eraser.rect, new Point(0,0), eraser);
  15.     redOval.x = mouseX;
  16.     redOval.y = mouseY;
  17.     redOval.rotation += 10
  18.     // transform.matrix contains all transformation information for
  19.     // the redOval including scaleX, scaleY, x, y, rotation etc...
  20.     canvas.draw(redOval, redOval.transform.matrix);
  21. }
  22.  
  23. function createBackground():void {
  24.     for (var i:int = 0; i<100; i++){
  25.         with(graphics) lineStyle(0,0x000000), lineTo(Math.random()*400, Math.random()*400);
  26.     }
  27. }

This example builds on yesterdays snippet. Instead of just allowing the red oval to be continuously drawn to the canvas BitmapData... in this example the canvas BitmapData is erased again and again before the red oval is drawn to it. To erase it with a flat color you might use BitmapData.fillRect() but if you want to erase it so that it is completely transparent you need to use BitmapData.copyPixels():

Actionscript:
  1. canvas.copyPixels(eraser, eraser.rect, new Point(0,0), eraser);

The eraser BitmapData is a completely transparent BitmapData object. The key here is the fourth argument.... "alphaBitmapData".This is what causes the canvas BitmapData to become completely transparent again at the beginning of each enterFrame.

This argument is separate from the first argument (sourceBitmapData) for added flexibility.

Posted in BitmapData, pixel manipulation | Tagged , | 2 Comments

BitmapData Brush

Actionscript:
  1. var canvas:BitmapData=new BitmapData(400,400,true,0xFFCCCCCC);
  2. addChild(new Bitmap(canvas));
  3.  
  4. var redOval:Shape = new Shape();
  5. with(redOval.graphics) lineStyle(0,0), beginFill(0xFF0000), drawEllipse(0,0,10,50);
  6.  
  7. addEventListener(Event.ENTER_FRAME, onLoop);
  8. function onLoop(evt:Event):void {
  9.  
  10.     redOval.x = mouseX;
  11.     redOval.y = mouseY;
  12.     redOval.rotation += 10
  13.     // transform.matrix contains all transformation information for
  14.     // the redOval including scaleX, scaleY, x, y, rotation etc...
  15.     canvas.draw(redOval, redOval.transform.matrix);
  16. }

A very easy way to create a BitmapData brush.

Posted in BitmapData | Tagged , | Leave a comment