Skip to content

Commit b17d367

Browse files
committed
Fixed Smoothing so it doesn't use the CanvasPool.
1 parent 74e4357 commit b17d367

1 file changed

Lines changed: 54 additions & 21 deletions

File tree

v3/src/dom/Smoothing.js

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var CanvasPool = require('./CanvasPool');
21

3-
var Smoothing = {
2+
// Browser specific prefix, so not going to change between contexts, only between browsers
3+
var prefix = '';
44

5+
var Smoothing = function ()
6+
{
57
/**
68
* Gets the Smoothing Enabled vendor prefix being used on the given context, or null if not set.
79
* iife
@@ -10,29 +12,49 @@ var Smoothing = {
1012
* @param {CanvasRenderingContext2D} context - The context to enable or disable the image smoothing on.
1113
* @return {string|null} Returns the smoothingEnabled vendor prefix, or null if not set on the context.
1214
*/
13-
prefix: (function ()
15+
var getPrefix = function (context)
1416
{
15-
var canvas = CanvasPool.create(this, 1, 1);
16-
var ctx = canvas.getContext('2d');
17-
1817
var vendors = [ 'i', 'webkitI', 'msI', 'mozI', 'oI' ];
1918

20-
vendors.forEach(function (vendor)
19+
for (var i = 0; i < vendors.length; i++)
2120
{
22-
var s = vendor + 'mageSmoothingEnabled';
21+
var s = vendors[i] + 'mageSmoothingEnabled';
2322

24-
if (s in ctx)
23+
if (s in context)
2524
{
26-
CanvasPool.remove(canvas);
2725
return s;
2826
}
29-
});
30-
31-
CanvasPool.remove(canvas);
27+
}
3228

3329
return null;
30+
};
31+
32+
/**
33+
* Sets the Image Smoothing property on the given context. Set to false to disable image smoothing.
34+
* By default browsers have image smoothing enabled, which isn't always what you visually want, especially
35+
* when using pixel art in a game. Note that this sets the property on the context itself, so that any image
36+
* drawn to the context will be affected. This sets the property across all current browsers but support is
37+
* patchy on earlier browsers, especially on mobile.
38+
*
39+
* @method Phaser.Canvas.setSmoothingEnabled
40+
* @param {CanvasRenderingContext2D} context - The context to enable or disable the image smoothing on.
41+
* @param {boolean} value - If set to true it will enable image smoothing, false will disable it.
42+
* @return {CanvasRenderingContext2D} Returns the source context.
43+
*/
44+
var enable = function (context)
45+
{
46+
if (prefix === '')
47+
{
48+
prefix = getPrefix(context);
49+
}
3450

35-
})(),
51+
if (prefix)
52+
{
53+
context[prefix] = true;
54+
}
55+
56+
return context;
57+
};
3658

3759
/**
3860
* Sets the Image Smoothing property on the given context. Set to false to disable image smoothing.
@@ -46,15 +68,20 @@ var Smoothing = {
4668
* @param {boolean} value - If set to true it will enable image smoothing, false will disable it.
4769
* @return {CanvasRenderingContext2D} Returns the source context.
4870
*/
49-
enable: function (context, value)
71+
var disable = function (context)
5072
{
51-
if (Smoothing.prefix)
73+
if (prefix === '')
74+
{
75+
prefix = getPrefix(context);
76+
}
77+
78+
if (prefix)
5279
{
53-
context[Smoothing.prefix] = value;
80+
context[prefix] = false;
5481
}
5582

5683
return context;
57-
},
84+
};
5885

5986
/**
6087
* Returns `true` if the given context has image smoothing enabled, otherwise returns `false`.
@@ -64,11 +91,17 @@ var Smoothing = {
6491
* @param {CanvasRenderingContext2D} context - The context to check for smoothing on.
6592
* @return {boolean} True if the given context has image smoothing enabled, otherwise false.
6693
*/
67-
isEnabled: function (context)
94+
var isEnabled = function (context)
6895
{
69-
return (Smoothing.prefix !== null) ? context[Smoothing.prefix] : null;
70-
}
96+
return (prefix !== null) ? context[prefix] : null;
97+
};
7198

99+
return {
100+
disable: disable,
101+
enable: enable,
102+
getPrefix: getPrefix,
103+
isEnabled: isEnabled
104+
};
72105
};
73106

74107
module.exports = Smoothing();

0 commit comments

Comments
 (0)