Extract bitmap (or bitmapdata) from Shape - actionscript-3

I got an swf that has SimpleButtons in the stage, and I need to get the bitmap or bitmapdata information from the button states.
When I load the symbol, it seems that no matter what composes the button states, they are all shapes, at least that's what I get in the expression panel.
So, how can I get the bitmap or bmpdata from a Shape?.
Thanks.

You can use BitmapData#draw() to get BitmapData from any DisplayObject. BitmapData is raster, so be aware you will lose features of the vector Shape
var bd:BitmapData = new BitmapData( shape.width, shape.height );
bd.draw( shape );

If you're targeting FP 11.6 I'd use the method graphics.readGraphicsData as explained here http://www.bytearray.org/?paged=6

Related

createjs mask a Bitmap with another Bitmap

I'm trying to mask a Bitmap object with another Bitmap, but so far with actual success.
There's a jsfiddle by Lanny, but it involves a Shape object being the mask, so my questions are:
Can you use an alpha channel from another bitmap to mask a createjs Bitmap object?
Is it possible to use an alpha channel from a png image as a mask?
#bks's comment is correct. You can use AlphaMaskFilter to use one image to mask another.
From the docs: http://createjs.com/docs/easeljs/classes/AlphaMaskFilter.html
var bmp = new createjs.Bitmap("path/to/image.jpg");
bmp.filters = [
new createjs.AlphaMaskFilter(box.cacheCanvas)
];
bmp.cache(0, 0, 100, 100);
Ensure you cache the bitmap after applying the filter. Keep in mind filters can be slow, so don't update it every frame.
Cheers.

Starling AS3 -- How To Use Bitmap Objects in SWC

