Painting Flash CS3: real time erase tool
Monday, February 11, 2008 Starting from the codes provided in Create a Paint in Flash CS3, and performing small modifications, it can greatly improve the tool to "erase". Adding a Shape is not visible, you can use it as a "plan" to run the draw() in blend mode "erase". As shown in the example below, after you draw something, hold down the Ctrl key and the effect of "erasing" now appears in real time.
The code is as follows - source:
- / /
- / /
- / /
- event_spr ) ; addChild (event_spr);
- / /
- / /
- 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);
- bmp ) ; addChild (bmp);
- / /
- / / Temporary shape
- draw_shape ) ; addChild (draw_shape);
- / /
- / / Shape, not visible, used for the "cancellation"
- / /
- ) ; debug ( "_onMouseDown");
- . lineStyle ( 10 , 0xffffff , 1 ) ; draw_shape. graphics. lineStyle (10, 0xffffff, 1);
- . lineStyle ( 20 , 0xffffff , 1 ) ; erase_shape. graphics. lineStyle (20, 0xffffff, 1);
- . moveTo ( e . localX , e . localY ) ; draw_shape. graphics. moveTo (and. localX, and. localy);
- . moveTo ( e . localX , e . localY ) ; erase_shape. graphics. moveTo (and. localX, and. localy);
- md = true;
- )
- / /
- md = false;
- . draw ( draw_shape ) ; bmp. bitmapData. draw (draw_shape);
- . clear ( ) ; draw_shape. graphics. Clear ();
- . clear ( ) ; erase_shape. graphics. Clear ();
- )
- / /
- ) ; 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) (
- . lineTo ( e . localX , e . localY ) ; erase_shape. graphics. lineTo (and. localX, and. localy);
- . draw ( erase_shape , null , null , "erase" ) ; bmp. bitmapData. draw (erase_shape, null, null, "erase");
- )
- )
- / /
- d . getMinutes ( ) + ":" + d . getSeconds ( ) + ":" + d . getMilliseconds ( ) + ": " + v ) ; trace (d. getMinutes () + ":" + d. getSeconds () + ":" + d. GetMilliseconds () + ":" + v);
- )
You have added a new shape, erase_shape not visible (has not been made any addChild()
In the part of code that takes care of design has been introduced to control the Ctrl key and if pressed, is concerned precisely the Shape erase_shape and copied to the Bitmap:
- ....
- . lineTo ( e . localX , e . localY ) ; erase_shape. graphics. lineTo (and. localX, and. localy);
- . draw ( erase_shape , null , null , "erase" ) ; bmp. bitmapData. draw (erase_shape, null, null, "erase");













A lever mejora en la función y drenaje of the hizo a mí clase.
@ Greg: good!