I need to draw on a bitmap but not draw on the transparent pixels of it?
Example picture here: https://i.stack.imgur.com/QvJTZ.png
I have written this code :
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Matrix;
var s:S = new S();
var m_bitmapData = new BitmapData(s.width, s.height, true, 0x00000000);
m_bitmapData.draw(s);
var hole=new Sprite();
var hole_matrix:Matrix
hole.graphics.beginFill(0x000000);
hole.graphics.drawCircle(0,0,30);
var bmp:Bitmap = new Bitmap(m_bitmapData);
bmp.x = 50
bmp.y =50
stage.addChild(bmp);
addEventListener(Event.ENTER_FRAME,asd);
function asd(e:Event):void{
hole_matrix=new Matrix();
hole_matrix.translate(mouseX-bmp.x,mouseY-bmp.y);
m_bitmapData.draw(hole,hole_matrix);
}
But result is such as picture “NO”.
Can someone please explain ?
Preserve alpha channel, then copy it back.
Basically what you do: You have a BitmapData elsewhere that has the same size as your canvas BitmapData, then you copy alpha channel into that BitmapData, draw what you want, then copy alpha channel back. Whatever was transparent, remained transparent. Note, if you never want the alpha channel to change, you can
copyChannel()it away only once, and then just restore.