Phaser API Documentation

  Version: 
Filter

A Canvas Texture is a special kind of Texture that is backed by an HTML Canvas Element as its source.

You can use the properties of this texture to draw to the canvas element directly, using all of the standard canvas operations available in the browser. Any Game Object can be given this texture and will render with it.

Note: When running under WebGL the Canvas Texture needs to re-generate its base WebGLTexture and reupload it to the GPU every time you modify it, otherwise the changes you make to this texture will not be visible. To do this you should call CanvasTexture.refresh() once you are finished with your changes to the canvas. Try and keep this to a minimum, especially on large canvas sizes, or you may inadvertently thrash the GPU by constantly uploading texture data to it. This restriction does not apply if using the Canvas Renderer.

It starts with only one frame that covers the whole of the canvas. You can add further frames, that specify sections of the canvas using the add method.

Should you need to resize the canvas use the setSize method so that it accurately updates all of the underlying texture data as well. Forgetting to do this (i.e. by changing the canvas size directly from your code) could cause graphical errors.

Constructor:

new CanvasTexture(manager, key, source, width, height)

Parameters:

name type description
manager Phaser.Textures.TextureManager

A reference to the Texture Manager this Texture belongs to.

key string

The unique string-based key of this Texture.

source HTMLCanvasElement

The canvas element that is used as the base of this texture.

width number

The width of the canvas.

height number

The height of the canvas.

Since: 3.7.0

Extends


Members

<private> _source: Phaser.Textures.TextureSource
Focus
Focus

Description:

A reference to the Texture Source of this Canvas.

Type:
Since: 3.7.0
Focus
Focus
buffer: ArrayBuffer
Focus
Focus

Description:

An ArrayBuffer the same size as the context ImageData.

Type:
ArrayBuffer
Since: 3.13.0
Focus
Focus
<readonly> canvas: HTMLCanvasElement
Focus
Focus

Description:

The source Canvas Element.

Type:
HTMLCanvasElement
Since: 3.7.0
Focus
Focus
<readonly> context: CanvasRenderingContext2D
Focus
Focus

Description:

The 2D Canvas Rendering Context.

Type:
CanvasRenderingContext2D
Since: 3.7.0
Focus
Focus
customData: object
Focus
Focus

Description:

Any additional data that was set in the source JSON (if any), or any extra data you'd like to store relating to this texture

Type:
object
Inherited from: Phaser.Textures.Texture#customData
Since: 3.0.0
Source: src/textures/Texture.js (Line 97)
Focus
Focus
data: Uint8ClampedArray
Focus
Focus

Description:

A Uint8ClampedArray view into the buffer. Use the update method to populate this when the canvas changes. Note that this is unavailable in some browsers, such as Epic Browser, due to their security restrictions.

Type:
Uint8ClampedArray
Since: 3.13.0
Focus
Focus
dataSource: array
Focus
Focus

Description:

An array of TextureSource data instances. Used to store additional data images, such as normal maps or specular maps.

Type:
array
Inherited from: Phaser.Textures.Texture#dataSource
Since: 3.0.0
Source: src/textures/Texture.js (Line 78)
Focus
Focus
firstFrame: string
Focus
Focus

Description:

The name of the first frame of the Texture.

Type:
string
Inherited from: Phaser.Textures.Texture#firstFrame
Since: 3.0.0
Source: src/textures/Texture.js (Line 107)
Focus
Focus
frameTotal: number
Focus
Focus

Description:

The total number of Frames in this Texture, including the __BASE frame.

A Texture will always contain at least 1 frame because every Texture contains a __BASE frame by default, in addition to any extra frames that have been added to it, such as when parsing a Sprite Sheet or Texture Atlas.

Type:
number
Default: 0
Inherited from: Phaser.Textures.Texture#frameTotal
Since: 3.0.0
Source: src/textures/Texture.js (Line 116)
Focus
Focus
frames: object
Focus
Focus

