Skip to main content
Version: Phaser v4.0.0-rc.6

DynamicTexture

A Dynamic Texture is a special texture that allows you to draw textures, frames and most kind of

Game Objects directly to it.

You can take many complex objects and draw them to this one texture, which can then be used as the

base texture for other Game Objects, such as Sprites. Should you then update this texture, all

Game Objects using it will instantly be updated as well, reflecting the changes immediately.

It's a powerful way to generate dynamic textures at run-time that are WebGL friendly and don't invoke

expensive GPU uploads on each change.


const t = this.textures.addDynamicTexture('player', 64, 128);

// draw objects to t

this.add.sprite(x, y, 'player');

this.render();

Because this is a standard Texture within Phaser, you can add frames to it, meaning you can use it

to generate sprite sheets, texture atlases or tile sets.

Under WebGL1, a FrameBuffer, which is what this Dynamic Texture uses internally, cannot be anti-aliased.

This means that when drawing objects such as Shapes or Graphics instances to this texture, they may appear

to be drawn with no aliasing around the edges. This is a technical limitation of WebGL1. To get around it,

create your shape as a texture in an art package, then draw that to this texture.

In the event that the WebGL context is lost, this DynamicTexture will

lose its contents. Once context is restored (signalled by the restorewebgl

event), you can choose to redraw the contents of the DynamicTexture.

You are responsible for the redrawing logic.

Constructor

new DynamicTexture(manager, key, [width], [height], [forceEven])

Parameters

nametypeoptionaldefaultdescription
managerPhaser.Textures.TextureManagerNoA reference to the Texture Manager this Texture belongs to.
keystringNoThe unique string-based key of this Texture.
widthnumberYes256The width of this Dymamic Texture in pixels. Defaults to 256 x 256.
heightnumberYes256The height of this Dymamic Texture in pixels. Defaults to 256 x 256.
forceEvenbooleanYestrueForce the given width and height to be rounded to even values. This significantly improves the rendering quality. Set to false if you know you need an odd sized texture.

Scope: static

Extends

Phaser.Textures.Texture

Source: src/textures/DynamicTexture.js#L20
Since: 3.60.0

Inherited Members

From Phaser.Textures.Texture:


Public Members

camera

camera: Phaser.Cameras.Scene2D.Camera

Description:

An internal Camera that can be used to move around this Dynamic Texture.

Control it just like you would any Scene Camera. The difference is that it only impacts

the placement of Game Objects (not textures) that you then draw to this texture.

You can scroll, zoom and rotate this Camera.

Source: src/textures/DynamicTexture.js#L157
Since: 3.12.0


canvas

canvas: HTMLCanvasElement

Description:

A reference to the Rendering Context belonging to the Canvas Element this Dynamic Texture is drawing to.

Source: src/textures/DynamicTexture.js#L138
Since: 3.2.0


commandBuffer

commandBuffer: array

Description:

An array of commands that are used to draw to this Dynamic Texture.

This is flushed by the render method.

The clear method will also clear this array, then store itself.

Source: src/textures/DynamicTexture.js#L127
Since: 4.0.0


context

context: CanvasRenderingContext2D

Description:

The 2D Canvas Rendering Context.

Source: src/textures/DynamicTexture.js#L147
Since: 3.7.0


drawingContext

drawingContext: Phaser.Renderer.WebGL.DrawingContext

Description:

The drawing context of this Dynamic Texture.

This contains the framebuffer that the Dynamic Texture is drawing to.

Source: src/textures/DynamicTexture.js#L171
Since: 4.0.0


height

height: number

Description:

The height of this Dynamic Texture.

Treat this property as read-only. Use the setSize method to change the size.

Source: src/textures/DynamicTexture.js#L116
Since: 3.60.0


renderer

renderer: Phaser.Renderer.Canvas.CanvasRenderer, Phaser.Renderer.WebGL.WebGLRenderer

Description:

A reference to either the Canvas or WebGL Renderer that the Game instance is using.

Source: src/textures/DynamicTexture.js#L96
Since: 3.2.0


