vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Show a model from a document, and redraw the viewer
0be968244db3882594fa65032ff6b4cf59385bb9 · Keenan Nemetz · 11d ago · parent ffbcb4cc
Show a model from a document, and redraw the viewer A README that names a 3D model with an image path was emitting an <img> of a mesh. The raw route serves model/stl, no browser draws that, so the reader saw a broken image where the rack was supposed to be. A model image in a document now becomes the same viewer host the code browser writes, with the image's alt text as its label and the download link inside it for a reader without JavaScript or WebGL. Only a document with a place in a tree does this: an issue body has no raw route to fetch. The sanitiser admits exactly that host's shape, so the one classed element in a rendered document is the one the renderer wrote. The viewer also never redrew after OrbitControls moved the camera: it renders on demand, and only the button handlers called render. A drag or a wheel zoom changed the camera and left the old frame on screen, which is why the viewer looked static. Listen for the controls' change event, add zoom in/out buttons clamped to the wheel's own limits, and give the canvas a grab cursor. Docs now say a Markdown path names a model the same way it names a picture, and that the code browser's size cap cannot apply to a document rendered from its text alone.
server/web/static/models/model-viewer.js +39−7
| @@ -6,9 +6,11 @@ | |||
| 6 | 6 | // link inside, so a reader without JavaScript or without WebGL still has the | |
| 7 | 7 | // file. | |
| 8 | 8 | // | |
| 9 | // A viewer is a canvas and two controls: orbit and zoom (OrbitControls) and a | ||
| 10 | // wireframe toggle. The model is centred and scaled to a unit sphere, so a part | ||
| 11 | // exported in millimetres and one in metres frame the same way. | ||
| 9 | // A viewer is a canvas, a pointer and four controls. Drag orbits, the wheel or a | ||
| 10 | // pinch zooms, and the buttons repeat the zoom, reset the view and toggle a | ||
| 11 | // wireframe, so a reader who only clicks can still move around. The model is | ||
| 12 | // centred and scaled to a unit sphere, so a part exported in millimetres and one | ||
| 13 | // in metres frame the same way. | ||
| 12 | 14 | ||
| 13 | 15 | import { | |
| 14 | 16 | WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight, | |
| @@ -46,11 +48,16 @@ const loaderFor = (format) => { | |||
| 46 | 48 | } | |
| 47 | 49 | }; | |
| 48 | 50 | ||
| 49 | const button = (label, onClick) => { | ||
| 51 | // button makes one control. label is what a reader sees, and title is the same | ||
| 52 | // action in words: it is the tooltip, the accessible name for a symbol, and what | ||
| 53 | // a screen reader announces. | ||
| 54 | const button = (label, title, onClick) => { | ||
| 50 | 55 | const element = document.createElement('button'); | |
| 51 | 56 | element.type = 'button'; | |
| 52 | 57 | element.className = 'model-tool'; | |
| 53 | 58 | element.textContent = label; | |
| 59 | element.title = title; | ||
| 60 | element.setAttribute('aria-label', title); | ||
| 54 | 61 | element.addEventListener('click', onClick); | |
| 55 | 62 | return element; | |
| 56 | 63 | }; | |
| @@ -143,6 +150,12 @@ export const upgrade = (host) => { | |||
| 143 | 150 | if (!disposed) renderer.render(scene, camera); | |
| 144 | 151 | }; | |
| 145 | 152 | ||
| 153 | // OrbitControls moves the camera and fires change; it does not draw. This | ||
| 154 | // viewer renders on demand, so without this a drag orbits nothing the reader | ||
| 155 | // can see: the frame on screen stays the one before the drag, and the wheel | ||
| 156 | // zooms a picture that never changes. | ||
| 157 | controls.addEventListener('change', render); | ||
| 158 | |||
| 146 | 159 | // fitDistance is the camera distance that puts a unit sphere inside the | |
| 147 | 160 | // narrower of the two field-of-view angles, so a narrow pane does not crop | |
| 148 | 161 | // the model. | |
| @@ -215,6 +228,20 @@ export const upgrade = (host) => { | |||
| 215 | 228 | render(); | |
| 216 | 229 | }; | |
| 217 | 230 | ||
| 231 | // zoomBy moves the camera along its line to the orbit target. The buttons and | ||
| 232 | // the wheel have to stop in the same place, so the distance is clamped to the | ||
| 233 | // limits OrbitControls enforces rather than left to grow without bound. update() | ||
| 234 | // fires change, and the change listener above draws the result. | ||
| 235 | const zoomBy = (factor) => { | ||
| 236 | const offset = camera.position.clone().sub(controls.target); | ||
| 237 | const distance = Math.min( | ||
| 238 | Math.max(offset.length() * factor, controls.minDistance), | ||
| 239 | controls.maxDistance, | ||
| 240 | ); | ||
| 241 | camera.position.copy(controls.target).add(offset.setLength(distance)); | ||
| 242 | controls.update(); | ||
| 243 | }; | ||
| 244 | |||
| 218 | 245 | const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(resize) : null; | |
| 219 | 246 | if (observer) observer.observe(host); | |
| 220 | 247 | else window.addEventListener('resize', resize); | |
| @@ -226,6 +253,7 @@ export const upgrade = (host) => { | |||
| 226 | 253 | if (observer) observer.disconnect(); | |
| 227 | 254 | else window.removeEventListener('resize', resize); | |
| 228 | 255 | host.removeEventListener('vole:dispose', dispose); | |
| 256 | controls.removeEventListener('change', render); | ||
| 229 | 257 | controls.dispose(); | |
| 230 | 258 | if (root) disposeTree(root); | |
| 231 | 259 | renderer.dispose(); | |
| @@ -258,13 +286,17 @@ export const upgrade = (host) => { | |||
| 258 | 286 | ||
| 259 | 287 | const tools = document.createElement('div'); | |
| 260 | 288 | tools.className = 'model-tools'; | |
| 261 | const reset = button('Reset view', place); | ||
| 262 | const wireframe = button('Wireframe', () => { | ||
| 289 | // Orbit covers the pointer: drag orbits and the wheel or a pinch zooms, and | ||
| 290 | // these two repeat the zoom for a reader who only clicks. | ||
| 291 | const zoomOut = button('\u2212', 'Zoom out', () => zoomBy(1.25)); | ||
| 292 | const zoomIn = button('+', 'Zoom in', () => zoomBy(0.8)); | ||
| 293 | const reset = button('Reset view', 'Reset view', place); | ||
| 294 | const wireframe = button('Wireframe', 'Toggle wireframe', () => { | ||
| 263 | 295 | const on = !wireframe.classList.contains('active'); | |
| 264 | 296 | wireframe.classList.toggle('active', on); | |
| 265 | 297 | setWireframe(on); | |
| 266 | 298 | }); | |
| 267 | tools.append(reset, wireframe); | ||
| 299 | tools.append(zoomOut, zoomIn, reset, wireframe); | ||
| 268 | 300 | host.append(tools); | |
| 269 | 301 | resize(); | |
| 270 | 302 | }, undefined, () => { | |