0
votes

i have the following situation:

one drawing layer (graphics object with lineTo, etc.)

one png with an alpha channel (supposed to serve only as a mask)

now i want to be able to only draw lines within an area restricted by the png mask.

i am trying like this:

var bitmapData:BitmapData = new BitmapData( 320, 320 );
bitmapData.draw( drawingLayer );
bitmapData.copyChannel( maskBitmapData, new Rectangle( 0, 0, 320, 320 ), new Point( 0, 0 ), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );

now the borders get cut off fine, but i get a black background, since the drawing layer has an alpha between the drawn lines (and it should remain like this) and the mask bitmap has an alpha outside the shape. so naturally the mask's alpha replaces the drawing layer's.

i tried it with merge, copyPixels and also with just setting the mask property on the drawing layer (i set everything to cacheAsBitmap) but to no avail.

can someone help me here?

ADDITION:

when trying to use a mask i tried it with

<s:BitmapImage id="mask" source="@Embed(source='...')" cacheAsBitmap="true" />

and also as

[Embed("...")]
private const BodyMask:Class;
var maskBitmap:Bitmap = new BodyMask();

and assigned them to the mask property of a s:Group element (cacheAsBitmap=true) where the operations on the graphics object occured. i also tried reassigning the mask after each draw operation.

is there maybe something wrong with that?

2

2 Answers

1
votes

I've just had the similar problem, and managed to work this out like this

maskedBitmap.fillRect(maskedBitmap.rect, 0);
bitmapData.draw( drawingLayer );
maskedBitmap.copyChannel( maskBitmap, maskBitmap.rect, new Point(), BitmapDataChannel.BLUE, BitmapDataChannel.ALPHA );
maskedBitmap.copyPixels( bitmapData, bitmapData.rect, new Point(),maskedBitmap, new Point() );

I'm copying blue channel to alpha channel since my maskBitmap is grayscale. Because you're using alpha in your mask, you should copy alpha to alpha:

maskedBitmap.copyChannel( maskBitmap, maskBitmap.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );
0
votes

It's been a while since I've done anything with Actionscript but I was wondering why you were trying to merge the two alpha channels. Can you not just set the png the be the mask of the sprite you're (or the user is) drawing into?

as in

drawingLayer.mask = maskBitmap;