type

type: string

Description:

The internal data type of this object.

Source: src/textures/DynamicTexture.js#L76
Since: 3.60.0


width

width: number

Description:

The width of this Dynamic Texture.

Treat this property as read-only. Use the setSize method to change the size.

Source: src/textures/DynamicTexture.js#L105
Since: 3.60.0


Inherited Methods

From Phaser.Textures.Texture:


Public Methods

callback

<instance> callback(callback)

Description:

Adds a callback to run during the render process.

This callback runs as a step in the command buffer.

It can be used to set up conditions for the next draw step.

Note that this will only execute after render() is called.

Parameters:

nametypeoptionaldescription
callbackfunctionNoA callback function to run during the render process.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1116
Since: 4.0.0


capture

<instance> capture(entry, config)

Description:

Draws the given object to this Dynamic Texture.

This allows you to draw the object as it appears in the game world,

or with various parameter overrides in the config.

Parameters:

nametypeoptionaldescription
entryPhaser.GameObjects.GameObjectNoAny renderable GameObject.
configPhaser.Types.Textures.CaptureConfigNoThe configuration object for the capture.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L843
Since: 4.0.0


clear

<instance> clear([x], [y], [width], [height])

Description:

Clears a portion or everything from this Dynamic Texture by erasing it and resetting it back to

a blank, transparent, texture. To clear an area, specify the x, y, width and height.

Parameters:

nametypeoptionaldefaultdescription
xnumberYes0The left coordinate of the fill rectangle.
ynumberYes0The top coordinate of the fill rectangle.
widthnumberYes"this.width"The width of the fill rectangle.
heightnumberYes"this.height"The height of the fill rectangle.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L612
Since: 3.2.0


destroy

<instance> destroy()

Description:

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

Overrides: Phaser.Textures.Texture#destroy

Source: src/textures/DynamicTexture.js#L1285
Since: 3.60.0


draw

<instance> draw(entries, [x], [y], [alpha], [tint])

Description:

Draws the given object, or an array of objects, to this Dynamic Texture.

It can accept any of the following:

  • Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.

  • Tilemap Layers.

  • A Group. The contents of which will be iterated and drawn in turn.

  • A Container. The contents of which will be iterated fully, and drawn in turn.

  • A Scene Display List. Pass in Scene.children to draw the whole list.

  • Another Dynamic Texture, or a Render Texture.

  • A Texture Frame instance.

  • A string. This is used to look-up the texture from the Texture Manager.

Note 1: You cannot draw a Dynamic Texture to itself.

Note 2: GameObjects will use the camera, while textures and frames will not.

Textures and frames are drawn using the stamp method.

If passing in a Group or Container it will only draw children that return true

when their willRender() method is called. I.e. a Container with 10 children,

5 of which have visible=false will only draw the 5 visible ones.

If passing in an array of Game Objects it will draw them all, regardless if

they pass a willRender check or not.

You can pass in a string in which case it will look for a texture in the Texture

Manager matching that string, and draw the base frame. If you need to specify

exactly which frame to draw then use the method drawFrame instead.

You can pass in the x and y coordinates to draw the objects at. The use of

the coordinates differ based on what objects are being drawn. If the object is

a Group, Container or Display List, the coordinates are added to the positions

of the children. For all other types of object, the coordinates are exact.

For textures and frames, the x and y values are the middle of the texture.

The alpha and tint values are only used by Texture Frames.

Game Objects use their own alpha and tint values when being drawn.

Parameters:

nametypeoptionaldefaultdescription
entriesanyNoAny renderable Game Object, or Group, Container, Display List, other Render Texture, Texture Frame or an array of any of these.
xnumberYes0The x position to draw the Frame at, or the offset applied to the object.
ynumberYes0The y position to draw the Frame at, or the offset applied to the object.
alphanumberYes1The alpha value. Only used when drawing Texture Frames to this texture. Game Objects use their own alpha.
tintnumberYes"0xffffff"The tint color value. Only used when drawing Texture Frames to this texture. Game Objects use their own tint. WebGL only.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L736
Since: 3.2.0


