Skip to content

Commit 14d2c6a

Browse files
committed
Updated CanvasPool to store the type (2d or webgl), so it doesn't give incorrect canvases back out again. Updated Features to use proper canvases when feature testing. Added Interpolation code. Cached the Smoothing prefix.
1 parent e450966 commit 14d2c6a

6 files changed

Lines changed: 133 additions & 98 deletions

File tree

v3/src/boot/Game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ Game.prototype = {
8787

8888
console.log(CHECKSUM.build);
8989

90-
console.log('pool', CanvasPool.getTotal());
91-
console.log('free', CanvasPool.getFree());
90+
console.log('pool', CanvasPool.total());
91+
console.log('free', CanvasPool.free());
9292

9393
// Add in ability to specify pre-init and post-init callbacks in the config
9494

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '0a915bb0-b376-11e6-8e00-5b1f6d1d763b'
2+
build: '1e855860-b380-11e6-ae1d-777159c5bd92'
33
};
44
module.exports = CHECKSUM;

v3/src/device/Features.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,31 @@ function init ()
136136
{
137137
try
138138
{
139-
var canvas = CanvasPool.create(this, 1, 1);
139+
var canvas = CanvasPool.createWebGL(this);
140140

141-
// cocoon ...
142-
canvas.screencanvas = false;
141+
if (OS.cocoonJS)
142+
{
143+
canvas.screencanvas = false;
144+
}
143145

144146
var ctx = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
145147

146-
var image = ctx.createImageData(1, 1);
148+
var canvas2D = CanvasPool.create2D(this);
147149

150+
var ctx2D = canvas2D.getContext('2d');
151+
152+
// Can't be done on a webgl context
153+
var image = ctx2D.createImageData(1, 1);
154+
148155
/**
149156
* Test to see if ImageData uses CanvasPixelArray or Uint8ClampedArray.
150157
*
151158
* @author Matt DesLauriers (@mattdesl)
152159
*/
153160
isUint8 = image.data instanceof Uint8ClampedArray;
154161

155-
CanvasPool.removeByCanvas(canvas);
162+
CanvasPool.remove(canvas);
163+
CanvasPool.remove(canvas2D);
156164

157165
return (ctx !== null);
158166
}

v3/src/dom/CanvasInterpolation.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var CanvasInterpolation = {
2+
3+
/**
4+
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast' on webkit).
5+
* Note that if this doesn't given the desired result then see the setSmoothingEnabled.
6+
*
7+
* @method Phaser.Canvas.setImageRenderingCrisp
8+
* @param {HTMLCanvasElement} canvas - The canvas to set image-rendering crisp on.
9+
* @return {HTMLCanvasElement} Returns the source canvas.
10+
*/
11+
setCrisp: function (canvas)
12+
{
13+
var types = [ 'optimizeSpeed', 'crisp-edges', '-moz-crisp-edges', '-webkit-optimize-contrast', 'optimize-contrast', 'pixelated' ];
14+
15+
types.forEach(function(type)
16+
{
17+
canvas.style['image-rendering'] = type;
18+
});
19+
20+
canvas.style.msInterpolationMode = 'nearest-neighbor';
21+
22+
return canvas;
23+
},
24+
25+
/**
26+
* Sets the CSS image-rendering property on the given canvas to be 'bicubic' (aka 'auto').
27+
* Note that if this doesn't given the desired result then see the CanvasUtils.setSmoothingEnabled method.
28+
*
29+
* @method Phaser.Canvas.setImageRenderingBicubic
30+
* @param {HTMLCanvasElement} canvas The canvas to set image-rendering bicubic on.
31+
* @return {HTMLCanvasElement} Returns the source canvas.
32+
*/
33+
setBicubic: function (canvas)
34+
{
35+
canvas.style['image-rendering'] = 'auto';
36+
canvas.style.msInterpolationMode = 'bicubic';
37+
38+
return canvas;
39+
}
40+
41+
};
42+
43+
module.exports = CanvasInterpolation;

v3/src/dom/CanvasPool.js

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var CONST = require('../const');
8+
79
/**
810
* The pool into which the canvas elements are placed.
911
*
@@ -35,18 +37,23 @@ var CanvasPool = function ()
3537
* @param {number} height - The height of the canvas element.
3638
* @return {HTMLCanvasElement} The canvas element.
3739
*/
38-
var create = function (parent, width, height)
40+
var create = function (parent, width, height, type)
3941
{
40-
var idx = first();
41-
var canvas;
42+
if (width === undefined) { width = 1; }
43+
if (height === undefined) { height = 1; }
44+
if (type === undefined) { type = CONST.CANVAS; }
4245

43-
console.log('CanvasPool.create', idx);
46+
var canvas;
47+
var container = first(type);
4448

45-
if (idx === -1)
49+
if (container === null)
4650
{
47-
var container = {
51+
console.log('CanvasPool.create new');
52+
53+
container = {
4854
parent: parent,
49-
canvas: document.createElement('canvas')
55+
canvas: document.createElement('canvas'),
56+
type: type
5057
};
5158

5259
pool.push(container);
@@ -55,9 +62,11 @@ var CanvasPool = function ()
5562
}
5663
else
5764
{
58-
pool[idx].parent = parent;
65+
console.log('CanvasPool.create existing');
66+
67+
container.parent = parent;
5968

60-
canvas = pool[idx].canvas;
69+
canvas = container.canvas;
6170
}
6271

6372
if (width !== undefined)
@@ -69,24 +78,36 @@ var CanvasPool = function ()
6978
return canvas;
7079
};
7180

81+
var create2D = function (parent, width, height)
82+
{
83+
return create(parent, width, height, CONST.CANVAS);
84+
};
85+
86+
var createWebGL = function (parent, width, height)
87+
{
88+
return create(parent, width, height, CONST.WEBGL);
89+
};
90+
7291
/**
7392
* Gets the first free canvas index from the pool.
7493
*
7594
* @static
7695
* @method Phaser.CanvasPool.getFirst
7796
* @return {number}
7897
*/
79-
var first = function ()
98+
var first = function (type)
8099
{
81-
for (var i = 0; i < pool.length; i++)
100+
if (type === undefined) { type = CONST.CANVAS; }
101+
102+
pool.forEach(function (container)
82103
{
83-
if (!pool[i].parent)
104+
if (!container.parent && container.type === type)
84105
{
85-
return i;
106+
return container;
86107
}
87-
}
108+
});
88109

89-
return -1;
110+
return null;
90111
};
91112

92113
/**
@@ -95,49 +116,23 @@ var CanvasPool = function ()
95116
*
96117
* @static
97118
* @method Phaser.CanvasPool.remove
98-
* @param {any} parent - The parent of the canvas element.
119+
* @param {any|HTMLCanvasElement} parent - The parent of the canvas element.
99120
*/
100121
var remove = function (parent)
101122
{
102-
// Check to see if the parent is a canvas object, then do removeByCanvas stuff instead
103-
// CanvasRenderingContext2D
104-
105-
for (var i = 0; i < pool.length; i++)
106-
{
107-
if (pool[i].parent === parent)
108-
{
109-
pool[i].parent = null;
110-
pool[i].canvas.width = 1;
111-
pool[i].canvas.height = 1;
112-
}
113-
}
114-
115-
};
116-
117-
/**
118-
* Looks up a canvas based on its type, and if found puts it back in the pool, freeing it up for re-use.
119-
* The canvas has its width and height set to 1, and its parent attribute nulled.
120-
*
121-
* @static
122-
* @method Phaser.CanvasPool.removeByCanvas
123-
* @param {HTMLCanvasElement} canvas - The canvas element to remove.
124-
*/
125-
var removeByCanvas = function (canvas)
126-
{
127-
console.log('removeByCanvas');
123+
// Check to see if the parent is a canvas object
124+
var isCanvas = parent instanceof HTMLCanvasElement;
128125

129-
for (var i = 0; i < pool.length; i++)
126+
pool.forEach(function (container)
130127
{
131-
if (pool[i].canvas === canvas)
128+
if ((isCanvas && container.canvas === parent) || (!isCanvas && container.parent === parent))
132129
{
133-
console.log('found and removed');
134-
135-
pool[i].parent = null;
136-
pool[i].canvas.width = 1;
137-
pool[i].canvas.height = 1;
130+
console.log('CanvasPool.remove found and removed');
131+
container.parent = null;
132+
container.canvas.width = 1;
133+
container.canvas.height = 1;
138134
}
139-
}
140-
135+
});
141136
};
142137

