Monthly Archives: December 2008

BitmapData.floodFill()

Actionscript:
  1. var canvas:BitmapData = new BitmapData(400,400,false, 0xCC0000);
  2. addChild(new Bitmap(canvas));
  3.  
  4. var s:Shape = new Shape();
  5. s.graphics.beginFill(0x00CCCC);
  6. s.graphics.moveTo(Math.random()*400,Math.random()*400);
  7. for(var i:int = 0; i<10; i++){
  8.     s.graphics.lineTo(Math.random()*400,Math.random()*400);
  9. }
  10. canvas.draw(s);
  11.  
  12. stage.addEventListener(MouseEvent.CLICK, onDown);
  13. function onDown(evt:MouseEvent):void {
  14.     canvas.floodFill(mouseX, mouseY, 0x000000);
  15. }

floodFill() is basically the paint bucket (fill) tool in any bitmap drawing program. The above example draws an arbitrary blue polygon to a red background. Click anywhere on the resulting image to see floodFill() in action.

floodFill() takes 3 arguments... x, y and color.

Posted in BitmapData, pixel manipulation | Tagged , | Leave a comment

E4X and ActionScript Variables

Actionscript:
  1. var backgroundColor:uint = 0xEFEFEF;
  2. var borderColor:uint = 0xFF0000;
  3. var buttonOverColor:uint = 0x0000FF;
  4. var buttonOutColor:uint = 0x00CCCC;
  5.  
  6. var uiColors:XML = <ui>
  7.     <default color="0xCCCCCC" />
  8.     <background color = { backgroundColor } />
  9.    
  10.     <!-- note hexidecimal formatting code -->
  11.     <border color={ ("0x" + borderColor.toString(16)) } />
  12.    
  13.     <button overColor={ buttonOverColor} outColor={ buttonOutColor } />
  14. </ui>
  15.  
  16. trace(uiColors.toXMLString());
  17.  
  18. /*outputs:
  19. <ui>
  20.   <default color="0xCCCCCC"/>
  21.   <background color="15724527"/>
  22.   <border color="0xff0000"/>
  23.   <button overColor="255" outColor="52428"/>
  24. </ui>
  25. */