Description:

A key-value object pair associating the unique Frame keys with the Frames objects.

Type:
object
Inherited from: Phaser.Textures.Texture#frames
Since: 3.0.0
Source: src/textures/Texture.js (Line 88)
Focus
Focus
<readonly> height: number
Focus
Focus

Description:

The height of the Canvas. This property is read-only, if you wish to change it use the setSize method.

Type:
number
Since: 3.7.0
Focus
Focus
imageData: ImageData
Focus
Focus

Description:

The context image data. Use the update method to populate this when the canvas changes.

Type:
ImageData
Since: 3.13.0
Focus
Focus
key: string
Focus
Focus

Description:

The unique string-based key of this Texture.

Type:
string
Inherited from: Phaser.Textures.Texture#key
Since: 3.0.0
Source: src/textures/Texture.js (Line 59)
Focus
Focus

Description:

A reference to the Texture Manager this Texture belongs to.

Type:
Inherited from: Phaser.Textures.Texture#manager
Since: 3.0.0
Source: src/textures/Texture.js (Line 50)
Focus
Focus
pixels: Uint32Array
Focus
Focus

Description:

An Uint32Array view into the buffer.

Type:
Uint32Array
Since: 3.13.0
Focus
Focus

Description:

An array of TextureSource instances. These are unique to this Texture and contain the actual Image (or Canvas) data.

Type:
Inherited from: Phaser.Textures.Texture#source
Since: 3.0.0
Source: src/textures/Texture.js (Line 68)
Focus
Focus
<readonly> width: number
Focus
Focus

Description:

The width of the Canvas. This property is read-only, if you wish to change it use the setSize method.

Type:
number
Since: 3.7.0
Focus
Focus

Methods

add(name, sourceIndex, x, y, width, height)
Focus
Focus

Description:

Adds a new Frame to this Texture.

A Frame is a rectangular region of a TextureSource with a unique index or string-based key.

The name given must be unique within this Texture. If it already exists, this method will return null.

Parameters:

name type description
name number | string

The name of this Frame. The name is unique within the Texture.

sourceIndex number

The index of the TextureSource that this Frame is a part of.

x number

The x coordinate of the top-left of this Frame.

y number

The y coordinate of the top-left of this Frame.

width number

The width of this Frame.

height number

The height of this Frame.

Returns:
Description:

The Frame that was added to this Texture, or null if the given name already exists.

Inherited from: Phaser.Textures.Texture#add
Since: 3.0.0
Source: src/textures/Texture.js (Line 136)
Focus
Focus
clear([x], [y], [width], [height], [update])
Focus
Focus

Description:

Clears the given region of this Canvas Texture, resetting it back to transparent. If no region is given, the whole Canvas Texture is cleared.

Parameters:

name type arguments Default description
x number <optional> 0

The x coordinate of the top-left of the region to clear.

y number <optional> 0

The y coordinate of the top-left of the region to clear.

width number <optional>

The width of the region.

height number <optional>

The height of the region.

update boolean <optional> true

Update the internal ImageData buffer and arrays.

Returns:
Description:

The Canvas Texture.

Since: 3.7.0
Focus
Focus
destroy()
Focus
Focus

Description:

Destroys this Texture and releases references to its sources and frames.

Overrides: Phaser.Textures.Texture#destroy
Since: 3.16.0
Focus
Focus
draw(x, y, source, [update])
Focus
Focus

Description:

Draws the given Image or Canvas element to this CanvasTexture, then updates the internal ImageData buffer and arrays.

Parameters:

name type arguments Default description
x number

The x coordinate to draw the source at.

y number

The y coordinate to draw the source at.

source HTMLImageElement | HTMLCanvasElement

The element to draw to this canvas.

update boolean <optional> true

Update the internal ImageData buffer and arrays.

Returns:
Description:

This CanvasTexture.