143138
/**
@@ -147,17 +142,17 @@ var CanvasPool = function ()
147142
* @method Phaser.CanvasPool.getTotal
148143
* @return {number} The number of in-use (parented) canvas elements in the pool.
149144
*/
150-
var getTotal = function ()
145+
var total = function ()
151146
{
152147
var c = 0;
153148

154-
for (var i = 0; i < pool.length; i++)
149+
pool.forEach(function (container)
155150
{
156-
if (pool[i].parent)
151+
if (container.parent)
157152
{
158153
c++;
159154
}
160-
}
155+
});
161156

162157
return c;
163158
};
@@ -169,32 +164,22 @@ var CanvasPool = function ()
169164
* @method Phaser.CanvasPool.getFree
170165
* @return {number} The number of free (un-parented) canvas elements in the pool.
171166
*/
172-
var getFree = function ()
167+
var free = function ()
173168
{
174-
var c = 0;
175-
176-
for (var i = 0; i < pool.length; i++)
177-
{
178-
if (!pool[i].parent)
179-
{
180-
c++;
181-
}
182-
}
183-
184-
return c;
169+
return pool.length - total();
185170
};
186171

187172
return {
188173
create: create,
174+
create2D: create2D,
175+
createWebGL: createWebGL,
189176
first: first,
190177
remove: remove,
191-
removeByCanvas: removeByCanvas,
192-
getTotal: getTotal,
193-
getFree: getFree,
178+
total: total,
179+
free: free,
194180
pool: pool
195181
};
196182
};
197183

198184
// If we export the called function here, it'll only be invoked once (not every time it's required).
199-
// This function must return something though
200185
module.exports = CanvasPool();

0 commit comments

Comments
 (0)