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

RenderTexture

A Render Texture is a combination of Dynamic Texture and an Image Game Object, that uses the

Dynamic Texture to display itself with.

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.

In versions of Phaser before 3.60 a Render Texture was the only way you could create a texture

like this, that had the ability to be drawn on. But in 3.60 we split the core functions out to

the Dynamic Texture class as it made a lot more sense for them to reside in there. As a result,

the Render Texture is now a light-weight shim that sits on-top of an Image Game Object and offers

proxy methods to the features available from a Dynamic Texture.

When should you use a Render Texture vs. a Dynamic Texture?

You should use a Dynamic Texture if the texture is going to be used by multiple Game Objects,

or you want to use it across multiple Scenes, because textures are globally stored.

You should use a Dynamic Texture if the texture isn't going to be displayed in-game, but is

instead going to be used for something like a mask or shader.

You should use a Render Texture if you need to display the texture in-game on a single Game Object,

as it provides the convenience of wrapping an Image and Dynamic Texture together for you.

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.

Constructor

new RenderTexture(scene, [x], [y], [width], [height], [forceEven])

Parameters

nametypeoptionaldefaultdescription
scenePhaser.SceneNoThe Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
xnumberYes0The horizontal position of this Game Object in the world.
ynumberYes0The vertical position of this Game Object in the world.
widthnumberYes32The width of the Render Texture.
heightnumberYes32The height of the Render Texture.
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.GameObjects.Image

Source: src/gameobjects/rendertexture/RenderTexture.js#L13
Since: 3.2.0

Inherited Members

From Phaser.GameObjects.Components.Alpha:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Size:

From Phaser.GameObjects.Components.TextureCrop:

From Phaser.GameObjects.Components.Tint:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:


Public Members

camera

camera: Phaser.Cameras.Scene2D.BaseCamera

Description:

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

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

the placement of Game Objects that you then draw to this texture.

You can scroll, zoom and rotate this Camera.

This property is a reference to RenderTexture.texture.camera.

Source: src/gameobjects/rendertexture/RenderTexture.js#L87
Since: 3.12.0


isCurrentlyRendering

isCurrentlyRendering: boolean

Description:

Whether this RenderTexture is currently executing renderWebGL.

This is used to prevent infinite loops when drawing containers.

You should not set this property directly.

Source: src/gameobjects/rendertexture/RenderTexture.js#L130
Since: 4.0.0


renderMode

renderMode: 'render', 'redraw', 'all'

Description:

The render mode of this Render Texture.

Set this property to change how the Render Texture is rendered.

  • 'render' mode draws the contents of the Render Texture to each frame.

  • 'redraw' mode calls render() and redraws the texture every frame,

    but does not render itself. This is useful for updating textures

    for reuse by other objects.

  • 'all' mode calls render() then draws the texture to the frame.

Source: src/gameobjects/rendertexture/RenderTexture.js#L113
Since: 4.0.0


Inherited Methods

From Phaser.Events.EventEmitter:

From Phaser.GameObjects.Components.Alpha:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.GetBounds:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.RenderSteps:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Size:

From Phaser.GameObjects.Components.TextureCrop:

From Phaser.GameObjects.Components.Tint:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:


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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L538
Since: 4.0.0


capture

<instance> capture(entry, config)

Description:

Draws the given object to this Render 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.GameObjects.RenderTexture - This Dynamic Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L462
Since: 4.0.0


clear

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

Description:

Clears a portion or everything from this Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L330
Since: 3.2.0


draw

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

Description:

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

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 Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L405
Since: 3.2.0


erase

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

Description:

Draws the given object, or an array of objects, to this Render 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.

Returns: Phaser.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L382
Since: 3.16.0


fill

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

Description:

Fills this Render 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 Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L303
Since: 3.2.0


preDestroy

<instance> preDestroy()

Description:

Internal destroy handler, called as part of the destroy process.

Access: protected

Source: src/gameobjects/rendertexture/RenderTexture.js#L647
Since: 3.9.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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L517
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.