Since: 3.13.0
Focus
Focus
drawFrame(key, [frame], [x], [y], [update])
Focus
Focus

Description:

Draws the given texture frame to this CanvasTexture, then updates the internal ImageData buffer and arrays.

Parameters:

name type arguments Default description
key string

The unique string-based key of the Texture.

frame string | number <optional>

The string-based name, or integer based index, of the Frame to get from the Texture.

x number <optional> 0

The x coordinate to draw the source at.

y number <optional> 0

The y coordinate to draw the source at.

update boolean <optional> true

Update the internal ImageData buffer and arrays.

Returns:
Description:

This CanvasTexture.

Since: 3.16.0
Focus
Focus
get([name])
Focus
Focus

Description:

Gets a Frame from this Texture based on either the key or the index of the Frame.

In a Texture Atlas Frames are typically referenced by a key. In a Sprite Sheet Frames are referenced by an index. Passing no value for the name returns the base texture.

Parameters:

name type arguments description
name string | number <optional>

The string-based name, or integer based index, of the Frame to get from this Texture.

Returns:
Description:

The Texture Frame.

Inherited from: Phaser.Textures.Texture#get
Since: 3.0.0
Source: src/textures/Texture.js (Line 224)
Focus
Focus
getCanvas()
Focus
Focus

Description:

Gets the Canvas Element.

Returns:
Description:

The Canvas DOM element this texture is using.

Type:
  • HTMLCanvasElement
Since: 3.7.0
Focus
Focus
getContext()
Focus
Focus

Description:

Gets the 2D Canvas Rendering Context.

Returns:
Description:

The Canvas Rendering Context this texture is using.

Type:
  • CanvasRenderingContext2D
Since: 3.7.0
Focus
Focus
getData(x, y, width, height)
Focus
Focus

Description:

Gets an ImageData region from this CanvasTexture from the position and size specified. You can write this back using CanvasTexture.putData, or manipulate it.

Parameters:

name type description
x number

The x coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.

y number

The y coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.

width number

The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.

height number

The height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up.

Returns:
Description:

The ImageData extracted from this CanvasTexture.

Type:
  • ImageData
Since: 3.16.0
Focus
Focus
getDataSourceImage([name])
Focus
Focus

Description:

Given a Frame name, return the data source image it uses to render with. You can use this to get the normal map for an image for example.

This will return the actual DOM Image.

Parameters:

name type arguments description
name string | number <optional>

The string-based name, or integer based index, of the Frame to get from this Texture.

Returns:
Description:

The DOM Image or Canvas Element.

Type:
  • HTMLImageElement
  • HTMLCanvasElement
Inherited from: Phaser.Textures.Texture#getDataSourceImage
Since: 3.7.0
Source: src/textures/Texture.js (Line 439)
Focus
Focus
getFrameBounds([sourceIndex])
Focus
Focus

Description:

Based on the given Texture Source Index, this method will get all of the Frames using that source and then work out the bounds that they encompass, returning them in an object.

This is useful if this Texture is, for example, a sprite sheet within an Atlas, and you need to know the total bounds of the sprite sheet.

Parameters:

name type arguments description
sourceIndex number <optional>

The index of the TextureSource to get the Frame bounds from.

Returns:
Description:

An object containing the bounds of the Frames using the given Texture Source Index.

Inherited from: Phaser.Textures.Texture#getFrameBounds
Since: 3.80.0
Source: src/textures/Texture.js (Line 319)
Focus
Focus
getFrameNames([includeBase])
Focus
Focus

Description:

Returns an array with all of the names of the Frames in this Texture.

Useful if you want to randomly assign a Frame to a Game Object, as you can pick a random element from the returned array.

Parameters:

name type arguments Default description
includeBase boolean <optional> false

Include the __BASE Frame in the output array?

Returns:
Description:

An array of all Frame names in this Texture.

Type:
  • Array.<string>
