Capítulo 849 de 859
A grab-bag of small, common gotchas: taking a screenshot needs to capture right after a render (browsers clear the WebGL drawing buffer by default), keyboard input needs an explicit tabindex, and making a transparent canvas or a page background needs a few specific renderer options.
canvas.toDataURL()/canvas.toBlob() often capture a black image because the browser clears the WebGL drawing buffer after each render for performance/compatibility reasons — call your capture code immediately after a fresh render, not on some later timer.preserveDrawingBuffer: true: prevents the browser from auto-clearing the canvas between frames (useful for drawing/painting-style apps), but the canvas still clears whenever its resolution changes (e.g. on window resize) — for a real persistent-drawing app, render to a render target instead.tabindex: a canvas doesn't receive keyboard events by default; setting tabindex="0" (or higher) on it enables focus and keyboard input, but then also needs outline: none in CSS to avoid an unwanted focus ring.alpha: true to the WebGLRenderer constructor, and typically set premultipliedAlpha: false to match how materials output alpha by default (the canvas itself defaults to premultipliedAlpha: true, which is a mismatch worth understanding before relying on canvas transparency).z-index: -1 behind normal page content (simplest, but your JS must coexist with the rest of the page's scripts), or an iframe styled to fill and sit behind the page (isolates the three.js code entirely from the host page's JavaScript).function render() {
// ...resize + draw...
renderer.render(scene, camera);
}
function animate(time) {
render();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
// capture immediately after a render, not on a delayed timer:
render();
canvas.toBlob((blob) => saveBlob(blob, "screenshot.png"));
preserveDrawingBuffer: true stops auto-clearing between frames but still clears on canvas resize; use a render target instead for a real persistent-drawing feature.tabindex to receive keyboard events, and pair it with outline: none in CSS to avoid an unwanted focus ring.alpha: true and (usually) premultipliedAlpha: false on the WebGLRenderer.z-index: -1) is simplest; an iframe fully isolates the three.js code's JavaScript from the rest of the page.alpha/premultipliedAlpha/preserveDrawingBuffer constructor options covered here.