Masking with Bitmap discrepancy

The following code works with Flash but doesn’t work on OpenFL (next and legacy).

var current = Lib.current;
var stage   = current.stage;
var circle  = new Bitmap(new BitmapData(50, 50, true, 0x00000000), PixelSnapping.ALWAYS, true);
var box     = new Bitmap(new BitmapData(50, 50, true, 0xFFFFFFFF), PixelSnapping.ALWAYS, true);

var cs = new Shape();
cs.graphics.beginFill(0xFF00FF, 1);
cs.graphics.drawCircle(25, 25, 20);
cs.graphics.endFill();
circle.bitmapData.draw(cs);

var boxSprite = new Sprite();
var cirSprite = new Sprite();

boxSprite.addChild(box);
cirSprite.addChild(circle);

boxSprite.cacheAsBitmap = true;
cirSprite.cacheAsBitmap = true;

current.addChild(boxSprite);
// current.addChild(cs);
current.addChild(cirSprite);

// boxSprite.mask = cs;
boxSprite.mask = cirSprite;

Making using Shape works correctly, but unfortunately I need to use bitmap mask because the mask shape would be complex to recreate using Shape (not a basic shape) and even if I could recreate it in shape I thought it would cause a performance hit?.

Sounds like the feature isn’t yet implemented. In the meantime, you can work around it using either copyPixels() or copyChannel().

Possible implementation:

box.bitmapData.copyChannel(circle.bitmapData,
    new Rectangle(0, 0, circle.width, circle.height),
    new Point(),
    BitmapDataChannel.ALPHA,
    BitmapDataChannel.ALPHA);
1 Like

While it works, unfortunately it doesn’t solve my requirement. The mask had to be updated each frame because the masked DisplayObject is updated every frame. Also the masked DisplayObject is not Bitmap/BitmapData but a Sprite. So copyPixels() and copyChannel() doesn’t really solve it.

doesn’t mean to bump on old thread, but it doesn’t seem to work until now. Is there other trick to get masking working?

You can also test and see if it works using a software renderer. On HTML5, try -Dcanvas or try -Dcairo elsewhere. If it works in software, bitmapData.draw of the objects should work, too, without changing the whole project to run in software