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;
}
}
}

[ActionScript] AS3 涂鸦的擦除和撤销功能的更多相关文章

  1. [ActionScript 3.0] AS3.0 涂鸦及擦除功能,撤销重做步骤记录实例

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMo ...

  2. as3.0划线带撤销功能

    package com{ import flash.display.MovieClip; import flash.display.SimpleButton; import flash.events. ...

  3. Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系

    转自zrong's blog:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的 ...

  4. [ActionScript] AS3代码实现渐变遮罩效果

    import flash.display.Shape; import flash.display.GradientType; import flash.geom.Matrix; import flas ...

  5. [ActionScript] AS3代码实现曝光过度效果

    package { import flash.display.Loader; import flash.display.SimpleButton; import flash.display.Sprit ...

  6. [ActionScript] AS3 绘制虚线

    import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; function dra ...

  7. [ActionScript] AS3解决html与flash鼠标滚轮冲突的问题

    JS端: <script type="text/javascript"> <!-- var winWidth = 0; var winHeight = 0; va ...

  8. [ActionScript] AS3利用SWFObject与JS通信

    首先介绍SWFObject的用法: swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flash ...

  9. js简易计算器底层运算逻辑(带撤销功能)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

随机推荐

  1. python_Day1_基础知识开篇

    一.python安装 1)windows上同时安装python2.0和python3.0配置 (1)在python官网下载windows版本python2.0和python3.0安装包 官网地址:ht ...

  2. SqlServer中日期和时间数据类型及函数 【转】

    来源:http://blog.csdn.net/royalwzy/article/details/6446075 日期和时间数据类型 下表列出了 Transact-SQL 的日期和时间数据类型. 数据 ...

  3. Openjudge计算概论——数组逆序重放【递归练习】

    /*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...

  4. html初学(二)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <table bor ...

  5. placeholder兼容方法(兼容IE8以上浏览器)

    //placeholder兼容方法(兼容IE8以上浏览器) var JPlaceHolder = { //检测 _check: function () { return 'placeholder' i ...

  6. 通过新浪ip地址库获得用户的省份、城市等信息

    <script src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script> ...

  7. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  8. linux ubuntu系统下,adb不是内部命令 (如何才能让adb命令可以使用)

    linux ubuntu系统下,adb不是内部命令 原文地址 linux ubuntu系统下,adb不是内部命令 解决方法: 1.sudo gedit ~/.bashrc 2.将下面的两句加到上面打开 ...

  9. Linux下访问其他机器的共享

    1.如何查看其他机器上的共享列表? 解答:使用smbclient 客户端,在Linux机器上可以用来查看服务器上的共享资源,也可以向ftp一样,用户可以登陆samba服务器,也可以上传put和下载ge ...

  10. Struts2 - Rest(1)

    Struts2提供了一个restful的插件:struts2-rest-plugin-2.3.16.1.jar 这个插件可以把Struts2当做restful来使用,不过它的rest功能目前来说有点“ ...