我正在寻找一些关于如何在 Actionscript 中制作涂抹工具的代码。我发现的最好的是这里的这个旧条目,但我想还有其他更好的选择。
谢谢你。
编辑:假设不存在更好的片段。这是我现在拥有的代码:
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);
}
}
但是,如果您在同一个地方涂抹很长时间,这些区域会变成黑色,而不是保持光标下方点的颜色。是否有更好的算法来创建不受上述代码限制的涂抹工具?