Source: src/gameobjects/rendertexture/RenderTexture.js#L289
Since: 4.0.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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L482
Since: 3.60.0


resize

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

Description:

Resizes the Render Texture to the new dimensions given.

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

In Canvas it will resize the underlying canvas element.

Both approaches will erase everything currently drawn to the Render Texture.

Calling this will then invoke the setSize method, setting the internal size of this Game Object

to the values given to this method.

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

Parameters:

nametypeoptionaldefaultdescription
widthnumberNoThe new width of the Render Texture.
heightnumberYes"width"The new height of the Render 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.GameObjects.RenderTexture - This Render Texture.

Source: src/gameobjects/rendertexture/RenderTexture.js#L181
Since: 3.10.0


saveTexture

<instance> saveTexture(key)

Description:

Stores a copy of this Render Texture in the Texture Manager using the given key.

After doing this, any texture based Game Object, such as a Sprite, can use the contents of this

Render Texture by using the texture key:


var rt = this.add.renderTexture(0, 0, 128, 128);



// Draw something to the Render Texture



rt.saveTexture('doodle');



this.add.image(400, 300, 'doodle');

Updating the contents of this Render Texture will automatically update any Game Object

that is using it as a texture. Calling saveTexture again will not save another copy

of the same texture, it will just rename the key of the existing copy.

By default it will create a single base texture. You can add frames to the texture

by using the Texture.add method. After doing this, you can then allow Game Objects

to use a specific frame from a Render Texture.

If you destroy this Render Texture, any Game Object using it via the Texture Manager will

stop rendering. Ensure you remove the texture from the Texture Manager and any Game Objects

using it first, before destroying this Render Texture.

Parameters:

nametypeoptionaldescription
keystringNoThe unique key to store the texture as within the global Texture Manager.

Returns: Phaser.Textures.DynamicTexture - The Texture that was saved.

Source: src/gameobjects/rendertexture/RenderTexture.js#L212
Since: 3.12.0


setRenderMode

<instance> setRenderMode(mode, [preserve])

Description:

Set the renderMode of this Render Texture.

Set this to change how the Render Texture is rendered.

  • 'render' mode draws the contents of the Render Texture to each frame.

  • 'redraw' mode calls render() and redraws the texture every frame,

    but does not render itself. This is useful for updating textures

    for reuse by other objects.

  • 'all' mode calls render() then draws the texture to the frame.

Parameters:

nametypeoptionaldefaultdescription
mode'render' | 'redraw''all'No
preservebooleanYesfalseWhether to call preserve(true) to preserve the current command buffer.

Returns: Phaser.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L261
Since: 4.0.0


setSize

<instance> setSize(width, height)

Description:

Sets the internal size of this Render Texture, as used for frame or physics body creation.

This will not change the size that the Game Object is rendered in-game.

For that you need to either set the scale of the Game Object (setScale) or call the

setDisplaySize method, which is the same thing as changing the scale but allows you

to do so by giving pixel values. You could also call the resize method, as that

will resize the underlying texture.

If you have enabled this Game Object for input, changing the size will also change the

size of the hit area, unless you have defined a custom hit area.

Parameters:

nametypeoptionaldescription
widthnumberNoThe width of this Game Object.
heightnumberNoThe height of this Game Object.

Overrides: Phaser.GameObjects.Image#setSize

Returns: Phaser.GameObjects.RenderTexture - This Game Object instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L143
Since: 3.0.0


snapshot

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

Description:

Takes a snapshot of the whole of this Render Texture.

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

To capture a portion of this Render 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 Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L592
Since: 3.19.0


snapshotArea

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

Description:

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

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

To capture the whole Render 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 Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L557
Since: 3.19.0


snapshotPixel

<instance> snapshotPixel(x, y, callback)

Description:

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

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

To capture the whole Render Texture see the snapshot method.

To capture a portion of this Render 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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L621
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.GameObjects.RenderTexture - This Render Texture instance.

Source: src/gameobjects/rendertexture/RenderTexture.js#L351
Since: 3.60.0