我正在寻找一些关于如何在Actionscript中制作涂抹工具的代码。我发现的最好的是old entry here,,但我想其他更好的选择存在。
谢谢。
编辑: 假设不存在更好的片段。这是我现在拥有的代码:
import flash.events.Event;
[SWF(backgroundColor = 0xCCCCCC)]
var canvas:BitmapData = new BitmapData(400,200,true, 0xFFFFFFFF);
addChild(new Bitmap(canvas));
var loader:Loader = new Loader();
loader.load(new URLRequest("http://actionsnippet.com/wp-content/chair.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete(evt:Event):void{
canvas.draw(loader);
}
var brush:Shape = new Shape();
var diameter:Number = 40;
var radius:Number = diameter / 2;
var matrix:Matrix = new Matrix();
var smudgeOn:Boolean = false;
var brushAlpha:BitmapData = new BitmapData(diameter, diameter, true, 0x00000000);
drawRadial();
brushAlpha.draw(brush);
var xp:Number = 0, yp:Number = 0, px:Number = 0, py:Number = 0;
var dx:Number, dy:Number;
stage.addEventListener(MouseEvent.MOUSE_DOWN, turnOnSmudge);
stage.addEventListener(MouseEvent.MOUSE_UP, turnOffSmudge);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onLoop);
function onLoop(evt:Event):void {
xp = mouseX - radius;
yp = mouseY - radius;
dx = xp - px;
dy = yp - py;
if(smudgeOn){
canvas.copyPixels(canvas,
new Rectangle(px, py, diameter, diameter),
new Point(xp, yp), brushAlpha, new Point(0,0), true);
}
px = xp;
py = yp;
}
function turnOnSmudge(e:Event){
smudgeOn = true;
}
function turnOffSmudge(e:Event){
smudgeOn = false;
}
function drawRadial():void{
matrix.createGradientBox(diameter, diameter, 0, 0, 0);
with (brush.graphics){
beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0xFFFFFF], [1,0], [0, 255], matrix, SpreadMethod.PAD);
drawCircle(radius, radius, radius);
}
}
但是,如果长时间在同一个地方涂抹,则区域会变为黑色,而不是保留光标下方点的颜色。是否有更好的算法来创建一个不受上述代码限制的涂抹工具?
答案 0 :(得分:0)
你放了一个计时器并继续绘制你涂抹的区域,将它绘制在鼠标处于该计时器的那一刻。你可以应用一点模糊。您也可以使用污迹区域的大小。