I'm trying to see how Starling could benefit the applications I build using as3. My knowledge thus far of Starling is that it only uses bitmap objects not symbol objects. How do I take a BitmapData class and create an starling Image out of it.
This bitmap was a symbol in flash that was converted into bitmap. It then had its properties modified to export for as3
Say there is a Ship BitmapData class in a swc.
According to Starlings documentation of how it handles other bitmap like a png, I would think I should be able to do this.
var myShip:Image = Image.fromBitmap(new Ship());
This of course doesn't work.
fromBitmap() expects to receive a bitmap object but here you are passing in a BitmapData Class. And Image class requires a texture.
Also your syntax is slightly incorrect.
NB. There is Texture.fromBitmap and Texture.fromBitmapData.
Although I haven't tried accessing assets from a swc you could try:
var myShip:Image = new Image(Texture.fromBitmapData(new Ship());
Perhaps try to get the Image appearing first before attempting to access the swc bitmapData
var bd:BitmapData = new BitmapData(100, 100, false, 0xFF0000);
var myShip:Image = new Image(Texture.fromBitmapData(bd);
addChild(myShip); //should display a red square

AS3 cacheAsBitmap questions?

I'm a bit confused. If I import a png picture, drag it onto my stage, and right click > convert to bitmap. Is that the same thing is if I have a vector created in code, and then apple cacheAsBitmap = true?
A PNG picture is a raster object, and it has a parent class of Bitmap already. You might, however, encapsulate that Bitmap into a drawn rectangle (effectively making a rectangle with bitmap fill, that is actually a vector object), and then convert to bitmap, applying cacheAsBitmap = true to the vector object made of the raster object. I don't understand why do you want double transformation raster->vector->raster in the first place. Probably Flash isn't so stupid if you just drag a library asset made out of a PNG to the stage, it'll make you a Bitmap based object instead, and "convert to bitmap" won't do a thing.

AS3 Is it possible to duplicate a Shape object?

I'm trying to make a shape available for duplicating. Here's an explanation of what I've done, what I'm trying to do, and where I'm stuck:
Drew a shape manually in the Flash IDE (paintbrush).
Created a new movieclip containing the shape; exports as a class.
Instantiate the class (var mc:MovieClip = new shapeMovieClip()).
Add a reference to the shape in mc that I want (var myShape:Shape = mc.getChildAt(0) as Shape;
This works perfect and I now have my shape, but how can I duplicate it without instantiating the parent MovieClip class - is it possible?
I've tried creating a new shape and using the copyFrom() graphics method with no avail, I believe this method just copies draw calls when they're made on the referenced graphics and instead I just have a reference of a shape that's already been drawn.
Right now I'm extending the movieclip as a sprite instead, instantiating, pulling the shape from the parent and saving its reference, and then nulling the sprite. Does anyone know of a better, more lightweight, strategy for duplicating Shapes in this manner?
Basically depends on whether you need to scale your shapes. If you don't, and you can work it out with a fixed sized bitmap representation of the shape, then you will get much better performance drawing your shape to a BitmapData (it's called rasterisation) and instanciating Bitmap objects (as other commenters have pointed out). The code would go something like this:
var base:Sprite = new shapeMovieClip();
var bmd:BitmapData = new BitmapData(base.width, base.height, true, 0);
bmd.draw(base);
var clip1:Bitmap = new Bitmap(bmd);
var clip2:Bitmap = new Bitmap(bmd);
If you do need to scale the clips, you will get pixelation using bitmaps. When scaling down Bitmap.smoothing can help to some extent (also when rotating), but if you need to scale up, you will probably have to use some kind of mip-mapping. This is basically creating a few bitmaps of the shape at different scale levels, and then swap them depending on the current scale. Coding this has some complexity (using a helper parent to adjust the scale can help) but it will definitely perform better than using many shape symbols (with or without a sprite parent).
This is very old, but it still comes up high in Google, so I just wanted to share a true shape duplicating method:
var shapeonstage:Shape = shapeMadeInIDE;
var g:Vector.<IGraphicsData> = shapeonstage.graphics.readGraphicsData();
var shapecopy:Shape = new Shape();
shapecopy.graphics.drawGraphicsData(g)
And boom. It works. Had to share this because it would have helped me a looooong time ago and in so many ways.
UPDATE:
There is some clarification I'd like to add why you would want to use this method, for duplicating AND for storing references to Shapes that are within an swf.
If you target a Shape object on the stage or in a movie clip, the Flash Rendering engine does something strange. It will RECYCLE Shape objects to render new graphics thus making your Shape reference point to a COMPLETELY different rendering.
For example:
Create an fla with a movieclip.
Inside the movie clip make 10 frames.
On each frame, draw a different shape
In your code, get a reference to the shape (The Shape on Frame 1)
Draw and verify your shape (draw to a bitmap then put the bmp on stage)
Now let the flash engine play for 5 frames
Draw and verify your shape again
The second time you draw your shape without EVER reassigning your shape reference, will SOMETIMES yield a completely different shape.
Just know, this little quirk can leave you pulling your hair out if you don't know what you're looking for :)

Screendump from within Actionscript 3.0 is it possible or?

We have Bitmap and Bitmapdata objects now. And when using the webcam, we can get raw-pixeldata output from it. But, can we get raw-pixeldata from the "stage" or "swf" object somehow?
I would like to use this to make "small thumbnails" of certain parts of Actionscript applications and these could be complex compositions of dynamic text, bitmap graphics and movieclips at the same time. So it would be nice to make a "quick snap" and just get the current combined pixels into one bitmap and then be able to "save that for later use".
Is that possible? is it too easy? am I just looking the wrong place in the Adobe Docs?
We have images, vectors etc at the same time on stage, so I need to grab the "stage" objects bitmapdata???
Create a BitmapData and call its draw() method with the corresponding DisplayObject
var bmpData:BitmapData = new BitmapData(sprite.width, sprite.height, true);
bmpData.draw(sprite);
If you want to make thumbnails smaller, create a Matrix and call its createBox method with required scaling parameters and pass it to the draw method.
var bmpData:BitmapData = new BitmapData(thumbW, thumbH, true);
var mat:Matrix = new Matrix();
mat.createBox(thumbW / sprite.width, thumbH / sprite.height);
bmpData.draw(sprite, mat);