Category Archives: Graphics

Three Color Triangle

Actionscript:
  1. /*
  2. *       Petri Leskinen, Finland
  3. *       25 October 2009
  4. *       pixelero.wordpress.com
  5. *
  6. *       Actionscript 3.0, Flash CS3, Flash Player 10
  7. *
  8. *       threeColorTriangle
  9. *       draws a triangle with a gradient fill of three colors for each vertex
  10. *       using graphics.drawTriangles and a BitmapData of size 2x2
  11. */
  12. function threeColorTriangle(
  13.       point0:Point, color0:uint,
  14.       point1:Point, color1:uint,
  15.       point2:Point, color2:uint):void {
  16.  
  17.       //      create a  bitmap of size 2x2 pixels
  18.       var bmd:BitmapData = new BitmapData(2,2,true);
  19.       //      copy colors to bitmap
  20.       //      the fourth color is average of color1 and color2
  21.       bmd.setVector(bmd.rect,
  22.               Vector.<uint>([color0,color1,color2,
  23.                                          (color1+color2)>>1
  24.                                          ]));
  25.  
  26.       //      draw triangle
  27.       this.graphics.beginBitmapFill(bmd,null,false,true /* =smooth */ );
  28.       this.graphics.drawTriangles(
  29.               // x,y -coordinates
  30.               Vector.<Number>([
  31.                       point0.x,point0.y,
  32.                       point1.x,point1.y,
  33.                       point2.x,point2.y]),
  34.               // indices
  35.               Vector.<int>([0,1,2]),
  36.               // texture coordinates
  37.               Vector.<Number>([0,0, 1,0, 0,1])
  38.               );
  39. }
  40.  
  41.  
  42. // demo, let's draw some of these on the stage
  43. randomize();
  44. function randomize():void {
  45.       this.graphics.clear();
  46.  
  47.       for (var i:int = 0;i<128;i++) {
  48.               //      pick some random colors
  49.               var color0:uint = 0xFFFFFFFF;
  50.               var color1:uint = 0xFFFFFF*Math.random() | 0xFF000000;
  51.               var color2:uint = 0xFFFFFF*Math.random() | 0xFF000000;
  52.  
  53.               //      random points
  54. var point0:Point = new Point(Math.random()*stage.stageWidth,
  55.             Math.random()*stage.stageHeight);
  56. var point1:Point = new Point(point0.x+200*(Math.random()-Math.random()),
  57.             point0.y+200*(Math.random()-Math.random()));
  58. var point2:Point = new Point(point0.x+200*(Math.random()-Math.random()),
  59.             point0.y+200*(Math.random()-Math.random()));
  60.  
  61.         threeColorTriangle(point0, color0, point1, color1, point2,  color2);
  62.       }
  63. }

This snippet is by Petri Leskinen (pixelero). It draws a triangle with a gradient fill of three colors for each vertex using Graphics.drawTriangles and a BitmapData of size 2x2. This is the simplest case, it can easily be extended to four colors or maybe to use a larger 3x3, 4x4 etc... bitmap.

Here's a still...

Also posted in Vector | Tagged , , , | 5 Comments

Flowing Leaves (optical illusion)


Saw this optical illusion today... figured I'd make a snippet to create a few variations on the illusion...

