Bitmapdata erasing in as3 (with custom brush shape)
For a drawing program made in flash, i came across the need to erase a bitmapData object using a custom brush tip. Finally i came across this example which was exactly what i needed, except it was written for actionscript 2.
I updated the script to actionscript 3, and added some extra comments that made me understand how this worked. I hope it saves you some time if you need to do something similar. (Please forgive the roughness of the code, its just a test for learning porpouses)
type="application/x-shockwave-flash">

Bob 12:41 am on August 1, 2008 Permalink
dude, nice! you just saved me a huge headache and tons of time… thanks
clean code too
mitomane 5:18 am on April 16, 2009 Permalink
Hi, thanks for the great code example!
I’m using your code to erase part of a BitmapData and I’m rotating this BitmapData using matrix and the erase part work great… but there’s a problem.
I’m rotating an object around the center of a given Point and this is the code of the function I’m using:
public function rotateAroundPoint(m:Matrix, x:Number, y:Number, angleDegrees:Number):void
{
m.translate(-x, -y);
m.rotate(angleDegrees*(Math.PI/180));
m.translate(x, y);
}
Theproblem is that the erasing part is not taking in account the rotation matrix of the BitmpaData and erase part of the image like the image was never rotated at all…
Do you have any idea or suggestion?
Thanks
mitomane 5:19 am on April 16, 2009 Permalink
…”I’m rotating an object around a given Point”
mitomane 6:52 am on April 16, 2009 Permalink
ok more or less I’m near the solution…
when I’ll have everything working good I’ll post the solution.
mitomane 5:59 am on May 6, 2009 Permalink
Erasing portion of a rotating BitmapData using AS 3
http://www.mitomane.com/?p=128
Frank 8:41 pm on September 3, 2009 Permalink
HELP! Great erase thing you got here. But how can I change the cursor to a big brush and have only the tip erase, not the wooden stick part. I tried:
Mouse.hide();
cursor_mc.startDrag(“true”);
and added another shape to it. With your brush you can still see the mouse pointer and the created brush behind it. I want to replace the mouse pointer with a big brush that layers on top of your brush. So it looks like I have a huge brush but only the tip erases stuff. Any ideas? That would be so great if you could help!
Jean-François Desgagné 2:56 pm on January 21, 2010 Permalink
A copy from http://www.senocular.com/flash/source/?id=0.175 (same comment) 2 years before!
Jean-François Desgagné 3:09 pm on January 21, 2010 Permalink
nvm, written in the script
Good work
sucho 4:53 pm on May 11, 2010 Permalink
You can achieve the same with this line
bitmapData.draw(eraser,matrix,colorTransform,BlendMode.ERASE)
where eraser is your “brush tip” display object, matrix and colortransform aren’t required, can be null
also, if eraser alpha value is lower than 1, you can get some interesting results :B
Piterwilson 3:05 pm on June 8, 2010 Permalink
You might be right, I haven’t looked at this in ages, should update it. Thanks.