as3使用LineStyle绘制线条,然后使用透明(不可见)像素进行编辑

时间:2012-08-27 13:40:40

标签: actionscript-3

我有外部加载图像的MC(jpg)。

下一层被遮挡了Sprite,我用graphics.lineStyle用不同的颜色,不透明度等绘制线条。一切正常,所以我可以看到图像和图纸。现在我想要橡皮擦制作,并尝试制作某种(透明)线条,以“擦除”现有的线条。

如果我例如绘制(用鼠标)红线,然后将颜色切换为黄色并在前一行的某个点上相交,则可以,我看到黄色。现在我不知道如何制作“透明”的颜色?因此,当鼠标移动到现有图形上时,只需再次看到背景图像的一部分。如果我设置一个颜色背景一切都OK,但我有图像,结束橡皮擦留下一些颜色,我需要透明。尝试混合模式,缓存为bmp,我发现但仍然没有解决方案。

1 个答案:

答案 0 :(得分:3)

您似乎使用覆盖图像的Graphics对象。为了擦除某些东西,你应该给你的精灵一个最顶层的形状,将BlendMode.ERASE作为混合模式,并用颜色为0xFF的alpha分量绘制它。精灵应该将其blendMode设置为BlendMode.LAYER。这是一个可以玩的测试:

public class TestErase extends Sprite 
{
    public function TestErase() {
        var b:Sprite = new Sprite();
        var c:Shape = new Shape();
        b.blendMode = BlendMode.LAYER;
        c.blendMode = flash.display.BlendMode.ERASE;
        c.graphics.beginFill(0, 1);
        c.graphics.drawRect(50, 50, 50, 50);
        c.graphics.endFill();
        b.graphics.beginFill(0xff0000, 1);
        b.graphics.drawRect(0, 0, 150, 150);
        b.graphics.endFill();
        b.addChild(c);
        addChild(b);
        this.graphics.beginFill(0x00c000, 1);
        graphics.drawRect(75, 75, 100, 100);
        graphics.endFill();

    }
}

这使得一个50x50的孔,由“b”精灵中的“c”精灵定义,不会影响“this”精灵。

好的,你问了一个例子。这里。

public class Main extends Sprite 
{
    public var sh:Shape;
    public var drawing:Boolean;
    public var sp:Sprite;
    public var bd:BitmapData;
    public var erasing:Boolean;
    [Embed(source = '../lib/093.jpg')]
    public static const Background:Class;

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point
        var ba:Bitmap = new Background();
        ba.scaleX = 0.5;
        ba.scaleY = 0.5;
        addChild(ba);
        bd = new BitmapData(640, 400, true, 0);
        var bm:Bitmap = new Bitmap(bd);
        sp = new Sprite();
        sp.addChild(bm);
        sp.blendMode = BlendMode.LAYER;
        var tf:TextField = new TextField();
        tf.y = 401;
        tf.background = true;
        tf.backgroundColor = 0x0;
        tf.defaultTextFormat = new TextFormat('Arial', 12, 0xe00000, true, false, false);
        tf.selectable = false;
        tf.text = 'Erase';
        addChild(tf);
        addChild(sp);
        tf.addEventListener(MouseEvent.CLICK, toggleErasing);
        sp.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
        sp.addEventListener(MouseEvent.ROLL_OUT, endDrawing);
        sp.addEventListener(MouseEvent.MOUSE_UP, endDrawing);
    }

    private function toggleErasing(e:MouseEvent):void {
        erasing = !erasing;
        if (erasing) (e.currentTarget as TextField).text = 'Erasing...'; else (e.currentTarget as TextField).text = 'Erase';
        e.stopPropagation();
    }
    private function startDrawing(e:MouseEvent):void {
        if (drawing) return;
        sh = new Shape();
        sh.graphics.lineStyle(3, Math.floor(Math.random() * 0x100000), 1,true);
        if (erasing) sh.blendMode = BlendMode.ERASE;
        sh.graphics.moveTo(e.localX, e.localY);
        addEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
        sp.addChild(sh);
        drawing = true;
    }
    private function endDrawing(e:MouseEvent):void {
        if (!drawing) return;
        removeEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
        if (erasing) bd.draw(sh, null, null, BlendMode.ERASE); 
        else  bd.draw(sh);
        sp.removeChild(sh);
        sh = null;
        drawing = false;
    }
    private function trackMouse(e:MouseEvent):void {
        sh.graphics.lineTo(e.localX, e.localY);
    }

}

由我测试,并且应该按原样工作,只需更改背景:)