开发者

BitmapData of a masked image in ActionScript 3 (AS3)

开发者 https://www.devze.com 2022-12-13 19:10 出处:网络
I am trying to take a snapshot of the masked region of an image... so, I load my image then perform the following functions:

I am trying to take a snapshot of the masked region of an image... so, I load my image then perform the following functions:

private function manageLoadedImage(e:Event):void
{

   _bitdata = e.currentTarget.content; // get the bitmap

   _bithold.addChild( _bitdata ); // add the bitmap to a sprite on the stage

   _bithold.x = holder1.x -((_bithold.width - holder1.width)/2); // center image

   _bithold.y = holder1.y -((_bithold.height - holder1.height)/2); // center image

   var m:Shape = new Shape(); // create the shape

   m.graphics.beginFill(0x0); // make the fill

   m.开发者_JS百科graphics.drawRect( this.x, this.y, holder1.width, holder1.height ); // draw the mask

   m.graphics.endFill(); // end the fill

   _bithold.mask = m; // mask the image


}// private function manageNewPaneAddition(e:Event):void

public function save( ):void
{

                          // WHAT DO I DO HERE ????????

   _bmdsrc = new BitmapData( holder1.width, holder1.height ); // create the new bitmapdata

    var m:Matrix = _bithold.transform.matrix; // lets try this out

    m.tx = -holder1.x + _bithold.width; // not sure what this means ?

    m.ty = -holder1.y + _bithold.height; // what does this mean ?

   _bmdsrc.draw( _bithold, m); // draw the bitmapdata

                        // END PROBLEM ??????????????

}// private function save(  ):void

So, after I manage the loaded image, I save it. But the save function only outputs a white square 80x80px. This tells me that I am snapshot-ing a blank stage.

The MovieClip construction is as follows:

I have a movie, inside that movie I have a thumbnail editor name ThumbEdit.

ThumbEdit has a movieclip on its stage called "holder1". In the document class of ThumbEdit I create a sprite "_bithold" and place it on the stage at holder1.x and holder1.y. When the image loads, I add the image to _bithold and then mask _bithold with a shape. So, I want to grab a snapshot of the masked region of _bithold but I'm not sure how I should go about doing that... any advice?


There are some approach to achieve what you need. The simpliest is to use BitmapData with Matrix classe, just as you've done.

The save method seems to be correct, except for the references and maybe the coords are not right. Your save method should look like this, considering that _mask is the shape masking the content, and _bithold is the masked movieclip:

public function save( ):void
{

   _bmp = new BitmapData( _mask.width, _mask.height ); // creates the bitmap of the mask's size

   var m:Matrix = new Matrix();
       m.translate( -_mask.x, -_mask.y ); // Create the matrix used to translate the positions of the source image.

   _bmp.draw( _bithold, m); // draw the bitmapdata considering the offset of the mask


   addChild( new Bitmap(_bmp) ); // just attaching the result on the screen, to see the result.

}

It is important to notice, that the line that translates the matrix object, must input values relatives to the _bithold movieclip. It means that if the mask shape and the masked object (_bithold) aren't in the same parent, you need to bring the coords to the movieclip parent, with globalToLocal and localToGlobal methods.

Any other doubt, I'll be here!

Cheers, CaioToOn!


You can try using BitmapData.copyPixels().


I decided to use Library movieclips instead. I created a frame with a masked movieclip the size I needed inside of a MovieClip that is on the stage.

So, on the stage I have the movieclip named "Snap". Inside snap is two layers, mask and holder, both are Movieclips.

When the bitmap loads, I add it to the holder movieclip which is masked appropriately by the mask layer. Then, I take a snapshot of the "Snap" movieclip.

I probably could have sorted it out programmatically, but this was much faster and very simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消