码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript] AS3 涂鸦的擦除和撤销功能

时间:2015-09-04 14:08:09      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

  1 package
  2 {
  3     import flash.display.Bitmap;
  4     import flash.display.BitmapData;
  5     import flash.display.BlendMode;
  6     import flash.display.DisplayObject;
  7     import flash.display.DisplayObjectContainer;
  8     import flash.display.Shape;
  9     import flash.display.Sprite;
 10     import flash.events.Event;
 11     import flash.events.MouseEvent;
 12     import flash.text.TextField;
 13     import flash.text.TextFieldAutoSize;
 14 
 15     [SWF(width="800",height="600")]
 16     public class DrawToUndo extends Sprite
 17     {
 18         private var _undoBtn:Sprite;
 19         private var _redoBtn:Sprite;
 20         private var _eraseBtn:Sprite;
 21         private var _drawBtn:Sprite;
 22         private var _canvas:Sprite;
 23         private var _bitmap:Bitmap;
 24         private var _bitmapData:BitmapData;
 25         private var _prevBmpd:BitmapData;
 26         private var _line:Shape;
 27         private var _thickness:Number=5;
 28         private var _bmpdArr:Array=[];//BitmapData数组
 29         private var _blendMode:String = BlendMode.NORMAL;
 30         private var _stepID:int=-1;
 31         public function DrawToUndo()
 32         {
 33             if(stage) init();
 34             else addEventListener(Event.ADDED_TO_STAGE,init);
 35         }
 36         private function init(e:Event=null):void
 37         {
 38             removeEventListener(Event.ADDED_TO_STAGE, init);
 39             _canvas = new Sprite();
 40             _canvas.graphics.beginFill(0,0.1);
 41             _canvas.graphics.drawRect(0,0,700,600);
 42             _canvas.graphics.endFill();
 43             _undoBtn = creatButton(75,25,0xeeeeee,"撤销") as Sprite;
 44             _redoBtn = creatButton(75,25,0xeeeeee,"重做") as Sprite;
 45             _eraseBtn = creatButton(75,25,0xeeeeee,"擦除") as Sprite;
 46             _drawBtn = creatButton(75,25,0xeeeeee,"涂鸦") as Sprite;
 47             _bitmap = new Bitmap();
 48             _bitmapData = new BitmapData(700,600,true,0);
 49             _bitmap.bitmapData = _bitmapData;
 50             _line = new Shape();
 51             
 52             addToDisplay(_undoBtn,this,710,10);
 53             addToDisplay(_redoBtn,this,710,45);
 54             addToDisplay(_eraseBtn,this,710,80);
 55             addToDisplay(_drawBtn,this,710,115);
 56             addToDisplay(_canvas,this);
 57             addToDisplay(_bitmap,_canvas);
 58             addToDisplay(_line,_canvas,0,0,1,false);
 59             initEventListener();
 60         }
 61         private function initEventListener():void
 62         {
 63             _undoBtn.addEventListener(MouseEvent.CLICK,onUndo);
 64             _redoBtn.addEventListener(MouseEvent.CLICK,onRedo);
 65             _eraseBtn.addEventListener(MouseEvent.CLICK,onErase);
 66             _drawBtn.addEventListener(MouseEvent.CLICK,onDraw);
 67             _canvas.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
 68         }
 69         private function onDown(e:MouseEvent):void
 70         {
 71             stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
 72             stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
 73             _line.graphics.lineStyle(_thickness,0xff00ff);
 74             _line.graphics.moveTo(_canvas.mouseX,_canvas.mouseY);
 75             while(_stepID<_bmpdArr.length-1){
 76                 _bmpdArr.pop();
 77                 if(_stepID!=-1) _prevBmpd = _bmpdArr[_stepID];
 78             }
 79             if(_prevBmpd){
 80                 var bmpd:BitmapData = _prevBmpd.clone();
 81             }else {
 82                 bmpd = new BitmapData(700,600,true,0);
 83             }
 84             _stepID++;
 85             _bitmap.bitmapData = bmpd;
 86             _prevBmpd = bmpd;
 87             _bmpdArr.push(bmpd);
 88             
 89             function onMove(e:MouseEvent):void
 90             {
 91                 _line.graphics.lineTo(_canvas.mouseX,_canvas.mouseY);
 92                 bmpd.draw(_line,null,null,_blendMode,null,true);
 93             }
 94             function onUp(e:MouseEvent):void
 95             {
 96                 _line.graphics.clear();
 97                 stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
 98                 stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
 99             }
100         }
101         private function onUndo(e:MouseEvent):void
102         {
103             if(_stepID>0){
104                 _stepID--;
105                 _bitmap.bitmapData = _bmpdArr[_stepID];
106                 //_prevBmpd = _bmpdArr[_stepID];
107             }else{
108                 _prevBmpd = null;
109                 _bitmap.bitmapData = null;
110                 _stepID=-1;
111             }
112             
113         }
114         private function onRedo(e:MouseEvent):void
115         {
116             if(_stepID<_bmpdArr.length-1){
117                 _stepID++;
118                 _bitmap.bitmapData = _bmpdArr[_stepID];
119                 //_prevBmpd = _bmpdArr[_stepID];
120             }
121         }
122         private function onErase(e:MouseEvent):void
123         {
124             _blendMode = BlendMode.ERASE;
125             _thickness = 10;//擦除的粗细
126         }
127         private function onDraw(e:MouseEvent):void
128         {
129             _blendMode = BlendMode.NORMAL;
130             _thickness = 5;//涂鸦的粗细
131         }
132         private function creatButton(w:Number,h:Number,color:uint,text:String=""):Sprite
133         {
134             var button:Sprite = new Sprite();
135             button.graphics.beginFill(color,1);
136             button.graphics.drawRect(0,0,w,h);
137             button.graphics.endFill();
138             var t:TextField = new TextField();
139             t.autoSize = TextFieldAutoSize.LEFT;
140             t.selectable = false;
141             t.text = text;
142             button.addChild(t);
143             t.x = (button.width-t.width)*.5;
144             t.y = (button.height-t.height)*.5;
145             return button;
146         }
147         private function addToDisplay(target:DisplayObject,parent:DisplayObjectContainer,x:Number=0,y:Number=0,alpha:Number=1,visible:Boolean = true,scaleX:Number=1,scaleY:Number=1):void
148         {
149             parent.addChild(target);
150             target.x = x;
151             target.y = y;
152             target.alpha = alpha;
153             target.visible = visible;
154             target.scaleX = scaleX;
155             target.scaleY = scaleY;
156         }
157     }
158 }

 

[ActionScript] AS3 涂鸦的擦除和撤销功能

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4781577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!