Skip to content

Commit 50516e3

Browse files
committed
Testing CanvasPool.
1 parent 0493e19 commit 50516e3

13 files changed

Lines changed: 194 additions & 27 deletions

File tree

build/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
5757
<script src="$path/src/pixi/utils/Utils.js"></script>
5858
<script src="$path/src/pixi/utils/Polyk.js"></script>
59+
<script src="$path/src/pixi/utils/CanvasPool.js"></script>
5960
6061
<script src="$path/src/pixi/renderers/webgl/utils/WebGLShaderUtils.js"></script>
6162
<script src="$path/src/pixi/renderers/webgl/shaders/PixiShader.js"></script>
@@ -73,6 +74,7 @@
7374
<script src="$path/src/pixi/renderers/webgl/utils/WebGLFastSpriteBatch.js"></script>
7475
<script src="$path/src/pixi/renderers/webgl/utils/WebGLFilterManager.js"></script>
7576
<script src="$path/src/pixi/renderers/webgl/utils/FilterTexture.js"></script>
77+
7678
<script src="$path/src/pixi/renderers/canvas/utils/CanvasBuffer.js"></script>
7779
<script src="$path/src/pixi/renderers/canvas/utils/CanvasMaskManager.js"></script>
7880
<script src="$path/src/pixi/renderers/canvas/utils/CanvasTinter.js"></script>

src/gameobjects/Text.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Phaser.Text = function (game, x, y, text, style) {
7373
/**
7474
* @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered.
7575
*/
76-
this.canvas = document.createElement('canvas');
76+
this.canvas = PIXI.CanvasPool.create(this);
7777

7878
/**
7979
* @property {HTMLCanvasElement} context - The context of the canvas element that the text is rendered to.
@@ -201,15 +201,17 @@ Phaser.Text.prototype.destroy = function (destroyChildren) {
201201

202202
this.texture.destroy(true);
203203

204-
if (this.canvas && this.canvas.parentNode)
205-
{
206-
this.canvas.parentNode.removeChild(this.canvas);
207-
}
208-
else
209-
{
210-
this.canvas = null;
211-
this.context = null;
212-
}
204+
PIXI.CanvasPool.remove(this);
205+
206+
// if (this.canvas && this.canvas.parentNode)
207+
// {
208+
// this.canvas.parentNode.removeChild(this.canvas);
209+
// }
210+
// else
211+
// {
212+
// this.canvas = null;
213+
// this.context = null;
214+
// }
213215

214216
Phaser.Component.Destroy.prototype.destroy.call(this, destroyChildren);
215217

@@ -1973,5 +1975,5 @@ Object.defineProperty(Phaser.Text.prototype, 'height', {
19731975

19741976
Phaser.Text.fontPropertiesCache = {};
19751977

1976-
Phaser.Text.fontPropertiesCanvas = document.createElement('canvas');
1978+
Phaser.Text.fontPropertiesCanvas = PIXI.CanvasPool.create(Phaser.Text.fontPropertiesCanvas);
19771979
Phaser.Text.fontPropertiesContext = Phaser.Text.fontPropertiesCanvas.getContext('2d');

src/input/Input.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ Phaser.Input.prototype = {
406406

407407
this.activePointer = this.mousePointer;
408408

409-
this.hitCanvas = document.createElement('canvas');
410-
this.hitCanvas.width = 1;
411-
this.hitCanvas.height = 1;
409+
this.hitCanvas = PIXI.CanvasPool.create(this, 1, 1);
412410
this.hitContext = this.hitCanvas.getContext('2d');
413411

414412
this.mouse.start();
@@ -454,6 +452,8 @@ Phaser.Input.prototype = {
454452

455453
this.moveCallbacks = [];
456454

455+
PIXI.CanvasPool.remove(this);
456+
457457
this.game.canvas.removeEventListener('click', this._onClickTrampoline);
458458

459459
},

src/pixi/extras/TilingSprite.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ PIXI.TilingSprite.prototype.getBounds = function()
497497

498498
PIXI.TilingSprite.prototype.destroy = function () {
499499

500+
this.canvasBuffer.destroy();
501+
500502
PIXI.Sprite.prototype.destroy.call(this);
501503

502504
this.tileScale = null;

src/pixi/renderers/canvas/CanvasRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ PIXI.CanvasRenderer = function(width, height, options)
107107
* @property view
108108
* @type HTMLCanvasElement
109109
*/
110-
this.view = options.view || document.createElement( "canvas" );
110+
this.view = options.view || PIXI.CanvasPool.create(this, this.width, this.height);
111111

112112
/**
113113
* The canvas 2d context that everything is drawn with
114114
* @property context
115115
* @type CanvasRenderingContext2D
116116
*/
117-
this.context = this.view.getContext( "2d", { alpha: this.transparent } );
117+
this.context = this.view.getContext("2d", { alpha: this.transparent } );
118118

119119
/**
120120
* Boolean flag controlling canvas refresh.

src/pixi/renderers/canvas/utils/CanvasBuffer.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PIXI.CanvasBuffer = function(width, height)
3434
* @property canvas
3535
* @type HTMLCanvasElement
3636
*/
37-
this.canvas = document.createElement("canvas");
37+
this.canvas = PIXI.CanvasPool.create(this, this.width, this.height);
3838

3939
/**
4040
* A CanvasRenderingContext2D object representing a two-dimensional rendering context.
@@ -74,3 +74,13 @@ PIXI.CanvasBuffer.prototype.resize = function(width, height)
7474
this.width = this.canvas.width = width;
7575
this.height = this.canvas.height = height;
7676
};
77+
78+
/**
79+
* Frees the canvas up for use again.
80+
*
81+
* @method destroy
82+
*/
83+
PIXI.CanvasBuffer.prototype.destroy = function()
84+
{
85+
PIXI.CanvasPool.remove(this);
86+
};

src/pixi/renderers/canvas/utils/CanvasTinter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PIXI.CanvasTinter = function() {};
2121
*/
2222
PIXI.CanvasTinter.getTintedTexture = function(sprite, color)
2323
{
24-
var canvas = sprite.tintedTexture || document.createElement("canvas");
24+
var canvas = sprite.tintedTexture || PIXI.CanvasPool.create(this);
2525

2626
PIXI.CanvasTinter.tintMethod(sprite.texture, color, canvas);
2727

src/pixi/renderers/webgl/WebGLRenderer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ PIXI.WebGLRenderer = function(width, height, options)
119119
* @property view
120120
* @type HTMLCanvasElement
121121
*/
122-
this.view = options.view || document.createElement('canvas');
122+
this.view = options.view || PIXI.CanvasPool.create(this, this.width, this.height);
123123

124124
/**
125125
* @property _contextOptions
@@ -445,6 +445,8 @@ PIXI.WebGLRenderer.prototype.destroy = function()
445445
this.gl = null;
446446
this.renderSession = null;
447447

448+
PIXI.CanvasPool.remove(this);
449+
448450
PIXI.instances[this.glContextId] = null;
449451

450452
PIXI.WebGLRenderer.glContextId--;

src/pixi/textures/BaseTexture.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ PIXI.BaseTexture.prototype.destroy = function()
171171
}
172172
else if (this.source && this.source._pixiId)
173173
{
174+
PIXI.CanvasPool.removeByCanvas(this.source);
175+
174176
delete PIXI.BaseTextureCache[this.source._pixiId];
175177
}
176178

@@ -254,7 +256,7 @@ PIXI.BaseTexture.fromImage = function(imageUrl, crossorigin, scaleMode)
254256
{
255257
// new Image() breaks tex loading in some versions of Chrome.
256258
// See https://code.google.com/p/chromium/issues/detail?id=238071
257-
var image = new Image();//document.createElement('img');
259+
var image = new Image();
258260

259261
if (crossorigin)
260262
{
@@ -287,7 +289,7 @@ PIXI.BaseTexture.fromImage = function(imageUrl, crossorigin, scaleMode)
287289
*/
288290
PIXI.BaseTexture.fromCanvas = function(canvas, scaleMode)
289291
{
290-
if(!canvas._pixiId)
292+
if (!canvas._pixiId)
291293
{
292294
canvas._pixiId = 'canvas_' + PIXI.TextureCacheIdGenerator++;
293295
}
@@ -304,7 +306,7 @@ PIXI.BaseTexture.fromCanvas = function(canvas, scaleMode)
304306

305307
var baseTexture = PIXI.BaseTextureCache[canvas._pixiId];
306308

307-
if(!baseTexture)
309+
if (!baseTexture)
308310
{
309311
baseTexture = new PIXI.BaseTexture(canvas, scaleMode);
310312
PIXI.BaseTextureCache[canvas._pixiId] = baseTexture;

src/pixi/textures/Texture.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ PIXI.Texture.fromCanvas = function(canvas, scaleMode)
305305
var baseTexture = PIXI.BaseTexture.fromCanvas(canvas, scaleMode);
306306

307307
return new PIXI.Texture(baseTexture);
308-
309308
};
310309

311310
/**

0 commit comments

Comments
 (0)