2013-08-29 16:20:59 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-08-29 16:20:59 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
2015-08-03 15:09:16 +00:00
|
|
|
* The Canvas class handles everything related to creating the `canvas` DOM tag that Phaser will use,
|
|
|
|
* including styles, offset and aspect ratio.
|
2013-09-19 14:34:48 +00:00
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @class Phaser.Canvas
|
2013-09-19 14:34:48 +00:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
Phaser.Canvas = {
|
2013-10-03 00:21:08 +00:00
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
2013-12-04 22:39:53 +00:00
|
|
|
* Creates a `canvas` DOM element. The element is not automatically added to the document.
|
2013-09-19 14:34:48 +00:00
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.create
|
2015-08-03 15:09:16 +00:00
|
|
|
* @param {object} parent - The object that will own the canvas that is created.
|
2013-12-04 22:39:53 +00:00
|
|
|
* @param {number} [width=256] - The width of the canvas element.
|
|
|
|
* @param {number} [height=256] - The height of the canvas element..
|
2014-12-31 17:04:55 +00:00
|
|
|
* @param {string} [id=(none)] - If specified, and not the empty string, this will be set as the ID of the canvas element. Otherwise no ID will be set.
|
2015-08-03 15:09:16 +00:00
|
|
|
* @param {boolean} [skipPool=false] - If `true` the canvas will not be placed in the CanvasPool global.
|
2013-12-04 22:39:53 +00:00
|
|
|
* @return {HTMLCanvasElement} The newly created canvas element.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2015-08-03 15:09:16 +00:00
|
|
|
create: function (parent, width, height, id, skipPool) {
|
2013-10-18 14:12:32 +00:00
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
width = width || 256;
|
|
|
|
height = height || 256;
|
|
|
|
|
2015-08-03 15:09:16 +00:00
|
|
|
if (skipPool === undefined)
|
2013-12-04 22:39:53 +00:00
|
|
|
{
|
2015-08-03 15:09:16 +00:00
|
|
|
var canvas = PIXI.CanvasPool.create(parent, width, height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var canvas = document.createElement('canvas');
|
2013-12-04 22:39:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 15:09:16 +00:00
|
|
|
if (typeof id === 'string' && id !== '')
|
|
|
|
{
|
|
|
|
canvas.id = id;
|
|
|
|
}
|
2013-09-09 11:35:09 +00:00
|
|
|
|
2015-08-24 10:32:32 +00:00
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
canvas.style.display = 'block';
|
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the background color behind the canvas. This changes the canvas style property.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setBackgroundColor
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to set the background color on.
|
|
|
|
* @param {string} [color] - The color to set. Can be in the format 'rgb(r,g,b)', or '#RRGGBB' or any valid CSS color.
|
2013-08-29 16:20:59 +00:00
|
|
|
* @return {HTMLCanvasElement} Returns the source canvas.
|
|
|
|
*/
|
|
|
|
setBackgroundColor: function (canvas, color) {
|
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
color = color || 'rgb(0,0,0)';
|
2013-08-29 16:20:59 +00:00
|
|
|
|
|
|
|
canvas.style.backgroundColor = color;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the touch-action property on the canvas style. Can be used to disable default browser touch actions.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setTouchAction
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to set the touch action on.
|
2014-11-29 19:40:35 +00:00
|
|
|
* @param {string} [value] - The touch action to set. Defaults to 'none'.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {HTMLCanvasElement} The source canvas.
|
2013-08-29 16:20:59 +00:00
|
|
|
*/
|
|
|
|
setTouchAction: function (canvas, value) {
|
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
value = value || 'none';
|
2013-08-29 16:20:59 +00:00
|
|
|
|
|
|
|
canvas.style.msTouchAction = value;
|
|
|
|
canvas.style['ms-touch-action'] = value;
|
|
|
|
canvas.style['touch-action'] = value;
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-03 00:21:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the user-select property on the canvas style. Can be used to disable default browser selection actions.
|
|
|
|
*
|
|
|
|
* @method Phaser.Canvas.setUserSelect
|
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to set the touch action on.
|
2014-11-29 19:40:35 +00:00
|
|
|
* @param {string} [value] - The touch action to set. Defaults to 'none'.
|
2013-10-03 00:21:08 +00:00
|
|
|
* @return {HTMLCanvasElement} The source canvas.
|
|
|
|
*/
|
2013-09-30 16:12:22 +00:00
|
|
|
setUserSelect: function (canvas, value) {
|
|
|
|
|
2013-10-03 00:21:08 +00:00
|
|
|
value = value || 'none';
|
|
|
|
|
2013-09-30 16:12:22 +00:00
|
|
|
canvas.style['-webkit-touch-callout'] = value;
|
|
|
|
canvas.style['-webkit-user-select'] = value;
|
|
|
|
canvas.style['-khtml-user-select'] = value;
|
|
|
|
canvas.style['-moz-user-select'] = value;
|
|
|
|
canvas.style['-ms-user-select'] = value;
|
|
|
|
canvas.style['user-select'] = value;
|
|
|
|
canvas.style['-webkit-tap-highlight-color'] = 'rgba(0, 0, 0, 0)';
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
/**
|
|
|
|
* Adds the given canvas element to the DOM. The canvas will be added as a child of the given parent.
|
|
|
|
* If no parent is given it will be added as a child of the document.body.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.addToDOM
|
2014-08-31 00:57:50 +00:00
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to be added to the DOM.
|
2013-12-04 22:39:53 +00:00
|
|
|
* @param {string|HTMLElement} parent - The DOM element to add the canvas to.
|
2013-12-30 16:54:00 +00:00
|
|
|
* @param {boolean} [overflowHidden=true] - If set to true it will add the overflow='hidden' style to the parent DOM element.
|
2013-08-29 16:20:59 +00:00
|
|
|
* @return {HTMLCanvasElement} Returns the source canvas.
|
|
|
|
*/
|
|
|
|
addToDOM: function (canvas, parent, overflowHidden) {
|
|
|
|
|
2013-11-04 21:29:32 +00:00
|
|
|
var target;
|
2013-09-18 05:34:56 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (overflowHidden === undefined) { overflowHidden = true; }
|
2013-08-29 16:20:59 +00:00
|
|
|
|
2013-11-04 21:29:32 +00:00
|
|
|
if (parent)
|
2013-08-29 16:20:59 +00:00
|
|
|
{
|
2013-11-04 21:29:32 +00:00
|
|
|
if (typeof parent === 'string')
|
2013-09-11 10:33:27 +00:00
|
|
|
{
|
2013-12-04 22:39:53 +00:00
|
|
|
// hopefully an element ID
|
2013-11-04 21:29:32 +00:00
|
|
|
target = document.getElementById(parent);
|
|
|
|
}
|
|
|
|
else if (typeof parent === 'object' && parent.nodeType === 1)
|
|
|
|
{
|
2013-12-04 22:39:53 +00:00
|
|
|
// quick test for a HTMLelement
|
2013-11-04 21:29:32 +00:00
|
|
|
target = parent;
|
2013-09-11 10:33:27 +00:00
|
|
|
}
|
2013-08-29 16:20:59 +00:00
|
|
|
}
|
2013-11-04 21:29:32 +00:00
|
|
|
|
2013-12-04 22:39:53 +00:00
|
|
|
// Fallback, covers an invalid ID and a non HTMLelement object
|
|
|
|
if (!target)
|
2013-08-29 16:20:59 +00:00
|
|
|
{
|
2013-11-04 21:29:32 +00:00
|
|
|
target = document.body;
|
2013-08-29 16:20:59 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (overflowHidden && target.style)
|
|
|
|
{
|
|
|
|
target.style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:29:32 +00:00
|
|
|
target.appendChild(canvas);
|
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-09-01 01:38:21 +00:00
|
|
|
/**
|
|
|
|
* Removes the given canvas element from the DOM.
|
|
|
|
*
|
|
|
|
* @method Phaser.Canvas.removeFromDOM
|
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to be removed from the DOM.
|
|
|
|
*/
|
|
|
|
removeFromDOM: function (canvas) {
|
|
|
|
|
|
|
|
if (canvas.parentNode)
|
|
|
|
{
|
|
|
|
canvas.parentNode.removeChild(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the transform of the given canvas to the matrix values provided.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setTransform
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {CanvasRenderingContext2D} context - The context to set the transform on.
|
|
|
|
* @param {number} translateX - The value to translate horizontally by.
|
|
|
|
* @param {number} translateY - The value to translate vertically by.
|
|
|
|
* @param {number} scaleX - The value to scale horizontally by.
|
|
|
|
* @param {number} scaleY - The value to scale vertically by.
|
|
|
|
* @param {number} skewX - The value to skew horizontaly by.
|
|
|
|
* @param {number} skewY - The value to skew vertically by.
|
2013-08-29 16:20:59 +00:00
|
|
|
* @return {CanvasRenderingContext2D} Returns the source context.
|
|
|
|
*/
|
|
|
|
setTransform: function (context, translateX, translateY, scaleX, scaleY, skewX, skewY) {
|
|
|
|
|
|
|
|
context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY);
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Image Smoothing property on the given context. Set to false to disable image smoothing.
|
|
|
|
* By default browsers have image smoothing enabled, which isn't always what you visually want, especially
|
|
|
|
* when using pixel art in a game. Note that this sets the property on the context itself, so that any image
|
|
|
|
* drawn to the context will be affected. This sets the property across all current browsers but support is
|
|
|
|
* patchy on earlier browsers, especially on mobile.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setSmoothingEnabled
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {CanvasRenderingContext2D} context - The context to enable or disable the image smoothing on.
|
2013-10-01 15:39:39 +00:00
|
|
|
* @param {boolean} value - If set to true it will enable image smoothing, false will disable it.
|
2013-08-29 16:20:59 +00:00
|
|
|
* @return {CanvasRenderingContext2D} Returns the source context.
|
|
|
|
*/
|
|
|
|
setSmoothingEnabled: function (context, value) {
|
|
|
|
|
2015-09-15 12:43:52 +00:00
|
|
|
var vendor = [ 'i', 'webkitI', 'msI', 'mozI', 'oI' ];
|
2015-07-10 03:02:21 +00:00
|
|
|
|
|
|
|
for (var prefix in vendor)
|
|
|
|
{
|
|
|
|
var s = vendor[prefix] + 'mageSmoothingEnabled';
|
|
|
|
|
|
|
|
if (s in context)
|
|
|
|
{
|
|
|
|
context[s] = value;
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 16:20:59 +00:00
|
|
|
|
|
|
|
return context;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-09-15 12:43:52 +00:00
|
|
|
/**
|
|
|
|
* Gets the Smoothing Enabled vendor prefix being used on the given context, or null if not set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Canvas.getSmoothingPrefix
|
|
|
|
* @param {CanvasRenderingContext2D} context - The context to enable or disable the image smoothing on.
|
|
|
|
* @return {string|null} Returns the smoothingEnabled vendor prefix, or null if not set on the context.
|
|
|
|
*/
|
|
|
|
getSmoothingPrefix: function (context) {
|
|
|
|
|
|
|
|
var vendor = [ 'i', 'webkitI', 'msI', 'mozI', 'oI' ];
|
|
|
|
|
|
|
|
for (var prefix in vendor)
|
|
|
|
{
|
|
|
|
var s = vendor[prefix] + 'mageSmoothingEnabled';
|
|
|
|
|
|
|
|
if (s in context)
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-09-04 04:04:54 +00:00
|
|
|
/**
|
|
|
|
* Returns `true` if the given context has image smoothing enabled, otherwise returns `false`.
|
|
|
|
*
|
|
|
|
* @method Phaser.Canvas.getSmoothingEnabled
|
|
|
|
* @param {CanvasRenderingContext2D} context - The context to check for smoothing on.
|
|
|
|
* @return {boolean} True if the given context has image smoothing enabled, otherwise false.
|
|
|
|
*/
|
2014-09-03 19:56:31 +00:00
|
|
|
getSmoothingEnabled: function (context) {
|
|
|
|
|
2015-09-15 12:43:52 +00:00
|
|
|
return (context['imageSmoothingEnabled'] || context['webkitImageSmoothingEnabled'] || context['msImageSmoothingEnabled'] || context['mozImageSmoothingEnabled'] || context['oImageSmoothingEnabled']);
|
2014-09-03 19:56:31 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 16:20:59 +00:00
|
|
|
/**
|
2014-09-03 19:56:31 +00:00
|
|
|
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast' on webkit).
|
2013-08-29 16:20:59 +00:00
|
|
|
* Note that if this doesn't given the desired result then see the setSmoothingEnabled.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setImageRenderingCrisp
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {HTMLCanvasElement} canvas - The canvas to set image-rendering crisp on.
|
2013-08-29 16:20:59 +00:00
|
|
|
* @return {HTMLCanvasElement} Returns the source canvas.
|
|
|
|
*/
|
|
|
|
setImageRenderingCrisp: function (canvas) {
|
|
|
|
|
2014-01-09 00:59:37 +00:00
|
|
|
canvas.style['image-rendering'] = 'optimizeSpeed';
|
2013-08-29 16:20:59 +00:00
|
|
|
canvas.style['image-rendering'] = 'crisp-edges';
|
|
|
|
canvas.style['image-rendering'] = '-moz-crisp-edges';
|
|
|
|
canvas.style['image-rendering'] = '-webkit-optimize-contrast';
|
2014-01-09 00:59:37 +00:00
|
|
|
canvas.style['image-rendering'] = 'optimize-contrast';
|
2014-09-03 21:16:03 +00:00
|
|
|
canvas.style['image-rendering'] = 'pixelated';
|
2013-08-29 16:20:59 +00:00
|
|
|
canvas.style.msInterpolationMode = 'nearest-neighbor';
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the CSS image-rendering property on the given canvas to be 'bicubic' (aka 'auto').
|
|
|
|
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
|
|
|
|
*
|
2013-10-03 00:21:08 +00:00
|
|
|
* @method Phaser.Canvas.setImageRenderingBicubic
|
2013-08-29 16:20:59 +00:00
|
|
|
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering bicubic on.
|
|
|
|
* @return {HTMLCanvasElement} Returns the source canvas.
|
|
|
|
*/
|
|
|
|
setImageRenderingBicubic: function (canvas) {
|
|
|
|
|
|
|
|
canvas.style['image-rendering'] = 'auto';
|
|
|
|
canvas.style.msInterpolationMode = 'bicubic';
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|