Create a Paint in Flash CS3

Sunday, February 10, 2008

As stated in Flash Actionscript Contest: erase tool you can not "erase" a particular area of a Sprite, MovieClip, and Shape on which were traced lines or rectangles using the pointer graphics. There is, in fact, the only method clear() which, however, has no effect on the whole area of our object. The solution to the problem lies in the possibility of using the Bitmap and BitmapData objects. As we shall see we can access and manipulate bitmap data directly in order to "erase" the traits with a real instrument "eraser."

I will present two different methods for achieving the "gate". The first "draw" (delete) data directly into BitmapData, using the method fillRect() - you can also use setPixel() for example. The second method, the preferred one, uses the methods of mergers (blendMode).

First of all let's see what kind of organization is needed to achieve a minimal simple Painter in Flash. The outline presented below applies to both proposals of the "gate"

schema-bitmap

I created three layers: the first, MovieClip or Sprite, works as a manager and background events (MouseDown, MouseMove and MouseUp). The second, the bitmap is our main layer, the one that will contain the graphics actually designed and which will apply the "delete". The third and final layer, the Shape, addresses two issues: the first is being able to directly use the functions provided by the graphics pointer (as lineStyle drawRect() etc. ...). It also increases performance during tracking chart, as detailed below.

Method 1 - Direct access to bitmap data

This "sample" uses the method fillRect() directly on BitmapData (layer 2) after the design has been copied (using the method draw() from the Shape (layer 3) to Bitmap (layer 2).

Loading Flash Player ...

To understand how it works here is the same movie with layers and disordered in pseudo 3D - drawn in colored box - source:

Loading Flash Player ...

As you can see you draw on layer 3, the Shape. MouseUp event fires when the content is copied on the Shape Bitmap (and therefore the data are present in BitmapData) and then the Shape is clean clear()

Note: this last point, the clear() the Shape, not be underestimated. Some examples of the painter in Flash (almost all devoid of a true "gate") by drawing directly on a MovieClip or Shape, keeping the data on that draw. After a little 'you draw the system slows down, as the area "vector" of the MovieClip increases. In the method I have presented this problem is solved because the data vector of Shape, when they are copied to the Bitmap, tasformati are in bits and Shape is deleted, freeing up memory and return it to the design flow!

Holding down the Ctrl (control) is "deleted" directly via the BitmapData fillRect() This is possible because fillRect() like other methods that operate on a BitmapData, allow you to set the "color" through 0xARGB, where A is the alpha value, transparent.

The code is as follows - source:

ActionScript
  1. . MouseEvent ; import flash.events. MouseEvent;
  2. . BitmapData ; import flash.display. BitmapData;
  3. . Bitmap ; import flash.display. Bitmap;
  4. . GradientType ; import flash.display. GradientType;
  5. / /
  6. . Matrix ; import flash.geom. Matrix;
  7. / /
  8. Boolean = false ; var md: Boolean = false;
  9. / /
  10. Sprite = new Sprite ( ) ; var event_spr: Sprite = new Sprite ();
  11. event_spr ) ; addChild (event_spr);
  12. / /
  13. Number = event_spr . stage . stageWidth ; var area_width: Number = event_spr. stage. stageWidth;
  14. Number = event_spr . stage . stageHeight - 32 ; var area_height: Number = event_spr. stage. stageHeight - 32;
  15. / /
  16. String = GradientType . LINEAR ; var fillType: String = GradientType. Linearity;
  17. : Array = [ 0xFF0000 , 0x00FF00 , 0x0000ff ] ; var colors: Array = [0xFF0000, 0x00FF00, 0x0000ff];
  18. : Array = [ 1 , 1 , 1 ] ; var alphas: Array = [1, 1, 1];
  19. : Array = [ 0 , 128 , 255 ] ; var ratios: Array = [0, 128, 255];
  20. String = SpreadMethod . PAD ; var spreadMethod: String = SpreadMethod. PAD;
  21. : Matrix = new Matrix ( ) ; var matrix: Matrix = new Matrix ();
  22. createGradientBox ( area_width , area_height , 1 , 0 , 0 ) ; matrix. createGradientBox (area_width, area_height, 1, 0, 0);
  23. / /
  24. event_spr . graphics ) { with (event_spr. graphics) (
  25. fillType , colors , alphas , ratios , matrix , spreadMethod ) ; beginGradientFill (fillType, colors, alphas, ratios, matrix, spreadMethod);
  26. 0 , 0 , area_width , area_height ) ; drawRect (0, 0, area_width, area_height);
  27. ; endFill ();
  28. )
  29. / / Paint event
  30. ( MouseEvent . MOUSE_DOWN , _onMouseDown ) ; event_spr. addEventListener (MouseEvent. MOUSE_DOWN, _onMouseDown);
  31. ( MouseEvent . MOUSE_MOVE , _onMouseMove ) ; event_spr. addEventListener (MouseEvent. MOUSE_MOVE, _onMouseMove);
  32. ( MouseEvent . MOUSE_UP , _onMouseUp ) ; event_spr. addEventListener (MouseEvent. MOUSE_UP, _onMouseUp);
  33. ( MouseEvent . MOUSE_OUT , _onMouseUp ) ; event_spr. addEventListener (MouseEvent. MOUSE_OUT, _onMouseUp);
  34. / /
  35. BitmapData = new BitmapData ( event_spr . width , event_spr . height , true , 0 ) ; var bmpd: BitmapData = new BitmapData (event_spr. width, event_spr. height, true, 0);
  36. Bitmap = new Bitmap ( bmpd ) ; var bmp: Bitmap = new Bitmap (bmpd);
  37. bmp ) ; addChild (bmp);
  38. / / Temporary shape
  39. Shape = new Shape ( ) ; var draw_shape: Shape = new Shape ();
  40. draw_shape ) ; addChild (draw_shape);
  41. / /
  42. e : MouseEvent ) : void { _onMouseDown function (e: MouseEvent): void (
  43. ) ; debug ( "_onMouseDown");
  44. : uint = 0xffffff ; var c: uint = 0xffffff;
  45. . lineStyle ( 10 , c , 1 ) ; draw_shape. graphics. lineStyle (10, c, 1);
  46. . moveTo ( e . localX , e . localY ) ; draw_shape. graphics. moveTo (and. localX, and. localy);
  47. md = true;
  48. )
  49. / /
  50. e : MouseEvent ) : void { _onMouseUp function (e: MouseEvent): void (
  51. md = false;
  52. . draw ( draw_shape ) ; bmp. bitmapData. draw (draw_shape);
  53. . clear ( ) ; draw_shape. graphics. Clear ();
  54. )
  55. / /
  56. e : MouseEvent ) : void { _onMouseMove function (e: MouseEvent): void (
  57. ) ; debug ( "_onMouseMove");
  58. md && ! e . ctrlKey ) { if (md & &! and. ctrlKey) (
  59. . lineTo ( e . localX , e . localY ) ; draw_shape. graphics. lineTo (and. localX, and. localy);
  60. ( md && e . ctrlKey ) { ) Else if (md & & and. CtrlKey) (
  61. . fillRect ( new Rectangle ( e . target . mouseX - 10 , e . target . mouseY - 10 , 20 , 20 ) , 0 ) ; bmp. bitmapData. hg.fillRect (new Rectangle (and. target. mouseX - 10, and. target. mouseY - 10, 20, 20), 0);
  62. )
  63. )
  64. / /
  65. v : String ) : void { function debug (v: String): void (
  66. : Date = new Date ( ) ; var d: Date = new Date ();
  67. d . getMinutes ( ) + ":" + d . getSeconds ( ) + ":" + d . getMilliseconds ( ) + ": " + v ) ; trace (d. getMinutes () + ":" + d. getSeconds () + ":" + d. GetMilliseconds () + ":" + v);
  68. )

