【问题标题】:Erasing parts of a bitmap in EaselJS using destination-out compositing使用目标输出合成在 EaselJS 中擦除位图的一部分
【发布时间】:2015-09-25 23:26:53
【问题描述】:

我在使某些功能正常工作时遇到了一些困难。我正在尝试使用easelJS创建橡皮擦并擦除图像的一部分。我见过其他人这样做,但只是擦除其他图形-当我尝试擦除图像时,我什么也做不了。如果我想擦除位图而不是其他图形,这可能吗?

我也尝试使用 AlphaMaskFilter,但它给我的结果与我正在寻找的完全相反(它掩盖了一切,只揭示了我画的东西)。

var c = createjs, stage, art;
var x, y, listener, color, hue=0;

stage = new c.Stage("test");
var testImg = new c.Bitmap("http://lorempixel.com/output/animals-q-c-640-480-5.jpg");

art = stage.addChild(testImg, new c.Shape());
art.cache(0,0,600,400);

stage.on("stagemousedown", startDraw, this);

function startDraw(evt) {
    listener = stage.on("stagemousemove", draw, this);
    stage.on("stagemouseup", endDraw, this);
    color = c.Graphics.getHSL(hue+=85, 50, 50);
    x = evt.stageX-0.001; // offset so we draw an initial dot
    y = evt.stageY-0.001;
    draw(evt); // draw the initial dot
}

function draw(evt) {
    art.graphics.ss(20,1).s(color).mt(x,y).lt(evt.stageX, evt.stageY);

    // the composite operation is the secret sauce.
    // we'll either draw or erase what the user drew.
    art.updateCache(erase.checked ? "destination-out" : "source-over");

    art.graphics.clear();
    x = evt.stageX;
    y = evt.stageY;
    stage.update();
}

function endDraw(evt) {
    stage.off("stagemousemove", listener);
    evt.remove();
}

http://jsfiddle.net/17xec9y5/8/

【问题讨论】:

    标签: easeljs


    【解决方案1】:

    您的示例仅影响您缓存的 Shape 实例。当您在 addChild() 中使用多个参数时,它会返回最后添加的项目,因此在您的示例中,art 变量仅引用形状。所以图像就在您正在绘制的“绘制区域”下方。

    要解决此问题,请改为创建并缓存一个容器。一些小的补充:

    1. 图像加载后,更新一次缓存(以应用图像)。
    2. 然后删除该图像,以便在每次绘制时更新缓存时不再应用它。

    就是这样!

    这是一个小提琴: http://jsfiddle.net/lannymcnie/17xec9y5/9/

    相关代码:

    // Listen for the image load
    testImg.image.onload = function() {
        cont.updateCache("source-over"); // Update cache once
        cont.removeChild(testImg); // Remove image
    
        stage.update(); // Draw the stage to see the image
    }
    
    // Create a sub-container that will hold the art and image 
    var cont = stage.addChild(new c.Container());
    art = new c.Shape(); // Art is just the shape
    cont.cache(0,0,600,400); // Cache the container instead
    cont.addChild(testImg, art);
    
    // Then, later update the container's cache (instead of the art)
    cont.updateCache(erase.checked ? "destination-out" : "source-over");
    

    【讨论】:

    • 不好意思,没想到这个。谢谢兰尼!顺便说一句,我真的不必查看 EaselJS 的文档——并且就像我使用 AS3 一样构建它。该规则有一些例外,但在大多数情况下,学习曲线非常小。这是一件很难实现的事情,值得一提。赞一个!
    • @Lanny 很抱歉恢复了这个旧帖子,但在你的例子中,我将如何将舞台“重置”到其原始状态(即,testImg 被任何绘图渲染且未触及/擦除)?
    • 你可以再次添加testImg,更新缓存,删除它,它基本上会被重置:jsfiddle.net/17xec9y5/28
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多