In order to create some “hole” into a picture, I tried to draw a transparent square over a colored one using BitmapData.draw() and the BlendMode.ALPHA (or BlendMode.ERASE) blending mode
However, this only seems to work when targetting flash but is bugged when targeting windows (and probably others cpp targets), both with openfl legacy and openfl next…
Consider the following sample test code:
package;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.BlendMode;
import openfl.display.Graphics;
import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFormatAlign;
class Main extends Sprite {
public function new () {
super ();
//Create a blue background square so we can check transparency
var bg:BitmapData = new BitmapData(200, 200, true, 0xFF0000FF); // A bitmapdata with a default red background
var background:Bitmap = new Bitmap(bg);
addChild(background);
//Create a bitmapdata with some transparent "windows" in it
var bm:BitmapData = new BitmapData(100, 100, true, 0xFFFF0000); // A bitmapdata with a default red background
//Draw the transparent windows over it
var hs:Shape = new Shape();
var g:Graphics = hs.graphics;
g.lineStyle(0, 0x000000);
g.beginFill(0x000000, 0);
g.drawRoundRect(25,25,50,50,10,10);
g.endFill();
bm.draw(hs,null,null,BlendMode.ALPHA,null,true);
var bitmap:Bitmap = new Bitmap(bm);
addChild(bitmap);
}
}
you should see this (which is what you get on flash)
But here is what you get on windows (openfl legacy and openfl next)
So is it possible to fix this both on openfl next but also openfl legacy (because I use legacy for some projects which still have issues with next for now)?
Thanks
Also, you can see in this example another bug. The line of the square “window” is supposed to have a thickness of 0, meaning it shouldn’t be visible. This is the case on flash but actually not on cpp targets… (even if it can be masked by setting an alpha of 0 in the linestyle() function to make it transparent. So it is not really anoying. But this is still a bug so might be cool to fix it too (probably just have to add a test to test if thickness is 0 and to prevent line from drawing if this is the case) )