/ /宛先出力合成を使用したEaselJSでのビットマップの一部の消去-easeljs

宛先からの合成を使用してEaselJSのビットマップの一部を消去する - easeljs

私はいくつかを取得するのに少し苦労しています機能する機能。私は、イーゼルJSを使用して消しゴムを作成し、画像の一部を消去しようとしています。他の人がこれを行うのを見ましたが、他のグラフィックを消去しているだけです。他のグラフィックではなくビットマップを消去したい場合、それは可能ですか?

また、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/

回答:

回答№1は1

この例は、キャッシュしたShapeインスタンスにのみ影響します。で複数の引数を使用する場合 addChild()、最後に追加されたアイテムを返すため、サンプルでは、 art 変数は形状を参照するだけです。したがって、画像は、描画先の「ペイントされた領域」のすぐ下にあります。

これを修正するには、代わりにコンテナを作成してキャッシュします。いくつかのマイナーな追加:

  1. イメージがロードされたら、キャッシュを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");