API

Render Scenes To .png Files

The CreateScreenshot functions can be used to render a screenshot of your scene, allowing you to download it or even use it as a texture in the scene itself. The available methods are the following:

CreateScreenshot

import { Tools } from "@babylonjs/core/Misc/tools";
scene.onReadyObservable.add(() => {
Tools.CreateScreenshot(engine, camera, size, successCallback, mimeType, forceDownload, quality, useFill);
});

Observe that the screenshot creation is inside the scene's onReadyObservable, so it can wait for the meshes to be drawn. Otherwise, they won't appear in the screenshot.

Simple Screenshot Example

The size parameter can be one of the following:

  • A {width: number, height: number} with the width and height of the canvas and the created screenshot.
  • A {precision: number} value that is a multiplier of the current resolution of the canvas.
  • A single number indicating both width and height.

The optional useFill parameter, when set to true, allows the render canvas to fill the screenshot. Otherwise, the render canvas will be "letterboxed" if its aspect ratio does not match the aspect ratio of the screenshot size.

The success callback is a function that contains the screenshot as a string of base64-encoded characters. This string can be assigned to the src attribute of an <img> to display it, or it can be used to create a new Texture in the scene.

Use screenshot as texture

More information about the other arguments of the function can be found in its documentation page.

This function also has a CreateScreenshotAsync version that can be awaited using the await keyword.

NOTE: If creating a screenshot with a camera that is NOT the scene's active camera, use CreateScreenshotUsingRenderTarget instead.

CreateScreenshotUsingRenderTarget

As the name suggests, this function renders the scene to a RenderTargetTexture for the screenshot. It has a few more configuration options:

import { Tools } from "@babylonjs/core/Misc/tools";
Tools.CreateScreenshotUsingRenderTarget(engine, camera, size, successCallback, mimeType, samples, antialiasing, fileName, renderSprites, enableStencilBuffer, useLayerMask, quality, customizeTexture);

The customizeTexture callback can be used to modify the texture before taking the screenshot. This can be used, for example, to modify its renderList property, changing which objects will appear in the screenshot. You can also turn post-processes on or off.

CreateScreenshotUsingRenderTarget

It also has a CreateScreenshotUsingRenderTargetAsync version.