Take a screenshot in the browser
Turn what the 3D viewer shows into a PNG, with or without a background, and render a series of images at a fixed size from a viewer that is never on the page.
viewer.screenshot() turns the viewer's canvas into a PNG in the browser: no server involved, and exactly what the user sees. For images without a browser, from a backend or a report, the server renders them instead (Render a screenshot on the server).
Live demo#
Screenshot copies the current view; Background gives it a white background instead of a transparent one. Three views off screen creates a second viewer that is never added to the page, sizes it to 1024 × 640 and renders three views of the model. Click an image to save it.
Steps#
// screenshot() copies what the canvas shows: after a camera change, let the viewer draw first
const drawn = () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
// Offer a Blob as a file
function save(blob, filename) {
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
}
// 1. What the viewer on the page shows, as a PNG; transparent unless you give a background
const blob = await viewer.screenshot({backgroundColor: "#FFFFFF"});
save(blob, "view-" + viewer.getWidth() + "x" + viewer.getHeight() + ".png");
// 2. A series of images at a fixed size, from a viewer that is never added to the page
const offscreenModels = new ModelManager(client);
const offscreen = new Viewer3d(new ObjectStateManager(offscreenModels));
await offscreen.initializeViewer();
offscreen.resize(1024, 640);
await offscreenModels.addModels([await client.fs.getNodeVersion("<version uuid of the first demo model>")]);
const directions = {southwest: [-0.5, -0.5, 0.3], northeast: [0.5, 0.5, 0.3], top: [0, 0, 1]};
for (const [name, viewDirection] of Object.entries(directions)) {
await offscreen.viewFitModels(["<version uuid of the first demo model>"], {viewDirection, animate: false});
await drawn();
save(await offscreen.screenshot(), name + ".png");
}
// A stored viewpoint works the same way: await offscreen.getCamera().setViewpoint(viewpoint, 0)
offscreen.destroy();
The off-screen viewer has managers of its own, so the viewer on the page is not touched.
- Take it.
await viewer.screenshot(options)returns a PNGBlobof the canvas at its current size (viewer.getWidth()×viewer.getHeight()). The background is transparent;backgroundColorputs a colour behind the model.annotations: truedraws the template annotations into the image too, without their cards (Annotate a model);includeText: truethe labels of a plan (how-to/viewer3d/show-a-floor-plan#room-labels). - Let it draw first. The image is what the canvas shows at that moment. After moving the camera from code, wait for the viewer to draw, two animation frames, before taking the screenshot; otherwise you get the previous view.
- Fix the size off screen. A viewer that is never added to the page renders all the same:
resize(width, height)gives it the size of the images you want, independent of any screen. Load the model into it, set the camera (Set the camera from code) and take a screenshot per view.destroy()frees it afterwards. - Save it. The
Blobcan be offered as a download through an object URL, uploaded, or put in an<img>.
Check#
The saved PNG has the size of the viewer, or of the resize call, and shows the view the camera was given before the screenshot.