Painting Flash CS3: real time erase tool


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import flash.events.MouseEvent;
  2. import flash.display.BitmapData;
  3. import flash.display.Bitmap;
  4. import flash.display.GradientType;
  5. //
  6. import flash.geom.Matrix;
  7. //
  8. var md:Boolean = false;
  9. //
  10. var event_spr:Sprite = new Sprite();
  11. addChild (event_spr);
  12. //
  13. var area_width:Number = event_spr.stage.stageWidth;
  14. var area_height:Number = event_spr.stage.stageHeight-32;
  15. //
  16. var fillType:String = GradientType.LINEAR;
  17. var colors:Array = [0xFF0000, 0x00FF00, 0x0000ff];
  18. var alphas:Array = [1, 1, 1];
  19. var ratios:Array = [0, 128, 255];
  20. var spreadMethod:String = SpreadMethod.PAD;
  21. var matrix:Matrix = new Matrix();
  22. matrix.createGradientBox (area_width, area_height, 1, 0, 0);
  23. //
  24. with (event_spr.graphics) {
  25. beginGradientFill (fillType,colors,alphas,ratios,matrix,spreadMethod);
  26. drawRect (0,0,area_width, area_height);
  27. endFill ();
  28. }
  29. // paint event
  30. event_spr.addEventListener (MouseEvent.MOUSE_DOWN, _onMouseDown);
  31. event_spr.addEventListener (MouseEvent.MOUSE_MOVE, _onMouseMove);
  32. event_spr.addEventListener (MouseEvent.MOUSE_UP, _onMouseUp);
  33. event_spr.addEventListener (MouseEvent.MOUSE_OUT, _onMouseUp);
  34. //
  35. var bmpd:BitmapData = new BitmapData(event_spr.width,event_spr.height,true,0);
  36. var bmp:Bitmap = new Bitmap(bmpd);
  37. addChild (bmp);
  38. //
  39. // shape temporanea
  40. var draw_shape:Shape = new Shape();
  41. addChild (draw_shape);
  42. //
  43. // shape, non visibile, usata per la "cancellazione"
  44. var erase_shape:Shape = new Shape();
  45. //
  46. function _onMouseDown (e:MouseEvent):void {
  47. debug ("_onMouseDown");
  48. draw_shape.graphics.lineStyle (10, 0xffffff, 1);
  49. erase_shape.graphics.lineStyle (20, 0xffffff, 1);
  50. draw_shape.graphics.moveTo (e.localX,e.localY);
  51. erase_shape.graphics.moveTo (e.localX,e.localY);
  52. md = true;
  53. }
  54. //
  55. function _onMouseUp (e:MouseEvent):void {
  56. md = false;
  57. bmp.bitmapData.draw (draw_shape);
  58. draw_shape.graphics.clear ();
  59. erase_shape.graphics.clear ();
  60. }
  61. //
  62. function _onMouseMove (e:MouseEvent):void {
  63. debug ("_onMouseMove");
  64. if (md && !e.ctrlKey) {
  65. draw_shape.graphics.lineTo (e.localX,e.localY);
  66. } else if (md && e.ctrlKey) {
  67. erase_shape.graphics.lineTo (e.localX,e.localY);
  68. bmp.bitmapData.draw (erase_shape,null,null,"erase");
  69. }
  70. }
  71. //
  72. function debug (v:String):void {
  73. var d:Date = new Date();
  74. trace (d.getMinutes()+":"+d.getSeconds()+":"+d.getMilliseconds()+": "+v);
  75. }

URL: http://www.undolog.com/2008/02/11/painting-flash-cs3-real-time-erase-tool/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.