Inherited from: Phaser.Textures.Texture#getFrameNames
Since: 3.0.0
Source: src/textures/Texture.js (Line 374)
Focus
Focus
getFramesFromTextureSource(sourceIndex, [includeBase])
Focus
Focus

Description:

Returns an array of all the Frames in the given TextureSource.

Parameters:

name type arguments Default description
sourceIndex number

The index of the TextureSource to get the Frames from.

includeBase boolean <optional> false

Include the __BASE Frame in the output array?

Returns:
Description:

An array of Texture Frames.

Type:
Inherited from: Phaser.Textures.Texture#getFramesFromTextureSource
Since: 3.0.0
Source: src/textures/Texture.js (Line 284)
Focus
Focus
getIndex(x, y)
Focus
Focus

Description:

Returns the Image Data index for the given pixel in this CanvasTexture.

The index can be used to read directly from the this.data array.

The index points to the red value in the array. The subsequent 3 indexes point to green, blue and alpha respectively.

Parameters:

name type description
x number

The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

y number

The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

Type:
  • number
Since: 3.16.0
Focus
Focus
getPixel(x, y, [out])
Focus
Focus

Description:

Get the color of a specific pixel from this texture and store it in a Color object.

If you have drawn anything to this CanvasTexture since it was created you must call CanvasTexture.update to refresh the array buffer, otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.

Parameters:

name type arguments description
x number

The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

y number

The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

out Phaser.Display.Color <optional>

A Color object to store the pixel values in. If not provided a new Color object will be created.

Returns:
Description:

An object with the red, green, blue and alpha values set in the r, g, b and a properties.

Since: 3.13.0
Focus
Focus
getPixels([x], [y], [width], [height])
Focus
Focus

Description:

Returns an array containing all of the pixels in the given region.

If the requested region extends outside the bounds of this CanvasTexture, the region is truncated to fit.

If you have drawn anything to this CanvasTexture since it was created you must call CanvasTexture.update to refresh the array buffer, otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.

Parameters:

name type arguments description
x number <optional>

The x coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.

y number <optional>

The y coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.

width number <optional>

The width of the region to get. Must be an integer. Defaults to the canvas width if not given.

height number <optional>

The height of the region to get. Must be an integer. If not given will be set to the width.

Returns:
Description:

A 2d array of Pixel objects.

Type:
Since: 3.16.0
Focus
Focus
getSourceImage([name])
Focus
Focus

Description:

Given a Frame name, return the source image it uses to render with.

This will return the actual DOM Image or Canvas element.

Parameters:

name type arguments description
name string | number <optional>

The string-based name, or integer based index, of the Frame to get from this Texture.

Returns:
Description:

The DOM Image, Canvas Element or Render Texture.

Type:
Inherited from: Phaser.Textures.Texture#getSourceImage
Since: 3.0.0
Source: src/textures/Texture.js (Line 406)
Focus
Focus
getTextureSourceIndex(source)
Focus
Focus

Description:

Takes the given TextureSource and returns the index of it within this Texture. If it's not in this Texture, it returns -1. Unless this Texture has multiple TextureSources, such as with a multi-atlas, this method will always return zero or -1.

Parameters:

name type description
source Phaser.Textures.TextureSource

The TextureSource to check.

Returns:
Description:

The index of the TextureSource within this Texture, or -1 if not in this Texture.

Type:
  • number
Inherited from: Phaser.Textures.Texture#getTextureSourceIndex
Since: 3.0.0
Source: src/textures/Texture.js (Line 258)
Focus
Focus
has(name)
Focus
Focus

Description:

Checks to see if a Frame matching the given key exists within this Texture.

Parameters:

name type description
name string

The key of the Frame to check for.

Returns:
Description:

True if a Frame with the matching key exists in this Texture.

Type:
  • boolean
