As explained in Flash Actionscript contest: erase tool you can not "erase" a particular area of a Sprite, MovieClip, or Shape on which lines or rectangles were drawn using the graphics cursor. There is, in fact, the only method clear() , however, has no effect on the entire area of the object. The solution to this problem lies in the possibility of using the Bitmap and BitmapData objects. As we shall see we can directly access and manipulate bitmap data in order to "erase" strokes with a real instrument "eraser".
Present two different methods to achieve the "gate". per esempio. The first "draw" (delete) data directly into the BitmapData, using the method fillRect() - can also be used setPixel() for example. The second method, the one I prefer, uses the methods of mergers (blendMode).
First of all let's see what kind of organization is minimally necessary to achieve a simple Flash Painter. The scheme presented below applies to both the proposals of the "gate":

I created three layers: the first, MovieClip or Sprite, background and work the event handler (MouseDown, MouseMove and MouseUp). The second, the Bitmap is our main layers, one that will actually drawn graphics will be applied and on which the "gate". , ecc…). The third and last layer, the Shape, addresses two issues: the first is that it allows to directly use the functions provided by the graphics cursor (like lineStyle , drawRect() , etc ...). It also increases the performance during the tracking chart, as explained in detail below.
METHOD 1 - Direct access to the bitmap data
) dalla Shape (layer 3) alla Bitmap (layer 2). This "sample" using the method fillRect() directly on the BitmapData (layer 2) after the design has been copied (using the method draw() ) from Shape (layer 3) to Bitmap (layer 2).
To understand how it works here is the same movie with the decomposed layer and pseudo-3D - drawn in colored box - Source :
As you can see you draw on the layer 3, the Shape. When taking the MouseUp event of the content is copied to the Bitmap Shape (and therefore the data is in BitmapData) and then the Shape is clean ( clear() ).
Note: this last point, the clear() on the Shape, is not to be underestimated. Some examples of the painter in Flash (almost all lack a true "gate") draw directly on a MovieClip, or Shape, maintaining data on the latter drawn. After a while 'you draw the system slows down, as the area "vector" of the MovieClip increases. In the method presented by me this problem is solved in that the data vector of Shape, when they are copied to the Bitmap are tasformati in bits and the Shape will be deleted, freeing up memory and return it to the design flow!
Holding down the Ctrl (control) is "deleted" directly through the BitmapData fillRect() . This is possible because fillRect() , like other methods that act on BitmapData, allow you to set the "color" by 0xARGB, where A is the alpha value, transparency.
The code is as follows - Source :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | . MouseEvent ; import flash.events. MouseEvent ; . BitmapData ; import flash.display. BitmapData ; . Bitmap ; import flash.display. Bitmaps ; . GradientType ; import flash.display. GradientType ; / / . Matrix ; flash.geom imports. Matrix ; / / Boolean = false ; md var: Boolean = false; / / Sprite = new Sprite ( ) ; var event_spr: Sprite = new Sprite (); event_spr ) ; addChild (event_spr); / / Number = event_spr . stage . stageWidth ; var area_width: Number = event_spr. internships. stageWidth; Number = event_spr . stage . stageHeight - 32 ; var area_height: Number = event_spr. internships. stageHeight - 32; / / String = GradientType . LINEAR ; fillType var: String = GradientType . LINEAR; : Array = [ 0xFF0000 , 0x00FF00 , 0x0000ff ] ; var colors: Array = [0xFF0000, 0x00FF00, 0x0000FF]; : Array = [ 1 , 1 , 1 ] ; var alphas: Array = [1, 1, 1]; : Array = [ 0 , 128 , 255 ] ; var ratios: Array = [0, 128, 255]; String = SpreadMethod . PAD ; spreadMethod var: String = SpreadMethod . PAD; : Matrix = new Matrix ( ) ; var matrix: Matrix = new Matrix (); createGradientBox ( area_width , area_height , 1 , 0 , 0 ) ; matrix. createGradientBox (area_width, area_height, 1, 0, 0); / / event_spr . graphics ) { with (event_spr. graphics) { fillType , colors , alphas , ratios , matrix , spreadMethod ) ; beginGradientFill (fillType, colors, alphas, ratios, matrix, spreadMethod); 0 , 0 , area_width , area_height ) ; drawRect (0, 0, area_width, area_height); ; endFill (); } / / Paint event ( MouseEvent . MOUSE_DOWN , _onMouseDown ) ; event_spr. addEventListener ( MouseEvent . MOUSE_DOWN, _onMouseDown); ( MouseEvent . MOUSE_MOVE , _onMouseMove ) ; event_spr. addEventListener ( MouseEvent . MOUSE_MOVE, _onMouseMove); ( MouseEvent . MOUSE_UP , _onMouseUp ) ; event_spr. addEventListener ( MouseEvent . MOUSE_UP, _onMouseUp); ( MouseEvent . MOUSE_OUT , _onMouseUp ) ; event_spr. addEventListener ( MouseEvent . MOUSE_OUT, _onMouseUp); / / BitmapData = new BitmapData ( event_spr . width , event_spr . height , true , 0 ) ; var bmpd: BitmapData = new BitmapData (event_spr. width, event_spr. height, true, 0); Bitmap = new Bitmap ( bmpd ) ; var bmp: Bitmap = new Bitmap (bmpd); bmp ) ; addChild (bmp); / / Temporary shape Shape = new Shape ( ) ; var draw_shape: Shape = new Shape (); draw_shape ) ; addChild (draw_shape); / / e : MouseEvent ) : void { _onMouseDown function (e: MouseEvent ): void { ) ; debug ("_onMouseDown"); : uint = 0xffffff ; var c: uint = 0xffffff; . lineStyle ( 10 , c , 1 ) ; draw_shape. graphics. lineStyle (10, c, 1); . moveTo ( e . localX , e . localY ) ; draw_shape. graphics. moveTo (and. localX, and. localY); md = true; } / / e : MouseEvent ) : void { _onMouseUp function (e: MouseEvent ): void { md = false; . draw ( draw_shape ) ; bmp. bitmapData. draw (draw_shape); . clear ( ) ; draw_shape. graphics. clear (); } / / e : MouseEvent ) : void { _onMouseMove function (e: MouseEvent ): void { ) ; debug ("_onMouseMove"); md && ! e . ctrlKey ) { if (md & &! and. ctrlKey) { . lineTo ( e . localX , e . localY ) ; draw_shape. graphics. lineTo (and. localX, and. localY); ( md && e . ctrlKey ) { } Else if (md & & and. CtrlKey) { . fillRect ( new Rectangle ( e . target . mouseX - 10 , e . target . mouseY - 10 , 20 , 20 ) , 0 ) ; bmp. bitmapData. fillRect (new Rectangle (and. target. mouseX - 10, and. target. mouseY - 10, 20, 20), 0); } } / / v : String ) : void { function debug (v: String ): void { : Date = new Date ( ) ; var d: Date = new Date (); d . getMinutes ( ) + ":" + d . getSeconds ( ) + ":" + d . getMilliseconds ( ) + ": " + v ) ; trace (d. getMinutes () + "" + d. getSeconds () + "" + d. getMilliseconds () + "" + st); } |
Line 61, as shown in the code above, runs so when the Control key is pressed. As you can see this section of code draws directly into the BitmapData with a colored 0, 0 × 0000!
METHOD 2 - Use of Blends
This is probably the best method to use when it allows any kind of "gate". Unlike the previous method does not directly access the bitmap data BitmapData present, but take advantage of blending (blendMode) and copy the Bitmap using the method draw().
3D version - source :
. In exactly the same way that we copy the data from the Bitmap Shape, the "gate" is "simultaneously" by copying the data from the Bitmap Shape, but this time, setting the blendMode the method draw() to erase .
The code is as follows - Source :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | . MouseEvent ; import flash.events. MouseEvent ; . BitmapData ; import flash.display. BitmapData ; . Bitmap ; import flash.display. Bitmaps ; . GradientType ; import flash.display. GradientType ; / / . Matrix ; flash.geom imports. Matrix ; / / Boolean = false ; md var: Boolean = false; / / Sprite = new Sprite ( ) ; var event_spr: Sprite = new Sprite (); event_spr ) ; addChild (event_spr); / / Number = event_spr . stage . stageWidth ; var area_width: Number = event_spr. internships. stageWidth; Number = event_spr . stage . stageHeight - 32 ; var area_height: Number = event_spr. internships. stageHeight - 32; / / String = GradientType . LINEAR ; fillType var: String = GradientType . LINEAR; : Array = [ 0xFF0000 , 0x00FF00 , 0x0000ff ] ; var colors: Array = [0xFF0000, 0x00FF00, 0x0000FF]; : Array = [ 1 , 1 , 1 ] ; var alphas: Array = [1, 1, 1]; : Array = [ 0 , 128 , 255 ] ; var ratios: Array = [0, 128, 255]; String = SpreadMethod . PAD ; spreadMethod var: String = SpreadMethod . PAD; : Matrix = new Matrix ( ) ; var matrix: Matrix = new Matrix (); createGradientBox ( area_width , area_height , 1 , 0 , 0 ) ; matrix. createGradientBox (area_width, area_height, 1, 0, 0); / / event_spr . graphics ) { with (event_spr. graphics) { fillType , colors , alphas , ratios , matrix , spreadMethod ) ; beginGradientFill (fillType, colors, alphas, ratios, matrix, spreadMethod); 0 , 0 , area_width , area_height ) ; drawRect (0, 0, area_width, area_height); ; endFill (); } / / Paint event ( MouseEvent . MOUSE_DOWN , _onMouseDown ) ; event_spr. addEventListener ( MouseEvent . MOUSE_DOWN, _onMouseDown); ( MouseEvent . MOUSE_MOVE , _onMouseMove ) ; event_spr. addEventListener ( MouseEvent . MOUSE_MOVE, _onMouseMove); ( MouseEvent . MOUSE_UP , _onMouseUp ) ; event_spr. addEventListener ( MouseEvent . MOUSE_UP, _onMouseUp); ( MouseEvent . MOUSE_OUT , _onMouseUp ) ; event_spr. addEventListener ( MouseEvent . MOUSE_OUT, _onMouseUp); / / BitmapData = new BitmapData ( event_spr . width , event_spr . height , true , 0 ) ; var bmpd: BitmapData = new BitmapData (event_spr. width, event_spr. height, true, 0); Bitmap = new Bitmap ( bmpd ) ; var bmp: Bitmap = new Bitmap (bmpd); bmp ) ; addChild (bmp); / / Temporary shape Shape = new Shape ( ) ; var draw_shape: Shape = new Shape (); draw_shape ) ; addChild (draw_shape); / / e : MouseEvent ) : void { _onMouseDown function (e: MouseEvent ): void { ) ; debug ("_onMouseDown"); : uint = ( ! e . ctrlKey ? 0xffffff : 0x000000 ) ; var c: uint = (! and. ctrlKey? 0xffffff: 0x000000); . lineStyle ( 10 , c , 1 ) ; draw_shape. graphics. lineStyle (10, c, 1); . moveTo ( e . localX , e . localY ) ; draw_shape. graphics. moveTo (and. localX, and. localY); md = true; } / / e : MouseEvent ) : void { _onMouseUp function (e: MouseEvent ): void { md = false; . draw ( draw_shape , null , null , ( e . ctrlKey ? "erase" : "normal" ) ) ; bmp. bitmapData. draw (draw_shape, null, null, (and. ctrlKey? "erase" or "normal")); . clear ( ) ; draw_shape. graphics. clear (); } / / e : MouseEvent ) : void { _onMouseMove function (e: MouseEvent ): void { ) ; debug ("_onMouseMove"); md ) { if (md) { . lineTo ( e . localX , e . localY ) ; draw_shape. graphics. lineTo (and. localX, and. localY); } } / / v : String ) : void { function debug (v: String ): void { : Date = new Date ( ) ; var d: Date = new Date (); d . getMinutes ( ) + ":" + d . getSeconds ( ) + ":" + d . getMilliseconds ( ) + ": " + v ) ; trace (d. getMinutes () + "" + d. getSeconds () + "" + d. getMilliseconds () + "" + st); } |
This code is not very different from the previous year. The real difference is in row 52! This determines the "blending mode" when copying from the Shape Bitmap when the Control key is pressed is used the blending erase .
That's all! We'll come back as soon as the bitmap and the differences between MovieClip, Sprite, Shape and Bitmap objects ... all visual!










[...] How to save images in Flash CS3 Tags: ActionScript 3.0, Bitmap, BitmapData, Flash CS3, GD, GD Library, Internet, PHP, Flash Save Image, Development, Flash CS3 TutorialsCon the use of Bitmap is so improved that is immediately want to create a small Paint. We already saw how to paint a little (see Creating a Paint € ™ application in Flash CS3 and Painter: simple application for drawing in Flash CS3 Pro) can support a true "eraser" - erase function, thanks to use Shape and Bitmap particular layer. Let's say that Flash (as opposed to Flex) it does not allow encoding (such as JPG or PNG) it automatically saves the Bitmap images. However you can get around it using a server-side scripting and the ability to send data in POST Flash. [...]
[...] Create a Paint in Flash CS3 [...]
Excellent solution, suitable for even the innnumerevoli solutions for Web graphics application.
@ John: Thanks! See also Painter: simple application for drawing in Flash Pro CS3