Actionscript:
  1. [SWF( backgroundColor=0x2E7999, width=780, height = 600) ]
  2.  
  3. var leafNum:Number = 375;
  4. var spacing:Number = 12;
  5. var cols:Number = 25;
  6. var hh:Number = stage.stageHeight / 2;
  7. var hw:Number = stage.stageWidth / 2;
  8.  
  9. for (var i:Number = 0; i<leafNum; i++){
  10.     var leaf:Shape = makeLeaf();
  11.     leaf.scaleX = leaf.scaleY = 0.25;
  12.     leaf.rotation = 90;
  13.     leaf.x = 50 + (i % cols) * (leaf.width + spacing);
  14.     leaf.y = 40 + int(i / cols) * (leaf.height + spacing);
  15.     var dx:Number = leaf.x - hw;
  16.     var dy:Number = leaf.y - hh;
  17.     leaf.rotation = Math.sqrt(dx * dx + dy * dy);
  18. }
  19.  
  20. function makeLeaf():Shape{
  21.     var leaf:Shape = Shape(addChild(new Shape()));
  22.     leaf.graphics.beginFill(0x9DC4D4);
  23.     scaleYcircle(leaf.graphics, 50, .65, false);
  24.     leaf.graphics.endFill();
  25.     leaf.graphics.lineStyle(2, 0x003366, 1, false, "none", CapsStyle.SQUARE, JointStyle.MITER);
  26.     scaleYcircle(leaf.graphics, 50, .65);
  27.     leaf.graphics.lineStyle(2, 0xFFFFFF, 1, false, "none", CapsStyle.SQUARE, JointStyle.MITER);
  28.     scaleYcircle(leaf.graphics, -50, .65);
  29.     return leaf;
  30. }
  31.  
  32.  
  33. // original circle function by senocular (www.senocular.com) from here http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
  34. // circle that can be scaled on the y axis
  35. function scaleYcircle(g:Graphics, r:Number, s:Number = 1, isHalf:Boolean=true):void {
  36.      
  37.     var c1:Number = r * (Math.SQRT2 - 1);
  38.     var c2:Number = r * Math.SQRT2 / 2;
  39.     var rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;
  40.     var x_r:Number =  -r, y_r:Number = -rs, x_c2:Number =  -c2;
  41.     var y_c2:Number =  -c2s, x_c1:Number =  -c1, y_c1:Number =  -c1s
  42.     g.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);
  43.     g.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);
  44.     g.curveTo(x_r, c1s, x_r, 0);
  45.     if (!isHalf){
  46.      g.curveTo(x_r,y_c1,x_c2,y_c2);
  47.      g.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);
  48.      g.curveTo(r,y_c1,r,0);
  49.     }
  50. }

Also posted in Math, Vector, misc, motion, pixel manipulation | Tagged , , , | 5 Comments

JS Sketch Experiment

Actionscript:
  1. [SWF(width=950, height=600)]
  2. with (graphics) beginFill(0xefefef), drawRect(0,0,stage.stageWidth, stage.stageHeight);
  3. var btn:Sprite = Sprite(addChild(new Sprite()));
  4. with (btn.graphics) beginFill(0x666666), drawRect(0,0,100,20);
  5. with(btn)  x=320, y=430, buttonMode = true;
  6. btn.addEventListener(MouseEvent.ROLL_OVER, function():void{
  7.   with(btn.graphics) clear(), beginFill(0x222222), drawRect(0,0,100,20);
  8. });
  9. btn.addEventListener(MouseEvent.ROLL_OUT, function():void{
  10.   with(btn.graphics) clear(), beginFill(0x666666), drawRect(0,0,100,20);
  11. });
  12. btn.addEventListener(MouseEvent.CLICK, function():void{
  13.     var res:*= ExternalInterface.call("function(){ plot=[]; colors=[]; " + txt.text + " return {plot:plot, colors:colors};}");
  14.     render((res == null) ? {plot:[], colors:[]} : res);
  15. });
  16.  
  17. var v:Shape = Shape(addChild(new Shape()));
  18. v.x = 700;
  19. v.y = 220;
  20. function render(obj:Object):void{
  21.     var plot:Array = obj.plot;
  22.     var colors:Array = obj.colors;
  23.     var leng:int = plot.length;
  24.     v.graphics.clear();
  25.     var inc:int = 0;
  26.     v.graphics.moveTo(plot[0], plot[1]);
  27.     for (var i:int = 2; i<leng; i+=2){
  28.         v.graphics.lineStyle(0,colors[inc++]);
  29.         v.graphics.lineTo(plot[i], plot[i + 1]);
  30.     }
  31. }
  32.  
  33.  
  34. var submit:TextField = TextField(btn.addChild(new TextField()));
  35. submit.defaultTextFormat = new TextFormat("_sans", 12);
  36. with(submit) textColor=0xFFFFFF, width=100, autoSize="center";
  37. with(submit) mouseEnabled = false,  text="submit";
  38.  
  39. var txt:TextField = TextField(addChild(new TextField()));
  40. with(txt) x = y = 20, type = "input", multiline=true;
  41. with(txt) width = 400, height = 400, border = true, background = 0xFFFFFF;
  42. txt.defaultTextFormat = new TextFormat("Monaco", 12);
  43. txt.text = "enter text";
  44. txt.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
  45. function onDown(evt:MouseEvent):void{
  46.     txt.text = "";
  47.     txt.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
  48. }

This snippet is a mini code editor that allows the user to write javascript into a textfield - the javascript is then run using external interface. Optionally the javascript code can populate two arrays (plot and colors). If these arrays are populated flash, will render the data in each array using the Graphics class.

