API Documentation for: 0.8.2
Show:

File:easeljs\filters\webgl\AlphaMapFilter.js

/*
 * AlphaMapFilter
 * 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 a greyscale alpha map image (or canvas) to the target, such that the alpha channel of the result will
	 * be copied from the red channel of the map, and the RGB channels will be copied from the target.
	 *
	 * Generally, it is recommended that you use {{#crossLink "AlphaMaskFilter"}}{{/crossLink}}, because it has much
	 * better performance.
	 *
	 * <h4>Example</h4>
	 * This example draws a red->blue box, caches it, and then uses the cache canvas as an alpha map on a 100x100 image.
	 *
	 *       var box = new createjs.Shape();
	 *       box.graphics.beginLinearGradientFill(["#ff0000", "#0000ff"], [0, 1], 0, 0, 0, 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.AlphaMapFilter(box.cacheCanvas)
	 *       ];
	 *       bmp.cache(0, 0, 100, 100);
	 *       stage.addChild(bmp);
	 *
	 * See {{#crossLink "Filter"}}{{/crossLink}} for more information on applying filters.
	 * @class AlphaMapFilter
	 * @extends Filter
	 * @constructor
	 * @param {Image|HTMLCanvasElement} alphaMap The greyscale image (or canvas) to use as the alpha value for the
	 * result. This should be exactly the same dimensions as the target.
	 **/
	var AlphaMapFilter = function (alphaMap) {
		this.initialize(alphaMap);
	};
	var p = AlphaMapFilter.prototype = new createjs.Filter();

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

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

// public properties:

	/**
	 * The greyscale image (or canvas) to use as the alpha value for the result. This should be exactly the same
	 * dimensions as the target.
	 * @property alphaMap
	 * @type Image|HTMLCanvasElement
	 **/
	p.alphaMap = null;

// private properties:
	p._alphaMap = null;
	p._mapData = null;

// public methods:

	p.Filter_applyFilter = p.applyFilter;
	/**
	 * Applies the filter to the specified context.
	 * @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 Optional. The 2D context to draw the result to. Defaults to the context passed to ctx.
	 * @param {Number} targetX Optional. The x position to draw the result to. Defaults to the value passed to x.
	 * @param {Number} targetY Optional. The y position to draw the result to. Defaults to the value passed to y.
	 * @return {Boolean}
	 **/
	p.applyFilter = function(ctx, x, y, width, height, targetCtx, targetX, targetY) {
		if (!this.alphaMap) { return true; }
		targetCtx = targetCtx || ctx;
		if (targetX == null) { targetX = x; }
		if (targetY == null) { targetY = y; }

		if (!this._prepAlphaMap()) { return false; }

		var wctx = this._workContext;
		if(this.useGL && wctx) {
			// TODO: In WebGL, the map 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 map = this.alphaMap;

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

			wctx.uniform4f(wctx.imageRectUniform, targetX, targetY, source.width, source.height);
			wctx.uniform4f(wctx.mapRectUniform, targetX, targetY, map.width, map.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;
		}

		try {
			var imageData = ctx.getImageData(x, y, width, height);
		} catch (e) {
			//if (!this.suppressCrossDomainErrors) throw new Error("unable to access local image data: " + e);
			return false;
		}
		var data = imageData.data;
		var map = this._mapData;
		var l = data.length;
		for(var i = 0; i < l; i += 4) {
			data[i + 3] = map[i] || 0;
		}
		imageData.data = data;
		targetCtx.putImageData(imageData, targetX, targetY);
		return true;
	};

	/**
	 * Returns a clone of this object.
	 * @method clone
	 * @return {AlphaMapFilter} A clone of the current AlphaMapFilter instance.
	 **/
	p.clone = function () {
		return new AlphaMapFilter(this.alphaMap);
	};

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

// private methods:
	p._prepAlphaMap = function () {
		if (!this.alphaMap) {
			return false;
		}
		if (this.alphaMap == this._alphaMap && this._mapData) {
			return true;
		}

		this._mapData = null;
		var map = this._alphaMap = this.alphaMap;
		var canvas = map;
		var ctx;
		if (map instanceof HTMLCanvasElement) {
			ctx = canvas.getContext("2d");
		} else {
			canvas = createjs.createCanvas ? createjs.createCanvas() : document.createElement("canvas");
			canvas.width = map.width;
			canvas.height = map.height;
			ctx = canvas.getContext("2d");
			ctx.drawImage(map, 0, 0);
		}

		try {
			var imgData = ctx.getImageData(0, 0, map.width, map.height);
		} catch (e) {
			//if (!this.suppressCrossDomainErrors) throw new Error("unable to access local image data: " + e);
			return false;
		}
		this._mapData = imgData.data;
		return true;
	};

// private methods:

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

		ctx.imageRectUniform = 	ctx.getUniformLocation(program, "uImageRect");
		ctx.mapRectUniform = 	ctx.getUniformLocation(program, "uMapRect");
	};

	/**
	 * @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 uSamplerMap;			\n" +

			"uniform vec4 uImageRect;				\n" +
			"uniform vec4 uMapRect;					\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(uMapRect.x - uImageRect.x, (uMapRect.y - uImageRect.y)); 				\n" +	// Shift point by mask offset.
			"	coord.y -= uImageRect.w - uMapRect.w;												\n" +	// Invert Y position of mask.
			"	coord /= vec2(uMapRect.z, uMapRect.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(uSamplerMap, coord).r);		\n" +
			"	} else {																			\n" +
			"		gl_FragColor = vec4(color.rgb, 0.);												\n" +
			"	}																					\n" +
			"}";
	};

}());