erase

<instance> erase(entries, [x], [y], [alpha], [tint])

Description:

Draws the given object, or an array of objects, to this Dynamic Texture using a blend mode of ERASE.

This has the effect of erasing any filled pixels present in the objects from this texture.

This method uses the draw method internally,

and the parameters behave the same way.

Parameters:

nametypeoptionaldefaultdescription
entriesanyNoAny renderable Game Object, or Group, Container, Display List, Render Texture, Texture Frame, or an array of any of these.
xnumberYes0The x position to draw the Frame at, or the offset applied to the object.
ynumberYes0The y position to draw the Frame at, or the offset applied to the object.
alphanumberYes1The alpha value. Only used when drawing Texture Frames to this texture. Game Objects use their own alpha.
tintnumberYes"0xffffff"The tint color value. Only used when drawing Texture Frames to this texture. Game Objects use their own tint. WebGL only.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L693
Since: 3.16.0


fill

<instance> fill(rgb, [alpha], [x], [y], [width], [height])

Description:

Fills this Dynamic Texture with the given color.

By default it will fill the entire texture, however you can set it to fill a specific

rectangular area by using the x, y, width and height arguments.

The color should be given in hex format, i.e. 0xff0000 for red, 0x00ff00 for green, etc.

Parameters:

nametypeoptionaldefaultdescription
rgbnumberNoThe color to fill this Dynamic Texture with, such as 0xff0000 for red.
alphanumberYes1The alpha value used by the fill.
xnumberYes0The left coordinate of the fill rectangle.
ynumberYes0The top coordinate of the fill rectangle.
widthnumberYes"this.width"The width of the fill rectangle.
heightnumberYes"this.height"The height of the fill rectangle.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L569
Since: 3.2.0


finishCapture

<instance> finishCapture(entry, cacheConfig)

Description:

Restores the object properties that were overridden during the capture method.

This method is called automatically during rendering.

Do not call it directly.

Parameters:

nametypeoptionaldescription
entryPhaser.GameObjects.GameObjectNoThe GameObject to restore the properties on.
cacheConfigPhaser.Types.Textures.CaptureConfigNoThe cached properties to restore.

Source: src/textures/DynamicTexture.js#L970
Since: 4.0.0


getWebGLTexture

<instance> getWebGLTexture()

Description:

Returns the underlying WebGLTextureWrapper, if not running in Canvas mode.

Returns: Phaser.Renderer.WebGL.Wrappers.WebGLTextureWrapper - The underlying WebGLTextureWrapper, if not running in Canvas mode.

Source: src/textures/DynamicTexture.js#L1232
Since: 3.60.0


preserve

<instance> preserve(preserve)

Description:

Sets the preserve flag for this Dynamic Texture.

Ordinarily, after each render, the command buffer is cleared.

When this flag is set to true, the command buffer is preserved between renders.

This makes it possible to repeat the same drawing commands on each render.

Make sure to call clear() at the start if you don't want to accumulate

drawing detail over the top of itself.

Parameters:

nametypeoptionaldescription
preservebooleanNoWhether to preserve the command buffer after rendering.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1095
Since: 4.0.0


render

<instance> render()

Description:

Render the buffered drawing commands to this Dynamic Texture.

You must do this in order to see anything drawn to it.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L284
Since: 4.0.0


renderCanvas

<instance> renderCanvas(renderer, mask, camera)

Description:

This is a NOOP method. Bitmap Masks are not supported by the Canvas Renderer.

Parameters:

nametypeoptionaldescription
rendererPhaser.Renderer.Canvas.CanvasRenderer | Phaser.Renderer.WebGL.WebGLRendererNoThe Canvas Renderer which would be rendered to.
maskPhaser.GameObjects.GameObjectNoThe masked Game Object which would be rendered.
cameraPhaser.Cameras.Scene2D.CameraNoThe Camera to render to.

Source: src/textures/DynamicTexture.js#L1270
Since: 3.60.0


renderWebGL

