使用bitmapData简单实现涂鸦中的橡皮功能(as3.0)

  • 作者:l4cd 来源:l4cd.net 文章点击数:

    最近公司粉忙的~很久没来更新了~昨天朋友问起了涂鸦中的橡皮功能,网上查了一些资料,发现都写得比较复杂。后来了解了一下bitmapData的draw,发现如果重复draw的时候,选择特定的“混合模式”,即可实现清除功能。最新draw进去的内容会与之前的内容混合,重复的地方将会去掉。

先看看代码:

import flash.display.BitmapData;   
import flash.geom.Rectangle;   
import flash.geom.Matrix;   
import flash.geom.ColorTransform;   
   
//建立显示mc   
this.createEmptyMovieClip("main", this.getNextHighestDepth());   
main.createEmptyMovieClip("mc", main.getNextHighestDepth());   
main.createEmptyMovieClip("show", main.getNextHighestDepth());   
var content:BitmapData = new BitmapData(550,440,true,0x00FFFFFF);   
main.show.attachBitmap(content,0)   
   
//默认选中画笔   
var action:Number = 0   
txt.text = "当前选中:画笔"   
mc_move.onPress = goDraw   
   
//选中画笔   
a.onPress = function(){   
    action = 0   
    txt.text = "当前选中:画笔"   
    main.mc._visible = true   
    mc_move.onPress = goDraw   
}   
   
//选中橡皮   
b.onPress = function(){   
    action = 1   
    txt.text = "当前选中:橡皮"   
    main.mc._visible = false   
    mc_move.onPress = goDraw   
}   
   
//放开鼠标   
mc_move.onRelease = function(){   
    delete onMouseMove   
    content.draw(main,new Matrix(),new ColorTransform(),1,new Rectangle(0,0,550,400))   
    main.mc.clear()   
    break;   
}   
mc_move.onReleaseOutside = mc_move.onRelease   
   
//画线   
function goDraw(){   
    main.mc.lineStyle(20)   
    main.mc.moveTo(_xmouse,_ymouse)   
    onMouseMove = function(){   
        main.mc.lineTo(_xmouse,_ymouse)   
        updateAfterEvent()   
        if(action==1){   
            //主要的代码,在使用橡皮的时候(action==1),draw的时候,"混合模式"选择12(第四个参数)   
            content.draw(main.mc,new Matrix(),new ColorTransform(),12)   
        }   
    }   
}

试试效果:


点此在新窗口浏览图片 [点击浏览该文件:2008070817914841.rar]

Tags:flash bitmapData 涂鸦 橡皮 as3.0
  • 相关文章

Copyright 2006-2008 Powered by Noheart.NET无心人网络 All Rights Reserved.

QQ:89232083 E-Mail:leijian212@163.com

豫ICP备08004854号