The first time I needed to use an ActionScript variable within inline XML I was stumped. I couldn't figure it out and I wasn't able to easily find it on google. I eventually found it somewhere (don't remember where... possibly hidden away in the docs).

Now a search for "insert actionscript variables into e4x" on google gives plenty of results. But I figure it's worth a post.

I use actionscript to generate XML all the time so this comes in handy. I also store color values in automatically generated XML all the time. If I'm feeling organized I'll use something like what you see on line 10:

Actionscript:
  1. <border color={ ("0x" + borderColor.toString(16)) } />

If you look at the output you'll see this formats the uint so that it's readable as a hex number. By default (as you can see in the output) uints will show up in decimal notation. This really doesn't make any difference if you don't care about XML readability ... or if you just don't care about the readability of the colors stored in your XML.....

Posted in XML, color, variables | Tagged , , , | Leave a comment

E4X Filtering

Actionscript:
  1. var userInfo:XML = <users>
  2.   <user fname="joe" lname="smith" age="31" />
  3.   <user fname="mildred" lname="calder" age="64" />
  4.   <user fname="ben" lname="nathanson" age="20" />
  5.   <user fname="james" lname="biuford" age="19" />
  6.   <user fname="nick" lname="calhoun" age="45" />
  7. </users>;
  8.  
  9.  
  10. trace("Users over 20:\n");
  11. trace(userInfo.user.(@age> 20).toXMLString());
  12.  
  13. trace("\nUsers with the name nick:\n");
  14. trace(userInfo.user.(@fname == "nick" ).toXMLString());
  15.  
  16. // use regular expressions with e4x
  17. trace("\nUsers with name starting with j:\n");
  18. trace(userInfo.user.(/^j/.test(@fname)));
  19.  
  20. /*
  21. outputs:
  22.  
  23. Users over 20:
  24.  
  25. <user fname="joe" lname="smith" age="31"/>
  26. <user fname="mildred" lname="calder" age="64"/>
  27. <user fname="nick" lname="calhoun" age="45"/>
  28.  
  29. Users with the name nick:
  30.  
  31. <user fname="nick" lname="calhoun" age="45"/>
  32.  
  33. Users with name starting with j:
  34.  
  35. <user fname="joe" lname="smith" age="31"/>
  36. <user fname="james" lname="biuford" age="19"/>
  37.  
  38. */

One of the nicest features of E4X is filtering. The above code shows a few simple examples ... the last example makes use of regular expressions -- I first read using regular expressions and E4X somewhere on http://www.darronschall.com/.

I usually prefer to use a database for any kind of info I'll be searching... but if you know you have a relatively small amount of data XML can be a fine way to go.

Posted in XML | Tagged , , , , | 2 Comments

Isometric Voxels

Actionscript:
  1. // isometric conversion
  2. var centerX:Number=stage.stageWidth/2;
  3. var centerY:Number=stage.stageHeight/2;
  4. var theta:Number=Math.PI/4;// 45 degrees;
  5. var cosX:Number=Math.cos(theta);
  6. var sinX:Number=Math.sin(theta);
  7. var pnt:Point = new Point();
  8. function iso3D(x:Number, y:Number, z:Number):Point {
  9.     pnt.x = centerX + (x-z) *  cosX
  10.     pnt.y = centerY -  (x+z) * 0.5 * sinX - y;
  11.     return pnt;
  12. }
  13. // example:
  14. var canvas:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000);
  15. addChild(new Bitmap(canvas,"auto",true));
  16. var size:int=100;
  17. var hs:int=size / 2;
  18. var pen:Point = new Point();
  19. var vect:Vector3D = new Vector3D();
  20. // draw a few shapes with offset:
  21. for (var dist:int = 10; dist <= 80; dist *= 2) {
  22.     // voxel space:
  23.     for (var i:int = 0; i<size; i++) {
  24.         for (var j:int = 0; j<size; j++) {
  25.             for (var k:int = 0; k<size; k++) {
  26.                 vect.x=j-hs;
  27.                 vect.y=i-hs;
  28.                 vect.z=k-hs;
  29.                 pen = iso3D(vect.x,vect.y,vect.z);
  30.                 if (Math.sqrt((vect.x * vect.x) + (vect.y * vect.y) + (vect.z * vect.z)) <dist) {
  31.                     // using Vector3D.distance() is very slow compared to above
  32.                     // a few types of coloring:
  33.                     var xp:Number = pen.x + (dist <<2) - 200;
  34.                     canvas.setPixel(xp, pen.y-100, (i <<16 | j <<8 | k) <<1);
  35.                     canvas.setPixel(xp, pen.y+100, (k <<16 | k <<8 | k+j)  );
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }

The above will draw this:

You can read more about voxels here.

This isn't the speediest way to do voxels (especially if you want to animate). This was just the first thing that came to mind.

Posted in 3D, BitmapData, color, pixel manipulation, setPixel | Tagged , , , | 2 Comments

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

Senocular’s Two Sided 3D Clip

CLICK HERE FOR TODAY'S SNIPPET

In a recent kirupa thread, senocular posted an very useful snippet to aid in the creation of a two sided 3D MovieClip. The thread also contains an in depth description of polygon winding.

Description of Polygon Winding

I created a navigation demo using this technique.

Here is the source for the above demo

UPDATE
Justin Windle of soulwire has written a nice class called PaperSprite that uses this technique. The class is worth checking out - it's nicely designed... read more about it here.

Posted in 3D, MovieClip | Tagged , , , | 4 Comments

Gradient Glow

Actionscript:
  1. var colors:Array = [0xFF0000, 0x666699, 0x223322, 0xCCCCDD, 0xFFEEFF];
  2. var alphas:Array = [0, 1, 1, 1, 1];
  3. var ratios:Array = [0, 50, 100, 200, 255]
  4. var filter:GradientGlowFilter = new GradientGlowFilter(0, 0, colors, alphas, ratios, 30, 30, 1, 2, "full", true);
  5.  
  6. var circles:Shape = new Shape();
  7.  
  8.  for (var i:int = 0; i<30; i++){
  9.   with(circles.graphics) beginFill(0xFF0000), drawCircle(Math.random()*500, Math.random()*400,10+Math.random()*40);
  10.  }
  11.  addChild(circles);
  12.  
  13.  circles.filters = [filter];

I don't see GradientGlowFilter used much. But with some tweaking you can probably get some decent stuff out of it.

Posted in Graphics | Tagged , | Leave a comment