<instance> renderWebGL(renderer, src, camera, parentMatrix)

Description:

Sets this Dynamic Texture onto the TextureManager.Stamp

and then calls its render method.

Parameters:

nametypeoptionaldescription
rendererPhaser.Renderer.WebGL.WebGLRendererNoA reference to the current active WebGL renderer.
srcPhaser.GameObjects.ImageNoThe Game Object being rendered in this call.
cameraPhaser.Cameras.Scene2D.CameraNoThe Camera that is rendering the Game Object.
parentMatrixPhaser.GameObjects.Components.TransformMatrixNoThis transform matrix is defined if the game object is nested

Source: src/textures/DynamicTexture.js#L1248
Since: 3.60.0


repeat

<instance> repeat(key, [frame], [x], [y], [width], [height], [config])

Description:

Takes the given Texture Frame and draws it to this Dynamic Texture as a fill pattern,

i.e. in a grid-layout based on the frame dimensions.

It uses a TileSprite internally to draw the frame repeatedly.

Textures are referenced by their string-based keys, as stored in the Texture Manager.

You can optionally provide a position, width, height, alpha and tint value to apply to

the frames before they are drawn. The position controls the top-left where the repeating

fill will start from. The width and height control the size of the filled area.

The position can be negative if required, but the dimensions cannot.

This method respects the camera settings of the Dynamic Texture.

Parameters:

nametypeoptionaldefaultdescription
keystringNoThe key of the texture to be used, as stored in the Texture Manager.
framestring | numberYesThe name or index of the frame within the Texture. Set to null to skip this argument if not required.
xnumberYes0The x position to start drawing the frames from (can be negative to offset).
ynumberYes0The y position to start drawing the frames from (can be negative to offset).
widthnumberYes"this.width"The width of the area to repeat the frame within. Defaults to the width of this Dynamic Texture.
heightnumberYes"this.height"The height of the area to repeat the frame within. Defaults to the height of this Dynamic Texture.
configPhaser.Types.GameObjects.TileSprite.TileSpriteConfigYesThe configuration object for the TileSprite which repeats the texture, allowing you to set further properties on it.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1026
Since: 3.60.0


setSize

<instance> setSize(width, [height], [forceEven])

Description:

Resizes this Dynamic Texture to the new dimensions given.

In WebGL it will destroy and then re-create the frame buffer being used by this Dynamic Texture.

In Canvas it will resize the underlying canvas DOM element.

Both approaches will erase everything currently drawn to this texture.

If the dimensions given are the same as those already being used, calling this method will do nothing.

Parameters:

nametypeoptionaldefaultdescription
widthnumberNoThe new width of this Dynamic Texture.
heightnumberYes"width"The new height of this Dynamic Texture. If not specified, will be set the same as the width.
forceEvenbooleanYestrueForce the given width and height to be rounded to even values. This significantly improves the rendering quality. Set to false if you know you need an odd sized texture.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture.

Source: src/textures/DynamicTexture.js#L195
Since: 3.10.0


snapshot

<instance> snapshot(callback, [type], [encoderOptions])

Description:

Takes a snapshot of the whole of this Dynamic Texture.

The snapshot is taken immediately, but the results are returned via the given callback.

To capture a portion of this Dynamic Texture see the snapshotArea method.

To capture just a specific pixel, see the snapshotPixel method.

Snapshots work by using the WebGL readPixels feature to grab every pixel from the frame buffer

into an ArrayBufferView. It then parses this, copying the contents to a temporary Canvas and finally

creating an Image object from it, which is the image returned to the callback provided.

All in all, this is a computationally expensive and blocking process, which gets more expensive

the larger the resolution this Dynamic Texture has, so please be careful how you employ this in your game.

Parameters:

nametypeoptionaldefaultdescription
callbackPhaser.Types.Renderer.Snapshot.SnapshotCallbackNoThe Function to invoke after the snapshot image is created.
typestringYes"'image/png'"The format of the image to create, usually image/png or image/jpeg.
encoderOptionsnumberYes0.92The image quality, between 0 and 1. Used for image formats with lossy compression, such as image/jpeg.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1177
Since: 3.19.0


