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.
Keep a model's turn on one axis, and link the two sides of a diff
32eefb58a7206f46dfbc280b2d038894e4db865e · Keenan Nemetz · 9d ago · parent 4489805d
Keep a model's turn on one axis, and link the two sides of a diff The free trackball composes a rotation about the screen's right and up axes on every pointer move, so a hand-drawn diagonal drag adds a little roll nobody asked for, and after a couple of drags the part is off its own vertical with no gesture that spins it in place. The first five pixels of a drag now pick the dominant axis and the rest of the gesture stays on it: it is still a trackball, but a mostly horizontal drag can no longer tilt the model, and the travel spent before the choice is applied rather than lost. There was also no way to ask for a known side, and no way to compare the two sides of a diff at one angle. Front, Top and Right turn the model to meet the fixed camera: each target maps the model's own axes onto the camera's own basis, so the side is exact whatever the zoom, and the turn eases there with a slerp, or arrives at once under prefers-reduced-motion. The row marks the view the model is at and a drag clears it. Two viewers in one .media-diff are the same file before and after, framed on the same unit sphere, so an orientation means the same thing in both. They now publish their turn to each other and start linked; Sync unlinks them for a reader who wants each side at its own angle, and linking again hands over the angle of the viewer that was clicked. The pair is a WeakMap keyed by the container, so a lone model gets no button; a disposed viewer leaves the set, and a viewer that finishes loading second takes the turn that is already there. The toggle buttons now carry aria-pressed as well as the active class, so the wireframe and the link announce their state instead of only showing it. Matrix4 and Quaternion are re-exported from the bundle for the view targets, regenerated with scripts/vendor-three.py; the docs say what the new buttons do. Checked in headless Chromium against the bundled three.js, with two STL models and a third host outside a .media-diff: a drag with 3px of vertical drift turns about the screen up axis only, a diagonal drag stays on one axis, the sibling follows and stops following when unlinked, each view puts its own axis at the reader, and reset and the lone model's toolbar are unchanged. vole workflow run verify passes. Closes: 01M87CY5GP3CS51X8KGCX2W4V4
server/web/static/models/model-viewer.js +182−11
| @@ -6,15 +6,19 @@ | |||
| 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, a pointer and four controls. Drag turns the model over | ||
| 10 | // in any direction, the wheel or a pinch zooms, and the buttons repeat the zoom, | ||
| 11 | // reset the view and toggle a wireframe, so a reader who only clicks can still | ||
| 12 | // move around. The model is centred and scaled to a unit sphere, so a part | ||
| 13 | // exported in millimetres and one in metres frame the same way. | ||
| 9 | // A viewer is a canvas, a pointer and a few buttons. Drag turns the model over | ||
| 10 | // in any direction, the wheel or a pinch zooms, and the buttons repeat the | ||
| 11 | // zoom, put a named side of the model toward the reader, reset the view and | ||
| 12 | // toggle a wireframe, so a reader who only clicks can still move around. A drag | ||
| 13 | // commits to the axis it starts on, so a mostly horizontal drag cannot tilt the | ||
| 14 | // model, and two viewers in one diff turn together unless the reader unlinks | ||
| 15 | // them. The model is centred and scaled to a unit sphere, so a part exported in | ||
| 16 | // millimetres and one in metres frame the same way. | ||
| 14 | 17 | ||
| 15 | 18 | import { | |
| 16 | 19 | WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight, | |
| 17 | Box3, Vector3, Group, Mesh, MeshStandardMaterial, SRGBColorSpace, | ||
| 20 | Box3, Vector3, Matrix4, Quaternion, Group, Mesh, MeshStandardMaterial, | ||
| 21 | SRGBColorSpace, | ||
| 18 | 22 | OrbitControls, STLLoader, OBJLoader, GLTFLoader, ThreeMFLoader, | |
| 19 | 23 | } from './three.bundle.js'; | |
| 20 | 24 | ||
| @@ -62,6 +66,38 @@ const button = (label, title, onClick) => { | |||
| 62 | 66 | return element; | |
| 63 | 67 | }; | |
| 64 | 68 | ||
| 69 | // pressed shows a button that holds a mode. The attribute carries the same state | ||
| 70 | // to a screen reader, so the drawn and announced states cannot drift apart. | ||
| 71 | const pressed = (element, on) => { | ||
| 72 | element.classList.toggle('active', on); | ||
| 73 | element.setAttribute('aria-pressed', on ? 'true' : 'false'); | ||
| 74 | }; | ||
| 75 | |||
| 76 | // reduceMotion reports the reader's preference, so a snap can arrive at its view | ||
| 77 | // at once instead of turning there. | ||
| 78 | const reduceMotion = () => typeof matchMedia === 'function' | ||
| 79 | && matchMedia('(prefers-reduced-motion: reduce)').matches; | ||
| 80 | |||
| 81 | // The two sides of a diff are one file before and after, drawn in one | ||
| 82 | // .media-diff. Each is framed the same way, so an orientation means the same | ||
| 83 | // thing in both, and a reader comparing them wants them turned together. | ||
| 84 | // Viewers that share a container publish their turn to the rest unless the | ||
| 85 | // reader unlinks them to look at each side on its own. | ||
| 86 | const pairs = new WeakMap(); | ||
| 87 | |||
| 88 | // pairFor is the shared turn for a host's container, or null when the container | ||
| 89 | // holds one model and there is nothing to turn with. | ||
| 90 | const pairFor = (host) => { | ||
| 91 | const container = host.closest('.media-diff'); | ||
| 92 | if (!container || container.querySelectorAll('[data-model]').length < 2) return null; | ||
| 93 | let pair = pairs.get(container); | ||
| 94 | if (!pair) { | ||
| 95 | pair = { linked: true, turn: null, viewers: new Set() }; | ||
| 96 | pairs.set(container, pair); | ||
| 97 | } | ||
| 98 | return pair; | ||
| 99 | }; | ||
| 100 | |||
| 65 | 101 | // disposeTree frees the geometry, materials and textures a loaded model owns. | |
| 66 | 102 | // A viewer that is swapped out of the page must give its GPU memory back. | |
| 67 | 103 | const disposeTree = (node) => { | |
| @@ -164,6 +200,35 @@ export const upgrade = (host) => { | |||
| 164 | 200 | // that never changes. | |
| 165 | 201 | controls.addEventListener('change', render); | |
| 166 | 202 | ||
| 203 | // The turn a diff pair shares. adopt takes a sibling's turn and cancels any | ||
| 204 | // snap in flight, so two animations cannot fight over the same quaternion. | ||
| 205 | let snap = 0; | ||
| 206 | const adoptTurn = (turn) => { | ||
| 207 | if (snap) { cancelAnimationFrame(snap); snap = 0; } | ||
| 208 | spin.quaternion.copy(turn); | ||
| 209 | render(); | ||
| 210 | }; | ||
| 211 | const pair = pairFor(host); | ||
| 212 | const member = { adopt: adoptTurn }; | ||
| 213 | if (pair) pair.viewers.add(member); | ||
| 214 | |||
| 215 | // publish hands this viewer's turn to its siblings. A pair starts linked; the | ||
| 216 | // Sync button turns the link off. | ||
| 217 | const publish = () => { | ||
| 218 | if (!pair || !pair.linked) return; | ||
| 219 | pair.turn = spin.quaternion.clone(); | ||
| 220 | for (const other of pair.viewers) { | ||
| 221 | if (other !== member) other.adopt(pair.turn); | ||
| 222 | } | ||
| 223 | }; | ||
| 224 | |||
| 225 | // viewButtons lets the row of views show which one the model is at. A drag off | ||
| 226 | // a view clears the row, so it never claims an angle the model has left. | ||
| 227 | const viewButtons = new Map(); | ||
| 228 | const markView = (name) => { | ||
| 229 | for (const [view, element] of viewButtons) pressed(element, view === name); | ||
| 230 | }; | ||
| 231 | |||
| 167 | 232 | // A drag turns the model, not the camera. Each move is a rotation about the | |
| 168 | 233 | // screen's own right and up axes, so the surface follows the pointer and the | |
| 169 | 234 | // model can be rolled over and over in any direction. A drag the height of the | |
| @@ -172,6 +237,12 @@ export const upgrade = (host) => { | |||
| 172 | 237 | let turning = null; | |
| 173 | 238 | let turnX = 0; | |
| 174 | 239 | let turnY = 0; | |
| 240 | // A gesture commits to one axis once the pointer has moved this far, so the | ||
| 241 | // few pixels a hand spends on the other axis do not roll the model. | ||
| 242 | const lockPx = 5; | ||
| 243 | let axis = null; // 'horizontal' or 'vertical' once the drag has chosen | ||
| 244 | let alongX = 0; // travel since the drag began, until it has chosen | ||
| 245 | let alongY = 0; | ||
| 175 | 246 | const screenRight = new Vector3(); | |
| 176 | 247 | const screenUp = new Vector3(); | |
| 177 | 248 | const screenOut = new Vector3(); | |
| @@ -185,6 +256,9 @@ export const upgrade = (host) => { | |||
| 185 | 256 | canvas.addEventListener('pointerdown', (event) => { | |
| 186 | 257 | pointers.add(event.pointerId); | |
| 187 | 258 | canvas.setPointerCapture(event.pointerId); | |
| 259 | axis = null; | ||
| 260 | alongX = 0; | ||
| 261 | alongY = 0; | ||
| 188 | 262 | // A second pointer is a pinch, which belongs to OrbitControls' zoom: it | |
| 189 | 263 | // cancels the turn instead of turning while the fingers spread. | |
| 190 | 264 | if (!root || event.button !== 0 || pointers.size !== 1) { | |
| @@ -194,6 +268,7 @@ export const upgrade = (host) => { | |||
| 194 | 268 | turning = event.pointerId; | |
| 195 | 269 | turnX = event.clientX; | |
| 196 | 270 | turnY = event.clientY; | |
| 271 | markView(''); | ||
| 197 | 272 | }); | |
| 198 | 273 | ||
| 199 | 274 | canvas.addEventListener('pointermove', (event) => { | |
| @@ -203,17 +278,75 @@ export const upgrade = (host) => { | |||
| 203 | 278 | turnX = event.clientX; | |
| 204 | 279 | turnY = event.clientY; | |
| 205 | 280 | if (dx === 0 && dy === 0) return; | |
| 281 | // A hand-drawn drag is never straight. The first few pixels decide which | ||
| 282 | // axis the reader is asking for and the rest of the drag stays on it, so a | ||
| 283 | // turn cannot roll the model a little more on every stroke. | ||
| 284 | let moveX = dx; | ||
| 285 | let moveY = dy; | ||
| 286 | if (!axis) { | ||
| 287 | alongX += dx; | ||
| 288 | alongY += dy; | ||
| 289 | if (Math.abs(alongX) < lockPx && Math.abs(alongY) < lockPx) return; | ||
| 290 | axis = Math.abs(alongX) >= Math.abs(alongY) ? 'horizontal' : 'vertical'; | ||
| 291 | moveX = alongX; | ||
| 292 | moveY = alongY; | ||
| 293 | } | ||
| 206 | 294 | camera.updateMatrixWorld(); | |
| 207 | 295 | camera.matrixWorld.extractBasis(screenRight, screenUp, screenOut); | |
| 208 | 296 | const rate = (2 * Math.PI) / Math.max(1, canvas.clientHeight); | |
| 209 | spin.rotateOnWorldAxis(screenUp, dx * rate); | ||
| 210 | spin.rotateOnWorldAxis(screenRight, dy * rate); | ||
| 297 | if (axis === 'horizontal') spin.rotateOnWorldAxis(screenUp, moveX * rate); | ||
| 298 | else spin.rotateOnWorldAxis(screenRight, moveY * rate); | ||
| 211 | 299 | render(); | |
| 300 | publish(); | ||
| 212 | 301 | }); | |
| 213 | 302 | ||
| 214 | 303 | canvas.addEventListener('pointerup', endTurn); | |
| 215 | 304 | canvas.addEventListener('pointercancel', endTurn); | |
| 216 | 305 | ||
| 306 | // A view puts a named side of the model toward the reader. The camera stays | ||
| 307 | // where it is and the model turns to meet it, so a view is the turn that maps | ||
| 308 | // the model's own axes onto the screen's: front sets +z at the reader with +y | ||
| 309 | // up, top puts +y there with +x to the right, right puts +x there with +y up. | ||
| 310 | // The targets are built from the camera's basis when asked for, so they hold | ||
| 311 | // whatever the zoom. | ||
| 312 | const viewTurn = (name) => { | ||
| 313 | camera.updateMatrixWorld(); | ||
| 314 | const right = new Vector3(); | ||
| 315 | const up = new Vector3(); | ||
| 316 | const out = new Vector3(); | ||
| 317 | camera.matrixWorld.extractBasis(right, up, out); | ||
| 318 | const basis = new Matrix4(); | ||
| 319 | if (name === 'front') basis.makeBasis(right, up, out); | ||
| 320 | else if (name === 'top') basis.makeBasis(right, out, up.clone().negate()); | ||
| 321 | else basis.makeBasis(out, up, right.clone().negate()); | ||
| 322 | return new Quaternion().setFromRotationMatrix(basis); | ||
| 323 | }; | ||
| 324 | |||
| 325 | // turnTo eases to a view and publishes each frame the way a drag does, so a | ||
| 326 | // linked pair arrives together. | ||
| 327 | const turnTo = (target) => { | ||
| 328 | if (snap) cancelAnimationFrame(snap); | ||
| 329 | snap = 0; | ||
| 330 | const from = spin.quaternion.clone(); | ||
| 331 | if (reduceMotion() || from.angleTo(target) < 0.001) { | ||
| 332 | spin.quaternion.copy(target); | ||
| 333 | render(); | ||
| 334 | publish(); | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | const start = performance.now(); | ||
| 338 | const step = (now) => { | ||
| 339 | const progress = Math.min(1, (now - start) / 220); | ||
| 340 | // A smoothstep leaves and arrives at rest, so the view settles instead of | ||
| 341 | // stopping dead at it. | ||
| 342 | spin.quaternion.copy(from).slerp(target, progress * progress * (3 - 2 * progress)); | ||
| 343 | render(); | ||
| 344 | publish(); | ||
| 345 | snap = progress < 1 ? requestAnimationFrame(step) : 0; | ||
| 346 | }; | ||
| 347 | snap = requestAnimationFrame(step); | ||
| 348 | }; | ||
| 349 | |||
| 217 | 350 | // fitDistance is the camera distance that puts a unit sphere inside the | |
| 218 | 351 | // narrower of the two field-of-view angles, so a narrow pane does not crop | |
| 219 | 352 | // the model. | |
| @@ -255,6 +388,8 @@ export const upgrade = (host) => { | |||
| 255 | 388 | // centre rather than a corner. | |
| 256 | 389 | const place = () => { | |
| 257 | 390 | if (!root) return; | |
| 391 | // A snap still in flight would overwrite the reset on its next frame. | ||
| 392 | if (snap) { cancelAnimationFrame(snap); snap = 0; } | ||
| 258 | 393 | // Reset the turn and the scale before measuring: the box is measured in world | |
| 259 | 394 | // space, and a previous turn would measure the model at an angle. The world | |
| 260 | 395 | // matrix is recomputed here because the measurement below reads the parent's | |
| @@ -313,6 +448,8 @@ export const upgrade = (host) => { | |||
| 313 | 448 | const dispose = () => { | |
| 314 | 449 | if (disposed) return; | |
| 315 | 450 | disposed = true; | |
| 451 | if (snap) cancelAnimationFrame(snap); | ||
| 452 | if (pair) pair.viewers.delete(member); | ||
| 316 | 453 | if (observer) observer.disconnect(); | |
| 317 | 454 | else window.removeEventListener('resize', resize); | |
| 318 | 455 | host.removeEventListener('vole:dispose', dispose); | |
| @@ -344,6 +481,8 @@ export const upgrade = (host) => { | |||
| 344 | 481 | fail('This model is empty.'); | |
| 345 | 482 | return; | |
| 346 | 483 | } | |
| 484 | // The other side may already have been turned while this one loaded. | ||
| 485 | if (pair && pair.linked && pair.turn) adoptTurn(pair.turn); | ||
| 347 | 486 | status.remove(); | |
| 348 | 487 | if (fallback) fallback.remove(); | |
| 349 | 488 | ||
| @@ -353,14 +492,46 @@ export const upgrade = (host) => { | |||
| 353 | 492 | // repeat the zoom for a reader who only clicks. | |
| 354 | 493 | const zoomOut = button('\u2212', 'Zoom out', () => zoomBy(1.25)); | |
| 355 | 494 | const zoomIn = button('+', 'Zoom in', () => zoomBy(0.8)); | |
| 356 | const reset = button('Reset view', 'Reset view', place); | ||
| 495 | const reset = button('Reset view', 'Reset view', () => { | ||
| 496 | markView(''); | ||
| 497 | place(); | ||
| 498 | publish(); | ||
| 499 | }); | ||
| 357 | 500 | const wireframe = button('Wireframe', 'Toggle wireframe', () => { | |
| 358 | 501 | const on = !wireframe.classList.contains('active'); | |
| 359 | wireframe.classList.toggle('active', on); | ||
| 502 | pressed(wireframe, on); | ||
| 360 | 503 | setWireframe(on); | |
| 361 | 504 | }); | |
| 362 | 505 | tools.append(zoomOut, zoomIn, reset, wireframe); | |
| 363 | host.append(tools); | ||
| 506 | // A diff holds two viewers of the same file, and a reader comparing them | ||
| 507 | // wants one angle in both. The button turns that link off; linking again | ||
| 508 | // makes this viewer's angle the shared one, so neither jumps on the next | ||
| 509 | // drag. | ||
| 510 | if (pair) { | ||
| 511 | const sync = button('Sync', 'Turn both models together', () => { | ||
| 512 | pair.linked = !pair.linked; | ||
| 513 | pressed(sync, pair.linked); | ||
| 514 | publish(); | ||
| 515 | }); | ||
| 516 | pressed(sync, pair.linked); | ||
| 517 | tools.append(sync); | ||
| 518 | } | ||
| 519 | |||
| 520 | // The snap views sit in their own row, low and to the left, so a narrow diff | ||
| 521 | // pane wraps one cluster rather than two. | ||
| 522 | const views = document.createElement('div'); | ||
| 523 | views.className = 'model-views'; | ||
| 524 | for (const name of ['Front', 'Top', 'Right']) { | ||
| 525 | const view = name.toLowerCase(); | ||
| 526 | const element = button(name, name + ' view', () => { | ||
| 527 | markView(view); | ||
| 528 | turnTo(viewTurn(view)); | ||
| 529 | }); | ||
| 530 | viewButtons.set(view, element); | ||
| 531 | views.append(element); | ||
| 532 | } | ||
| 533 | |||
| 534 | host.append(tools, views); | ||
| 364 | 535 | resize(); | |
| 365 | 536 | }, undefined, () => { | |
| 366 | 537 | fail('This model could not be loaded. Download it to open it in a 3D application.'); | |