Have a look at the demo:

If you do something nice with this... post your javascript snippet as a comment and I'll add it to the JS Sketch page...

Also posted in Math, dynamic, external data, functions | Tagged , , , | Leave a comment

Seamless Vector Texture

Actionscript:
  1. [SWF(width = 800, height = 450, backgroundColor=0xFFFFFF)]
  2.  
  3. graphics.beginFill(0xDEDEDE);
  4. graphics.drawRect(0,0,400,400);
  5.  
  6. var container:Sprite = Sprite(addChild(new Sprite()));
  7. draggable(rx(), ry(), 0x112233, 20);
  8. draggable(rx(), ry(), 0x2266FF, 15);
  9. draggable(rx(), ry(), 0x003299, 50, "rect");
  10. draggable(rx(), ry(), 0xFFFFFF, 30);
  11. draggable(rx(), ry(), 0x00CCFF, 15);
  12. draggable(rx(), ry(), 0x1188CC, 50, "rect");
  13.  
  14. function rx():Number{
  15.     return Math.random() * 200 + 100;
  16. }
  17. function ry():Number{
  18.     return Math.random() * 200 + 100;
  19. }
  20.  
  21. var guides:Shape = Shape(addChild(new Shape()));
  22. with(guides.graphics){
  23.        lineStyle(0, 0x666666)
  24.        moveTo(200,0);
  25.        lineTo(200,400);
  26.        moveTo(0, 200);
  27.        lineTo(400, 200);
  28.        moveTo(400,0);
  29.        lineTo(400,400);
  30.        moveTo(0,400);
  31.        lineTo(800,400);
  32. }
  33.  
  34. var canvas:BitmapData = new BitmapData(200,200,true, 0x00000000);
  35. var tex:BitmapData = new BitmapData(800,800,false, 0xFFFFFF);
  36.  
  37. var frame:Bitmap = Bitmap(addChild(new Bitmap(tex, "auto", true)));
  38. frame.x = 401;
  39. frame.scaleX = frame.scaleY = 0.5;
  40.  
  41. var pnt:Point = new Point();
  42. var m:Matrix = new Matrix();
  43.  
  44. addEventListener(Event.ENTER_FRAME, onLoop);
  45. function onLoop(evt:Event):void {
  46.     canvas.fillRect(canvas.rect, 0x00000000);
  47.     canvas.draw(container);
  48.     drawQuadrant(-200,0)
  49.     drawQuadrant(-400,0)
  50.     drawQuadrant(0,-200)
  51.     drawQuadrant(-200,-200);
  52.     tex.fillRect(tex.rect, 0xFFFFFF);
  53.     for (var i:int = 0; i<16; i++){
  54.         var px:Number = i % 4;
  55.         var py:Number = int(i / 4);
  56.         pnt.x = px * 200;
  57.         pnt.y = py * 200;
  58.         tex.copyPixels(canvas, canvas.rect, pnt, null, null, true);
  59.        }
  60. }
  61.  
  62. function drawQuadrant(x:Number, y:Number):void{
  63.      m.tx = x;
  64.      m.ty = y;
  65.      canvas.draw(container, m);
  66. }
  67.  
  68. // draggable Sprite
  69. function draggable(xp:Number, yp:Number, col:uint = 0xFF0000,
  70. size:Number=4, type:String="circle"):Sprite {
  71.        var s:Sprite = Sprite(container.addChild(new Sprite()));
  72.        s.x = xp;
  73.        s.y = yp;
  74.        with(s.graphics){
  75.                beginFill(col)
  76.                if(type == "circle"){
  77.                  drawCircle(size,size,size);
  78.                }else if(type == "rect"){
  79.                  drawRect(0,0,size, size);
  80.                }
  81.        }
  82.        s.buttonMode = true;
  83.        s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
  84.        return s;
  85. }
  86. function onDrag(evt:MouseEvent):void {
  87.     var w:Number = evt.currentTarget.width;
  88.     var h:Number = evt.currentTarget.height;
  89.     evt.currentTarget.startDrag(false, new Rectangle(0,0,400 - w, 400 - h));
  90. }
  91. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  92. function onUp(evt:MouseEvent):void{ stopDrag() }

This snippet creates a seamless texture from a few draggable vector shapes. BitmapData is used to combine four quadrants into one seamless tile.


Have a look at the swf

I created this snippet as a sketch for a new feature for my command drawing project.

Also posted in BitmapData, misc | Tagged , , | 4 Comments