AlphaMaskFilter Class
Applies the alpha from the mask image (or canvas) to the target, such that the alpha channel of the result will be derived from the mask, and the RGB channels will be copied from the target. This can be used, for example, to apply an alpha mask to a display object. This can also be used to combine a JPG compressed RGB image with a PNG32 alpha mask, which can result in a much smaller file size than a single PNG32 containing ARGB.
IMPORTANT NOTE: This filter currently does not support the targetCtx, or targetX/Y parameters correctly.
Example
This example draws a gradient box, then caches it and uses the "cacheCanvas" as the alpha mask on a 100x100 image. var box = new createjs.Shape();
box.graphics.beginLinearGradientFill(["#000000", "rgba(0, 0, 0, 0)"], [0, 1], 0, 0, 100, 100)
box.graphics.drawRect(0, 0, 100, 100);
box.cache(0, 0, 100, 100);
var bmp = new createjs.Bitmap("path/to/image.jpg");
bmp.filters = [
new createjs.AlphaMaskFilter(box.cacheCanvas)
];
bmp.cache(0, 0, 100, 100);
See Filter for more information on applying filters.
Constructor
AlphaMaskFilter
-
mask
Parameters:
-
maskImage
Item Index
Methods
- _applyFilter
- _createWebGLRenderer
- _getFragmentShader Get fragment shader code in GLSL.
- _getFragmentShader Get fragment shader code in GLSL. Filters are responsible for their own fragment shaders.
- _getVertexShader Get vertex shader in GLSL code. This sets the positions of each point on the context in 3D space.
- _initBuffers
- _initShaders
- _initUniforms
- applyFilter
- applyFilter
- clone
- getBounds
- initialize
- shaderParamSetup
- toString
Properties
Methods
_applyFilter
-
imageData
Parameters:
-
imageDataImageDataTarget ImageData instance.
Returns:
_createWebGLRenderer
()
protected
Attempt to creates a WebGLContextRenderer for fast, efficient filter rendering. Used during filter initialization. If it cannot be done, or this.useGL is set to false, then the filter is rendered normally using a Context2D.
_getFragmentShader
Get fragment shader code in GLSL.
Filters are responsible for their own fragment shaders.
()
String
protected
Returns:
_getVertexShader
Get vertex shader in GLSL code. This sets the positions of each point on the context in 3D space.
()
String
protected
Returns:
_initBuffers
-
ctx
Setup the buffers and camera for the object. In this case, just create a rectangle that occupies the full stage.
Parameters:
-
ctxObjectWebGL Context.
_initShaders
-
ctx
Setup the shaders for WebGL rendering. Each filter is responsible for its own fragment shader.
Parameters:
-
ctxObjectWebGL Context.
_initUniforms
-
ctx
Setup the uniforms for the fragment shader. Each filter is responsible for its own fragment shader uniforms, as they are responsible for their own fragment shaders.
Parameters:
-
ctxObjectWebGL Context.
applyFilter
-
ctx -
x -
y -
width -
height -
[targetCtx] -
[targetX] -
[targetY]
Applies the filter to the specified context.
IMPORTANT NOTE: This filter currently does not support the targetCtx, or targetX/Y parameters correctly.
Parameters:
-
ctxCanvasRenderingContext2DThe 2D context to use as the source.
-
xNumberThe x position to use for the source rect.
-
yNumberThe y position to use for the source rect.
-
widthNumberThe width to use for the source rect.
-
heightNumberThe height to use for the source rect.
-
[targetCtx]CanvasRenderingContext2D optionalNOT SUPPORTED IN THIS FILTER. The 2D context to draw the result to. Defaults to the context passed to ctx.
-
[targetX]Number optionalNOT SUPPORTED IN THIS FILTER. The x position to draw the result to. Defaults to the value passed to x.
-
[targetY]Number optionalNOT SUPPORTED IN THIS FILTER. The y position to draw the result to. Defaults to the value passed to y.
Returns:
If the filter was applied successfully.
applyFilter
-
ctx -
x -
y -
width -
height -
[targetCtx] -
[targetX] -
[targetY]
Applies the filter to the specified context.
IMPORTANT NOTE: This filter currently does not support the targetCtx, or targetX/Y parameters correctly.
Parameters:
-
ctxCanvasRenderingContext2DThe 2D context to use as the source.
-
xNumberThe x position to use for the source rect.
-
yNumberThe y position to use for the source rect.
-
widthNumberThe width to use for the source rect.
-
heightNumberThe height to use for the source rect.
-
[targetCtx]CanvasRenderingContext2D optionalThe 2D context to draw the result to. Defaults to the context passed to ctx.
-
[targetX]Number optionalThe x position to draw the result to. Defaults to the value passed to x.
-
[targetY]Number optionalThe y position to draw the result to. Defaults to the value passed to y.
Returns:
If the filter was applied successfully.
getBounds
()
Rectangle
Returns a rectangle with values indicating the margins required to draw the filter or null. For example, a filter that will extend the drawing area 4 pixels to the left, and 7 pixels to the right (but no pixels up or down) would return a rectangle with (x=-4, y=0, width=11, height=0).
Returns:
a rectangle object indicating the margins required to draw the filter or null if the filter does not effect bounds.
initialize
()
protected
Initialization method.
shaderParamSetup
-
gl -
stage -
shaderProgram
Assign any unique uniforms or other setup functionality here.
Parameters:
-
glWebGLContextThe context associated with the stage performing the render.
-
stageSpriteStageThe stage instance that will be rendering.
-
shaderProgramShaderProgramThe compiled shader that is going to be sued to perform the render.
Properties
FRAG_SHADER
String
Pre-processing shader code, will be parsed before being fed in. This should be based upon SpriteStage.SHADER_FRAGMENT_BODY_REGULAR
mask
Image
The image (or canvas) to use as the mask.
mask
HTMLImageElement | HTMLCanvasElement
The image (or canvas) to use as the mask.
VTX_SHADER
String
Pre-processing shader code, will be parsed before being fed in. This should be based upon SpriteStage.SHADER_VERTEX_BODY_REGULAR