snapshotArea

<instance> snapshotArea(x, y, width, height, callback, [type], [encoderOptions])

Description:

Takes a snapshot of the given area of this Dynamic Texture.

The snapshot is taken immediately, but the results are returned via the given callback.

To capture the whole Dynamic Texture see the snapshot method.

To capture just a specific pixel, see the snapshotPixel method.

Snapshots work by using the WebGL readPixels feature to grab every pixel from the frame buffer

into an ArrayBufferView. It then parses this, copying the contents to a temporary Canvas and finally

creating an Image object from it, which is the image returned to the callback provided.

All in all, this is a computationally expensive and blocking process, which gets more expensive

the larger the resolution this Dynamic Texture has, so please be careful how you employ this in your game.

Parameters:

nametypeoptionaldefaultdescription
xnumberNoThe x coordinate to grab from.
ynumberNoThe y coordinate to grab from.
widthnumberNoThe width of the area to grab.
heightnumberNoThe height of the area to grab.
callbackPhaser.Types.Renderer.Snapshot.SnapshotCallbackNoThe Function to invoke after the snapshot image is created.
typestringYes"'image/png'"The format of the image to create, usually image/png or image/jpeg.
encoderOptionsnumberYes0.92The image quality, between 0 and 1. Used for image formats with lossy compression, such as image/jpeg.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1135
Since: 3.19.0


snapshotPixel

<instance> snapshotPixel(x, y, callback)

Description:

Takes a snapshot of the given pixel from this Dynamic Texture.

The snapshot is taken immediately, but the results are returned via the given callback.

To capture the whole Dynamic Texture see the snapshot method.

To capture a portion of this Dynamic Texture see the snapshotArea method.

Unlike the two other snapshot methods, this one will send your callback a Color object

containing the color data for the requested pixel. It doesn't need to create an internal

Canvas or Image object, so is a lot faster to execute, using less memory than the other snapshot methods.

Parameters:

nametypeoptionaldescription
xnumberNoThe x coordinate of the pixel to get.
ynumberNoThe y coordinate of the pixel to get.
callbackPhaser.Types.Renderer.Snapshot.SnapshotCallbackNoThe Function to invoke after the snapshot pixel data is extracted.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L1206
Since: 3.19.0


stamp

<instance> stamp(key, [frame], [x], [y], [config])

Description:

Takes the given texture key and frame and then stamps it at the given

x and y coordinates. You can use the optional 'config' argument to provide

lots more options about how the stamp is applied, including the alpha,

tint, angle, scale and origin.

By default, the frame will stamp on the x/y coordinates based on its center.

If you wish to stamp from the top-left, set the config originX and

originY properties both to zero.

This method ignores the camera property of the Dynamic Texture.

Parameters:

nametypeoptionaldefaultdescription
keystringNoThe key of the texture to be used, as stored in the Texture Manager.
framestring | numberYesThe name or index of the frame within the Texture. Set to null to skip this argument if not required.
xnumberYes0The x position to draw the frame at.
ynumberYes0The y position to draw the frame at.
configPhaser.Types.Textures.StampConfigYesThe stamp configuration object, allowing you to set the alpha, tint, angle, scale and origin of the stamp.

Returns: Phaser.Textures.DynamicTexture - This Dynamic Texture instance.

Source: src/textures/DynamicTexture.js#L638
Since: 3.60.0


startCapture

<instance> startCapture(entry, config)

Description:

Prepares an object to be rendered using the capture method.

This method is called automatically during rendering.

Do not call it directly.

Parameters:

nametypeoptionaldescription
entryPhaser.GameObjects.GameObjectNoThe object to set up for capture.
configPhaser.Types.Textures.CaptureConfigNoThe configuration object for the capture.

Returns: Phaser.Types.Textures.CaptureConfig - A configuration object containing the appropriate parent transform in transform, and the cached object properties in any fields that were overridden.

Source: src/textures/DynamicTexture.js#L865
Since: 4.0.0