Inherited from: Phaser.Textures.Texture#has
Since: 3.0.0
Source: src/textures/Texture.js (Line 209)
Focus
Focus
putData(imageData, x, y, [dirtyX], [dirtyY], [dirtyWidth], [dirtyHeight])
Focus
Focus

Description:

Puts the ImageData into the context of this CanvasTexture at the given coordinates.

Parameters:

name type arguments description
imageData ImageData

The ImageData to put at the given location.

x number

The x coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.

y number

The y coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.

dirtyX number <optional>

Horizontal position (x coordinate) of the top-left corner from which the image data will be extracted.

dirtyY number <optional>

Vertical position (x coordinate) of the top-left corner from which the image data will be extracted.

dirtyWidth number <optional>

Width of the rectangle to be painted. Defaults to the width of the image data.

dirtyHeight number <optional>

Height of the rectangle to be painted. Defaults to the height of the image data.

Returns:
Description:

This CanvasTexture.

Since: 3.16.0
Focus
Focus
refresh()
Focus
Focus

Description:

This should be called manually if you are running under WebGL. It will refresh the WebGLTexture from the Canvas source. Only call this if you know that the canvas has changed, as there is a significant GPU texture allocation cost involved in doing so.

Returns:
Description:

This CanvasTexture.

Since: 3.7.0
Focus
Focus
remove(name)
Focus
Focus

Description:

Removes the given Frame from this Texture. The Frame is destroyed immediately.

Any Game Objects using this Frame should stop using it before you remove it, as it does not happen automatically.

Parameters:

name type description
name string

The key of the Frame to remove.

Returns:
Description:

True if a Frame with the matching key was removed from this Texture.

Type:
  • boolean
Inherited from: Phaser.Textures.Texture#remove
Since: 3.19.0
Source: src/textures/Texture.js (Line 180)
Focus
Focus
setDataSource(data)
Focus
Focus

Description:

Adds a data source image to this Texture.

An example of a data source image would be a normal map, where all of the Frames for this Texture equally apply to the normal map.

Parameters:

name type description
data HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement>

The source image.

Inherited from: Phaser.Textures.Texture#setDataSource
Since: 3.0.0
Source: src/textures/Texture.js (Line 476)
Focus
Focus
setFilter(filterMode)
Focus
Focus

Description:

Sets the Filter Mode for this Texture.

The mode can be either Linear, the default, or Nearest.

For pixel-art you should use Nearest.

The mode applies to the entire Texture, not just a specific Frame of it.

Parameters:

name type description
filterMode Phaser.Textures.FilterMode

The Filter Mode.

Inherited from: Phaser.Textures.Texture#setFilter
Since: 3.0.0
Source: src/textures/Texture.js (Line 502)
Focus
Focus
setPixel(x, y, red, green, blue, [alpha])
Focus
Focus

Description:

Sets a pixel in the CanvasTexture to the given color and alpha values.

This is an expensive operation to run in large quantities, so use sparingly.

Parameters:

name type arguments Default description
x number

The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

y number

The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.

red number

The red color value. A number between 0 and 255.

green number

The green color value. A number between 0 and 255.

blue number

The blue color value. A number between 0 and 255.

alpha number <optional> 255

The alpha value. A number between 0 and 255.

Returns:
Description:

This CanvasTexture.

Since: 3.16.0
Focus
Focus
setSize(width, [height])
Focus
Focus

Description:

Changes the size of this Canvas Texture.

Parameters:

name type arguments description
width number

The new width of the Canvas.

height number <optional>

The new height of the Canvas. If not given it will use the width as the height.

Returns:
Description:

The Canvas Texture.

Since: 3.7.0
Focus
Focus
update()
Focus
Focus

Description:

This re-creates the imageData from the current context. It then re-builds the ArrayBuffer, the data Uint8ClampedArray reference and the pixels Int32Array.

Warning: This is a very expensive operation, so use it sparingly.

Returns:
Description:

This CanvasTexture.

Since: 3.13.0
Focus
Focus