phaser/src/display/canvas/Smoothing.js

127 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
// Browser specific prefix, so not going to change between contexts, only between browsers
var prefix = '';
2018-02-12 21:39:26 +00:00
/**
* @namespace Phaser.Display.Canvas.Smoothing
* @since 3.0.0
*/
var Smoothing = function ()
{
2018-02-12 21:39:26 +00:00
/**
* Gets the Smoothing Enabled vendor prefix being used on the given context, or null if not set.
*
* @function Phaser.Display.Canvas.Smoothing.getPrefix
* @since 3.0.0
2018-03-19 13:03:41 +00:00
*
* @param {CanvasRenderingContext2D|WebGLRenderingContext} context - [description]
*
2018-02-12 21:39:26 +00:00
* @return {string} [description]
*/
var getPrefix = function (context)
{
var vendors = [ 'i', 'webkitI', 'msI', 'mozI', 'oI' ];
for (var i = 0; i < vendors.length; i++)
{
var s = vendors[i] + 'mageSmoothingEnabled';
if (s in context)
{
return s;
}
}
return null;
};
/**
2018-02-12 21:39:26 +00:00
* 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.
*
* @function Phaser.Display.Canvas.Smoothing.enable
* @since 3.0.0
2018-03-19 13:03:41 +00:00
*
* @param {CanvasRenderingContext2D|WebGLRenderingContext} context - [description]
*
* @return {CanvasRenderingContext2D|WebGLRenderingContext} [description]
2018-02-12 21:39:26 +00:00
*/
var enable = function (context)
{
if (prefix === '')
{
prefix = getPrefix(context);
}
if (prefix)
{
context[prefix] = true;
}
return context;
};
/**
2018-02-12 21:39:26 +00:00
* 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.
*
* @function Phaser.Display.Canvas.Smoothing.disable
* @since 3.0.0
2018-03-19 13:03:41 +00:00
*
* @param {CanvasRenderingContext2D|WebGLRenderingContext} context - [description]
*
* @return {CanvasRenderingContext2D|WebGLRenderingContext} [description]
2018-02-12 21:39:26 +00:00
*/
var disable = function (context)
{
if (prefix === '')
{
prefix = getPrefix(context);
}
if (prefix)
{
context[prefix] = false;
}
return context;
};
/**
* Returns `true` if the given context has image smoothing enabled, otherwise returns `false`.
* Returns null if no smoothing prefix is available.
2018-02-12 21:39:26 +00:00
*
* @function Phaser.Display.Canvas.Smoothing.isEnabled
* @since 3.0.0
2018-03-19 13:03:41 +00:00
*
* @param {CanvasRenderingContext2D|WebGLRenderingContext} context - [description]
*
2018-03-20 14:36:03 +00:00
* @return {?boolean} [description]
*/
var isEnabled = function (context)
{
return (prefix !== null) ? context[prefix] : null;
};
return {
disable: disable,
enable: enable,
getPrefix: getPrefix,
isEnabled: isEnabled
};
2018-02-12 21:39:26 +00:00
};
module.exports = Smoothing();