/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import flash.events.MouseEvent; import flash.display.BitmapData; import flash.display.Bitmap; import flash.display.GradientType; // import flash.geom.Matrix; // var md:Boolean = false; // var event_spr:Sprite = new Sprite(); addChild (event_spr); // var area_width:Number = event_spr.stage.stageWidth; var area_height:Number = event_spr.stage.stageHeight-32; // var fillType:String = GradientType.LINEAR; var colors:Array = [0xFF0000, 0x00FF00, 0x0000ff]; var alphas:Array = [1, 1, 1]; var ratios:Array = [0, 128, 255]; var spreadMethod:String = SpreadMethod.PAD; var matrix:Matrix = new Matrix(); matrix.createGradientBox (area_width, area_height, 1, 0, 0); // with (event_spr.graphics) { beginGradientFill (fillType,colors,alphas,ratios,matrix,spreadMethod); drawRect (0,0,area_width, area_height); endFill (); } // paint event event_spr.addEventListener (MouseEvent.MOUSE_DOWN, _onMouseDown); event_spr.addEventListener (MouseEvent.MOUSE_MOVE, _onMouseMove); event_spr.addEventListener (MouseEvent.MOUSE_UP, _onMouseUp); event_spr.addEventListener (MouseEvent.MOUSE_OUT, _onMouseUp); // var bmpd:BitmapData = new BitmapData(event_spr.width,event_spr.height,true,0); var bmp:Bitmap = new Bitmap(bmpd); addChild (bmp); // // shape temporanea var draw_shape:Shape = new Shape(); addChild (draw_shape); // // shape, non visibile, usata per la "cancellazione" var erase_shape:Shape = new Shape(); // function _onMouseDown (e:MouseEvent):void { debug ("_onMouseDown"); draw_shape.graphics.lineStyle (10, 0xffffff, 1); erase_shape.graphics.lineStyle (20, 0xffffff, 1); draw_shape.graphics.moveTo (e.localX,e.localY); erase_shape.graphics.moveTo (e.localX,e.localY); md = true; } // function _onMouseUp (e:MouseEvent):void { md = false; bmp.bitmapData.draw (draw_shape); draw_shape.graphics.clear (); erase_shape.graphics.clear (); } // function _onMouseMove (e:MouseEvent):void { debug ("_onMouseMove"); if (md && !e.ctrlKey) { draw_shape.graphics.lineTo (e.localX,e.localY); } else if (md && e.ctrlKey) { erase_shape.graphics.lineTo (e.localX,e.localY); bmp.bitmapData.draw (erase_shape,null,null,"erase"); } } // function debug (v:String):void { var d:Date = new Date(); trace (d.getMinutes()+":"+d.getSeconds()+":"+d.getMilliseconds()+": "+v); }
URL: http://www.undolog.com/2008/02/11/painting-flash-cs3-real-time-erase-tool/