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

 package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize; [SWF(width="800",height="600")]
public class DrawToUndo extends Sprite
{
private var _undoBtn:Sprite;
private var _redoBtn:Sprite;
private var _eraseBtn:Sprite;
private var _drawBtn:Sprite;
private var _canvas:Sprite;
private var _bitmap:Bitmap;
private var _bitmapData:BitmapData;
private var _prevBmpd:BitmapData;
private var _line:Shape;
private var _thickness:Number=5;
private var _bmpdArr:Array=[];//BitmapData数组
private var _blendMode:String = BlendMode.NORMAL;
private var _stepID:int=-1;
public function DrawToUndo()
{
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_canvas = new Sprite();
_canvas.graphics.beginFill(0,0.1);
_canvas.graphics.drawRect(0,0,700,600);
_canvas.graphics.endFill();
_undoBtn = creatButton(75,25,0xeeeeee,"撤销") as Sprite;
_redoBtn = creatButton(75,25,0xeeeeee,"重做") as Sprite;
_eraseBtn = creatButton(75,25,0xeeeeee,"擦除") as Sprite;
_drawBtn = creatButton(75,25,0xeeeeee,"涂鸦") as Sprite;
_bitmap = new Bitmap();
_bitmapData = new BitmapData(700,600,true,0);
_bitmap.bitmapData = _bitmapData;
_line = new Shape(); addToDisplay(_undoBtn,this,710,10);
addToDisplay(_redoBtn,this,710,45);
addToDisplay(_eraseBtn,this,710,80);
addToDisplay(_drawBtn,this,710,115);
addToDisplay(_canvas,this);
addToDisplay(_bitmap,_canvas);
addToDisplay(_line,_canvas,0,0,1,false);
initEventListener();
}
private function initEventListener():void
{
_undoBtn.addEventListener(MouseEvent.CLICK,onUndo);
_redoBtn.addEventListener(MouseEvent.CLICK,onRedo);
_eraseBtn.addEventListener(MouseEvent.CLICK,onErase);
_drawBtn.addEventListener(MouseEvent.CLICK,onDraw);
_canvas.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
}
private function onDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
_line.graphics.lineStyle(_thickness,0xff00ff);
_line.graphics.moveTo(_canvas.mouseX,_canvas.mouseY);
while(_stepID<_bmpdArr.length-1){
_bmpdArr.pop();
if(_stepID!=-1) _prevBmpd = _bmpdArr[_stepID];
}
if(_prevBmpd){
var bmpd:BitmapData = _prevBmpd.clone();
}else {
bmpd = new BitmapData(700,600,true,0);
}
_stepID++;
_bitmap.bitmapData = bmpd;
_prevBmpd = bmpd;
_bmpdArr.push(bmpd); function onMove(e:MouseEvent):void
{
_line.graphics.lineTo(_canvas.mouseX,_canvas.mouseY);
bmpd.draw(_line,null,null,_blendMode,null,true);
}
function onUp(e:MouseEvent):void
{
_line.graphics.clear();
stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
}
}
private function onUndo(e:MouseEvent):void
{
if(_stepID>0){
_stepID--;
_bitmap.bitmapData = _bmpdArr[_stepID];
//_prevBmpd = _bmpdArr[_stepID];
}else{
_prevBmpd = null;
_bitmap.bitmapData = null;
_stepID=-1;
} }
private function onRedo(e:MouseEvent):void
{
if(_stepID<_bmpdArr.length-1){
_stepID++;
_bitmap.bitmapData = _bmpdArr[_stepID];
//_prevBmpd = _bmpdArr[_stepID];
}
}
private function onErase(e:MouseEvent):void
{
_blendMode = BlendMode.ERASE;
_thickness = 10;//擦除的粗细
}
private function onDraw(e:MouseEvent):void
{
_blendMode = BlendMode.NORMAL;
_thickness = 5;//涂鸦的粗细
}
private function creatButton(w:Number,h:Number,color:uint,text:String=""):Sprite
{
var button:Sprite = new Sprite();
button.graphics.beginFill(color,1);
button.graphics.drawRect(0,0,w,h);
button.graphics.endFill();
var t:TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;
t.selectable = false;
t.text = text;
button.addChild(t);
t.x = (button.width-t.width)*.5;
t.y = (button.height-t.height)*.5;
return button;
}
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
{
parent.addChild(target);
target.x = x;
target.y = y;
target.alpha = alpha;
target.visible = visible;
target.scaleX = scaleX;
target.scaleY = scaleY;
}
}
}
上一篇:ios9基础知识总结(一)


下一篇:JavaScript的for循环编写九九乘法表