Line 61, as shown in the code above, it runs so long when the Control key is pressed. As you can see this section of code draws directly into the BitmapData with color 0, which is 0x0000!

METHOD 2 - Using Blending Modes

This is probably the best method to time lets you use any kind of "gate". Unlike the previous method does not directly access the bitmap data present in BitmapData, but exploit the blend (blendMode) and the copy of bitmaps using the method draw().

Loading Flash Player ...

3D version - source:

Loading Flash Player ...

Exactly the same way that we copy the data from the Bitmap Shape, the "gate" is "simultaneously" by copying the data from the Shape Bitmap but this time, setting the blendMode method draw() to erase

The code is as follows - source:

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

This code is not very dissimilar from the previous year. The real difference is in row 52! This determines the "blend mode" when you copy from the Shape Bitmap, and when the Control key is pressed is used the method of fusion erase
That's all! We talk more about the bitmap as soon as possible and the differences between MovieClip, Sprite, Shape and Bitmap ... All visual objects!

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

4 comments to "Create a Paint in Flash CS3"

  1. getAvatar 1.0
    March 31, 2008 Undolog.com »Blog Archive» How to save images in Flash CS3:

    [...] How to save images in Flash CS3 Tags: ActionScript 3.0, Bitmap, BitmapData, Flash CS3, GD, GD Library, Internet, PHP, Flash Save Image, Development, TutorialsCon Flash CS3 using the Bitmap is so improved that is immediately wanted to create a small Paint. We have already seen how to make a small Paint (see Creating a € ™ Paint application in Flash CS3 and Painter: simple application for drawing in Flash CS3 Pro) can support a true "gate" - erase function, through use particular layer Shape and Bitmap. Let's say that Flash (as opposed to Flex) will not allow encoding (JPG or PNG) will auto-saving of bitmap images. However, you can overcome this obstacle by using a server-side scripting and the ability to send data in POST Flash. [...]

  2. getAvatar 1.0
  3. getAvatar 1.0
    October 30, 2008 Gianni:

    Optimal solution, suitable for even innnumerevoli solutions relating to web application graphics.

  4. getAvatar 1.0

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL