1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The CanvasPool is a global static object that allows Pixi and Phaser to pool canvas DOM elements.
*
* @class CanvasPool
* @static
*/
PIXI.CanvasPool = {
/**
* Creates a new Canvas DOM element, or pulls one from the pool if free.
*
* @method create
* @static
* @param parent {any} The parent of the canvas element.
* @param width {number} The width of the canvas element.
* @param height {number} The height of the canvas element.
* @return {HTMLCanvasElement} The canvas element.
*/
create: function (parent, width, height) {
var idx = PIXI.CanvasPool.getFirst();
var canvas;
if (idx === -1)
{
var container = {
parent: parent,
canvas: document.createElement('canvas')
}
PIXI.CanvasPool.pool.push(container);
canvas = container.canvas;
}
else
{
PIXI.CanvasPool.pool[idx].parent = parent;
canvas = PIXI.CanvasPool.pool[idx].canvas;
}
if (width !== undefined)
{
canvas.width = width;
canvas.height = height;
}
return canvas;
},
/**
* Gets the first free canvas index from the pool.
*
* @method getFirst
* @static
* @return {number}
*/
getFirst: function () {
var pool = PIXI.CanvasPool.pool;
for (var i = 0; i < pool.length; i++)
{
if (!pool[i].parent)
{
return i;
}
}
return -1;
},
/**
* Removes the parent from a canvas element from the pool, freeing it up for re-use.
*
* @method remove
* @param parent {any} The parent of the canvas element.
* @static
*/
remove: function (parent) {
var pool = PIXI.CanvasPool.pool;
for (var i = 0; i < pool.length; i++)
{
if (pool[i].parent === parent)
{
pool[i].parent = null;
pool[i].canvas.width = 1;
pool[i].canvas.height = 1;
}
}
},
/**
* Removes the parent from a canvas element from the pool, freeing it up for re-use.
*
* @method removeByCanvas
* @param canvas {HTMLCanvasElement} The canvas element to remove
* @static
*/
removeByCanvas: function (canvas) {
var pool = PIXI.CanvasPool.pool;
for (var i = 0; i < pool.length; i++)
{
if (pool[i].canvas === canvas)
{
pool[i].parent = null;
pool[i].canvas.width = 1;
pool[i].canvas.height = 1;
}
}
},
/**
* Gets the total number of used canvas elements in the pool.
*
* @method getTotal
* @static
* @return {number} The number of in-use (parented) canvas elements in the pool.
*/
getTotal: function () {
var pool = PIXI.CanvasPool.pool;
var c = 0;
for (var i = 0; i < pool.length; i++)
{
if (pool[i].parent)
{
c++;
}
}
return c;
},
/**
* Gets the total number of free canvas elements in the pool.
*
* @method getFree
* @static
* @return {number} The number of free (un-parented) canvas elements in the pool.
*/
getFree: function () {
var pool = PIXI.CanvasPool.pool;
var c = 0;
for (var i = 0; i < pool.length; i++)
{
if (!pool[i].parent)
{
c++;
}
}
return c;
}
};
/**
* The pool into which the canvas dom elements are placed.
*
* @property pool
* @type Array
* @static
*/
PIXI.CanvasPool.pool = [];