API Documentation for: 0.8.2
Show:

File:easeljs\filters\webgl\AlphaMaskFilter.js

/*
 * AlphaMaskFilter
 * Visit http://createjs.com/ for documentation, updates and examples.
 *
 * Copyright (c) 2010 gskinner.com, inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @module EaselJS
 */

// namespace:
this.createjs = this.createjs || {};

(function () {
	"use strict";

	/**
	 * 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.
	 *
	 * <b>IMPORTANT NOTE: This filter currently does not support the targetCtx, or targetX/Y parameters correctly.</b>
	 *
	 * <h4>Example</h4>
	 * 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 {{#crossLink "Filter"}}{{/crossLink}} for more information on applying filters.
	 * @class AlphaMaskFilter
	 * @extends Filter
	 * @constructor
	 * @param {Image} mask
	 **/
	var AlphaMaskFilter = function (mask) {
		this.initialize(mask);
	};
	var p = AlphaMaskFilter.prototype = new createjs.Filter();

// constructor:
	/** @ignore */
	p.initialize = function (mask) {
		this.mask = mask;

		// Create WebGL canvas for fast rendering.
		this._createWebGLRenderer();
	};

// public properties:

	/**
	 * The image (or canvas) to use as the mask.
	 * @property mask
	 * @type Image
	 **/
	p.mask = null;

// public methods:

	p.Filter_applyFilter = p.applyFilter;
	/**
	 * Applies the filter to the specified context.
	 *
	 * <strong>IMPORTANT NOTE: This filter currently does not support the targetCtx, or targetX/Y parameters
	 * correctly.</strong>
	 * @method applyFilter
	 * @param {CanvasRenderingContext2D} ctx The 2D context to use as the source.
	 * @param {Number} x The x position to use for the source rect.
	 * @param {Number} y The y position to use for the source rect.
	 * @param {Number} width The width to use for the source rect.
	 * @param {Number} height The height to use for the source rect.
	 * @param {CanvasRenderingContext2D} [targetCtx] The 2D context to draw the result to. Defaults to the context passed to ctx.
	 * @param {Number} [targetX] The x position to draw the result to. Defaults to the value passed to x.
	 * @param {Number} [targetY] The y position to draw the result to. Defaults to the value passed to y.
	 * @return {Boolean} If the filter was applied successfully.
	 **/

	p.applyFilter = function(ctx, x, y, width, height, targetCtx, targetX, targetY) {
		if (!this.mask) { return true; }
		targetCtx = targetCtx || ctx;
		if (targetX == null) { targetX = x; }
		if (targetY == null) { targetY = y; }

		var wctx = this._workContext;
		if(this.useGL && wctx) {
			// TODO: In WebGL, the mask currently doesn't take individual x, y, width. and height into account. It will always scale itself to the same size as the object.
			var source = ctx.canvas;
			var mask = this.mask;

			this._initViewport(wctx, source.width, source.height);
			this._initTexture(wctx, source, false);
			this._initTexture(wctx, mask, true);

			wctx.uniform4f(wctx.imageRectUniform, targetX, targetY, source.width, source.height);
			wctx.uniform4f(wctx.maskRectUniform, targetX, targetY, mask.width, mask.height);

			wctx.drawArrays(wctx.TRIANGLE_STRIP, 0, 4);

			var glData = new Uint8Array(width*height*4);
			wctx.readPixels(0,0,width,height, wctx.RGBA, wctx.UNSIGNED_BYTE, glData);
			var imageData = targetCtx.createImageData(width, height);
			imageData.data.set(glData);

			targetCtx.putImageData(imageData, targetX, targetY);

			return true;
		}

		targetCtx.save();
		if (ctx != targetCtx) {
			// TODO: support targetCtx and targetX/Y
			// clearRect, then draw the ctx in?
		}

		targetCtx.globalCompositeOperation = "destination-in";
		targetCtx.drawImage(this.mask, targetX, targetY);
		targetCtx.restore();
		return true;
	};

	/**
	 * Returns a clone of this object.
	 * @method clone
	 * @return {AlphaMaskFilter}
	 **/
	p.clone = function () {
		return new AlphaMaskFilter(this.mask);
	};

	p.toString = function () {
		return "[AlphaMaskFilter]";
	};

// private methods:

	p._initUniforms = function(ctx, program) {
		ctx.activeTexture(ctx.TEXTURE1);
		ctx.uniform1i(ctx.getUniformLocation(program, "uSamplerMask"), 1);

		ctx.imageRectUniform = 	ctx.getUniformLocation(program, "uImageRect");
		ctx.maskRectUniform = 	ctx.getUniformLocation(program, "uMaskRect");
	};

	/**
	 * @method _getFragmentShader
	 * Get fragment shader code in GLSL.
	 * @returns {String}
	 * @protected
	 */
	p._getFragmentShader = function() {
		return "" +
			"precision mediump float;				\n" +

			"varying vec2 vTextureCoord;			\n" +

			"uniform sampler2D uSampler;			\n" +
			"uniform sampler2D uSamplerMask;		\n" +

			"uniform vec4 uImageRect;				\n" +
			"uniform vec4 uMaskRect;				\n" +

			// Main
			"void main(void) { 																		\n" +
			"	vec4 color = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)); 			\n" +	// Get color for pixel.
			"	vec2 coord = vec2(vTextureCoord.s * uImageRect.z, vTextureCoord.t * uImageRect.w); 	\n" +	// Get position of pixel on main image.
			"	coord += vec2(uMaskRect.x - uImageRect.x, (uMaskRect.y - uImageRect.y)); 			\n" + 	// Shift point by mask offset.
			"	coord.y -= uImageRect.w - uMaskRect.w;												\n" + 	// Invert Y position of mask.
			"	coord /= vec2(uMaskRect.z, uMaskRect.w);											\n" +	// Revert point to 0-1 value.
			"	if(coord.x <= 1. && coord.x >= 0. && coord.y <= 1. && coord.y >= 0.) {				\n" +	// No alpha if point outside mask.
			"		gl_FragColor = vec4(color.rgb, color.a * texture2D(uSamplerMask, coord).a);		\n" +
			"	} else {																			\n" +
			"		gl_FragColor = vec4(color.rgb, 0.);												\n" +
			"	}																					\n" +
			"}";
	};

createjs.AlphaMaskFilter = AlphaMaskFilter;
}());