Capítulo 832 de 859
Instead of creating one WebGL canvas/renderer per 3D diagram on a page (which runs into WebGL context limits and resource-sharing problems), use a single full-viewport canvas and one Scene per "virtual canvas" element, rendering each scene into the screen-space rectangle of its corresponding DOM element via the scissor/viewport.
z-index: -1) sits behind the page; regular DOM elements mark where each "virtual canvas" should appear.renderer.setScissorTest, setViewport, setScissor: restrict rendering to the on-screen rectangle matching each marker element before rendering that element's scene.position: fixed canvas doesn't scroll with the page, so slow renders visibly lag behind scrolled content; switching the canvas to position: absolute and repositioning it via a transform on each render lets it scroll with the page, trading a possible edge glitch for no mid-page slip.{elem, fn} pairs (or a name→init-function map driven by HTML dataset attributes) lets the main render loop stay agnostic of what each scene actually draws, only rendering scenes whose element is currently visible on screen.TrackballControls to an individual scene just means constructing it with that scene's own camera and DOM element, and updating it inside that scene's render function.<canvas> per element — more compositing flexibility (real HTML elements instead of one shared background canvas) at the cost of an extra copy per area.function makeScene(elem) {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, 2, 0.1, 5);
camera.position.set(0, 1, 2);
camera.lookAt(0, 0, 0);
scene.add(new THREE.DirectionalLight(0xffffff, 1));
return { scene, camera, elem };
}
position: fixed keeps the shared canvas visually static during scroll (causing lag on slow frames); position: absolute with a per-frame transform lets it scroll with the page instead.dataset-driven map) of scenes with their own init/render functions keeps the main render loop reusable across any number of diagrams.TrackballControls) just need their own camera/DOM-element pairing, same as a full-page three.js app.