Flash怎么制作互动的橡皮刷
以前用Flash做橡皮刷的時候通常都是通過繪制底圖實現的,最近在發現其實還有一種更好的實現方法就是用bitmapdata的alpha通道。
只要在一張圖片上用draw畫一個透明度為零的圖片,就可以實現在draw的區域圖片透明了。
完成效果如下:
點擊選擇右邊的橡皮刷或者畫筆圖標,再用鼠標在畫面上拖動,看看會出現什么情況?
AS代碼如下:
//導入所需要的類
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
//初始點(0,0)
var base_point:Point = new Point(0, 0);
//初始區域(0,0,25,25)
var base_rectangle:Rectangle = new Rectangle(0, 0, 25, 25);
//導入庫中圖片
var bit1:BitmapData = BitmapData.loadBitmap("img1");
//定義org_bit:BitmapData用于恢復圖片
var org_bit:BitmapData = new BitmapData(mc._width, bit1.height, true, 0);
org_bit.draw(bit1);
//draw_bit拷貝org_bit用于涂鴉操作
var draw_bit:BitmapData = org_bit.clone();
//導入到舞臺中
_root.createEmptyMovieClip("draw_mc", 1);
draw_mc.attachBitmap(draw_bit, 1);
//定義橡皮刷erase_bit和筆刷redraw_bit argb為0(透明)
var erase_bit:BitmapData = new BitmapData(mc1._width, mc1._height, true, 0);
var redraw_bit:BitmapData = erase_bit.clone();
//橡皮刷erase_bit填充為白色,這里注意a必須不為0 rbg為FFFFFF
erase_bit.fillRect(erase_bit.rectangle, 0xFFFFFFFF);
//定義橡皮刷erase_bit和筆刷redraw_bit形狀 注意mc1必須為黑色 你也可以嘗試用別的顏色看看效果慢慢體會吧
erase_bit.draw(mc1);
redraw_bit.draw(mc1);
//交換erase_bit r通道和a通道數值 所以a通道數值為00
erase_bit.copyChannel(erase_bit, erase_bit.rectangle, new Point(0, 0), 1, 8);
//保存當前使用的工具
var tools:String;
//點擊筆刷工具
mc_bursh.onRelease = function()
{
this.gotoAndStop(2);
mc_earse.gotoAndStop(1);
tools = "bursh";
};
//點擊橡皮刷工具
mc_earse.onRelease = function()
{
this.gotoAndStop(2);
mc_bursh.gotoAndStop(1);
tools = "easre";
};
//在draw_bit上涂鴉
draw_mc.onPress = function()
{
trace(tools);
if (tools == "bursh")
{
this.onMouseMove = bursh_pic;
}
if (tools == "easre")
{
this.onMouseMove = earse_pic;
}
};
//停止涂鴉
draw_mc.onRelease = function()
{
delete this.onMouseMove;
};
//橡皮刷工具
function earse_pic()
{
var now_rect:Rectangle = new Rectangle(_xmouse, _ymouse, _xmouse base_rectangle.width, _ymouse base_rectangle.height);
trace(now_rect);
//在draw_bit上使用copyPixels alpha為false 透明區域透明 不透明區域保持原色
draw_bit.copyPixels(draw_bit, now_rect, new Point(_xmouse, _ymouse), erase_bit, new Point(0, 0), false);
updateAfterEvent();
}
//筆刷工具
function bursh_pic()
{
var now_rect:Rectangle = new Rectangle(_xmouse, _ymouse, _xmouse base_rectangle.width, _ymouse base_rectangle.height);
trace(now_rect);
//在org_bit上使用copyPixels alpha為true 則筆刷工具只有不透明的地方起作用
draw_bit.copyPixels(org_bit, now_rect, new Point(_xmouse, _ymouse), redraw_bit, new Point(0, 0), true);
updateAfterEvent();
}
//移動背景圖觀察效果
mc.onPress = function()
{
this.startDrag();
};
mc.onRelease = function()
{
this.stopDrag();
};
總結
以上是生活随笔為你收集整理的Flash怎么制作互动的橡皮刷的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Customing 维护中TR弹出逻辑
- 下一篇: 瓜大无人船使用踩坑记