createjs mask a Bitmap with another Bitmap - html

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.

Related

Storing the result of blended images in AS3.0

I am making an interactive animation which background images are masked by a shape captured in a camera. In the every frames, camera changes images into black and white images and then it's used to mask background images.
In the code, "now" is the images captured by camera, "P1" is the back ground image.
After I masked by using blend mode multiply, I wish to store the result image which is masked and use for other things.
I don't understand when I use blendmode function, how the result is defined well.
Can I capture, copy or store the result image?
var P1:MovieClip = new p1();
var mskimg:MovieClip = new maskimage();
var bitmap_obj:Bitmap = new Bitmap(now);
addChild(P1);
P1.blendMode = BlendMode.LAYER;
addChild(bitmap_obj);
bitmap_obj.blendMode = BlendMode.MULTIPLY;
You can draw a masked object if you put everything that you intend to be drawn into a container, then call BitmapData.draw() to draw the complete layered object to some BitmapData. Then, you can use that bitmap data to do whatever you want, Graphics.beginBitmapFill(), FileReference.save(), whatever. The operation is however expensive, so it'll be better if you trigger the drawing by responding to a user action, and not do that every frame by default, unless required by your application's logic.

Extract bitmap (or bitmapdata) from Shape

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

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 Stage3D: how to render all triangles with an overall alpha?

I am working on a 2D AS3 project, where the different layers are rendered via Stage3D, in a single drawTriangles() call, as a single mesh.
(if you are familiar with the BunnyMark GPUSprite mini render engine, that will give you an idea: http://www.bytearray.org/?p=4074)
What I would like is to draw one of these entire 'render layers', with an overall alpha transparency value, that would apply to ALL triangles drawn, adding to their own alpha values.
IE I am not looking to change the alpha by using textures with alpha transparency or via having to go through setting each triangle separately to have the same alpha: I want a master switch that would affect the alpha value of everything that is drawn? ( something computationally cheap)
Is this possible? Perhaps via a Shader, or a setProgramConstantsFromVector command?
Well you could render your layer to a texture, then you could put this texture on the stage with the alpha you want, this way your whole layer would have the same alpha!
You can do this in two ways, the first way is to render it to a texture, and display that texture with Stage3D and create a shader that adjusts the alpha.
The second way is rendering it to a bitmapData, and displaying it on the normal stage. I'll give an example for this way:
var bmd:BitmapData = new BitmapData(renderWidth, renderHeight, true, 0);
var bit:Bitmap = new Bitmap(bmd);
bit.alpha = 0.5;
addChild(bit);
//In your render loop at the beginning
context3D.renderToBitmapData(bmd);

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 :)