The CanvasPool is a global static object, that allows Phaser to recycle and pool 2D Context Canvas DOM elements. It does not pool WebGL Contexts, because once the context options are set they cannot be modified again, which is useless for some of the Phaser pipelines / renderer.
This singleton is instantiated as soon as Phaser loads, before a Phaser.Game instance has even been created. Which means all instances of Phaser Games on the same page can share the one single pool.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 16)
Methods
-
<static> create(parent [, width] [, height] [, canvasType] [, selfParent])
-
Creates a new Canvas DOM element, or pulls one from the pool if free.
Parameters:
Name Type Argument Default Description parent
* The parent of the Canvas object.
width
number <optional>
1 The width of the Canvas.
height
number <optional>
1 The height of the Canvas.
canvasType
number <optional>
Phaser.CANVAS The type of the Canvas. Either
Phaser.CANVAS
orPhaser.WEBGL
.selfParent
boolean <optional>
false Use the generated Canvas element as the parent?
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 29)
Returns:
The canvas element that was created or pulled from the pool
- Type
- HTMLCanvasElement
-
<static> create2D(parent [, width] [, height])
-
Creates a new Canvas DOM element, or pulls one from the pool if free.
Parameters:
Name Type Argument Default Description parent
* The parent of the Canvas object.
width
number <optional>
1 The width of the Canvas.
height
number <optional>
1 The height of the Canvas.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 91)
Returns:
The created canvas.
- Type
- HTMLCanvasElement
-
<static> createWebGL(parent [, width] [, height])
-
Creates a new Canvas DOM element, or pulls one from the pool if free.
Parameters:
Name Type Argument Default Description parent
* The parent of the Canvas object.
width
number <optional>
1 The width of the Canvas.
height
number <optional>
1 The height of the Canvas.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 108)
Returns:
The created WebGL canvas.
- Type
- HTMLCanvasElement
-
<static> disableSmoothing()
-
Disable context smoothing on any new Canvas element created.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 218)
-
<static> enableSmoothing()
-
Enable context smoothing on any new Canvas element created.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 229)
-
<static> first( [canvasType])
-
Gets the first free canvas index from the pool.
Parameters:
Name Type Argument Default Description canvasType
number <optional>
Phaser.CANVAS The type of the Canvas. Either
Phaser.CANVAS
orPhaser.WEBGL
.- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 125)
Returns:
The first free canvas, or
null
if a WebGL canvas was requested or if the pool doesn't have free canvases.- Type
- HTMLCanvasElement
-
<static> free()
-
Gets the total number of free canvas elements in the pool.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 205)
Returns:
The number of free canvases.
- Type
- number
-
<static> remove(parent)
-
Looks up a canvas based on its parent, and if found puts it back in the pool, freeing it up for re-use. The canvas has its width and height set to 1, and its parent attribute nulled.
Parameters:
Name Type Description parent
* The canvas or the parent of the canvas to free.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 157)
-
<static> total()
-
Gets the total number of used canvas elements in the pool.
- Since: 3.0.0
- Source: src/display/canvas/CanvasPool.js (Line 182)
Returns:
The number of used canvases.
- Type
- number