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. 20150906VS小知识

    .sln :解决方案管理文件.caproj:项目管理文件.cs:程序源代码文件项目文件目录下有个bin文件夹,里面的debug文件夹,里面存放生成后的程序. //注释一行/* */ 注释一段 alt ...

  2. 如何用ABBYY把PDF如何转换成HTML

    将PDF转换成HTML网页格式,是快速打造专业级网站的方法之一.当用户找到了非常详实的PDF资料,打算将之制作成为网页格式时,如果重新开发往往需要耗费大量的时间,可是又不知道怎么样才可以将PDF文件转 ...

  3. Open vSwitch

    https://github.com/openvswitch/ovs/blob/master/INSTALL.RHEL.md

  4. 关于Schema设计规范及SQL使用建议

    1.所有的InnoDB表都设计一个无业务用途的自增列做主键,对于绝大多数场景都是如此,真正纯只读用InnoDB表的并不多,真如此的话还不如用TokuDB来得划算: 2.字段长度满足需求前提下,尽可能选 ...

  5. Blitz Templates介绍

    Blitz Templates Blitz Templates-应用于大型互联网项目的非常强大非常快的模板引擎.   下载: sourceforge, 源代码 主页, win32 二进制文件, 其他语 ...

  6. 支付宝客户端支付配置RSA公钥的问题错误,导致收不到回发通知

    没收到通知的原因是你们的商户公钥上传地址弄错了,应该上传到合作伙伴管理,您上传到无线wap哪里了,把您的公钥,从无线wap哪里复制贴到合作伙伴管理即可

  7. 【NCDC数据】获取 hadoop权威指南3中的NCDC数据

    vi getNcdcBigData.sh 内容如下: #!/bin/bash for i in {1901..2014} do cd /home/xxxx/hapood/ncdc wget --exe ...

  8. Jedis编程设计:连接池

        Jedis作为redis的最佳客户端,它提供了连接池的特性,"连接池"在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apa ...

  9. WINDOWS黑客基础(6):查看文件里面的导入表

    int main(void) { HANDLE hFile = CreateFile("D:\\Shipyard.exe", GENERIC_READ, FILE_SHARE_RE ...

  10. 求助:为什么我用360浏览器和UC浏览器打不开JAVA中的index.html文件? 一打开就显示浏览器首界页

    如下图,在oracle官网下载了一个JAVA的API文档,双击index.html时打开是浏览器的首页,不知道为什么?请问怎样才能以chm文档格式显示?