Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Sync the zoom between the two sides of a model diff

7d989146a0e792a4df512cdcb13b46f8c3918fde · Keenan Nemetz · 8d ago · parent f865b269

Sync the zoom between the two sides of a model diff

The pair shared a turn but not the camera distance, so a linked diff could show each side at a different zoom. Share the distance on every controls change, adopt it when the other side loads, and publish both halves on reset and on Sync re-link. Framing a model stays local so a load or a resize cannot overwrite the shared view, and adoption does not publish the value straight back.

server/web/static/models/model-viewer.js +73−24

@@ -12,9 +12,9 @@
12 12 // mix without ever rolling it. The wheel or a pinch zooms, and the buttons
13 13 // repeat the zoom, reset the view and toggle a wireframe, so a reader who only
14 14 // clicks can still move around. The cube puts a named side of the model toward
15 // the reader. Two viewers in one diff turn together unless the reader unlinks
16 // them. The model is centred and scaled to a unit sphere, so a part exported in
17 // millimetres and one in metres frame the same way.
15 // the reader. Two viewers in one diff turn and zoom together unless the reader
16 // unlinks them. The model is centred and scaled to a unit sphere, so a part
17 // exported in millimetres and one in metres frame the same way.
18 18
19 19 import {
20 20 WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight,
@@ -92,9 +92,9 @@ const reduceMotion = () => typeof matchMedia === 'function'
92 92
93 93 // The two sides of a diff are one file before and after, drawn in one
94 94 // .media-diff. Each is framed the same way, so an orientation means the same
95 // thing in both, and a reader comparing them wants them turned together.
96 // Viewers that share a container publish their turn to the rest unless the
97 // reader unlinks them to look at each side on its own.
95 // thing in both, and a reader comparing them wants them turned and zoomed
96 // together. Viewers that share a container publish their view to the rest
97 // unless the reader unlinks them to look at each side on its own.
98 98 const pairs = new WeakMap();
99 99
100 100 // pairFor is the shared turn for a host's container, or null when the container
@@ -104,7 +104,7 @@ const pairFor = (host) => {
104 104 if (!container || container.querySelectorAll('[data-model]').length < 2) return null;
105 105 let pair = pairs.get(container);
106 106 if (!pair) {
107 pair = { linked: true, turn: null, viewers: new Set() };
107 pair = { linked: true, turn: null, distance: 0, viewers: new Set() };
108 108 pairs.set(container, pair);
109 109 }
110 110 return pair;
@@ -215,8 +215,18 @@ export const upgrade = (host) => {
215 215
216 216 // OrbitControls moves the camera and fires change; it does not draw. This
217 217 // viewer renders on demand, so without this the wheel would zoom a picture
218 // that never changes.
219 controls.addEventListener('change', render);
218 // that never changes. A change is also the moment a zoom belongs to a linked
219 // sibling, so the handler below publishes it. adopting and placing mark a
220 // camera move this viewer did not ask for, which must not travel back out.
221 let adopting = false;
222 let placing = false;
223 let publishTurn = () => {};
224 let publishZoom = () => {};
225 const onViewChange = () => {
226 render();
227 if (!adopting && !placing) publishZoom();
228 };
229 controls.addEventListener('change', onViewChange);
220 230
221 231 // The turn a diff pair shares. adopt takes a sibling's turn and cancels any
222 232 // snap in flight, so two animations cannot fight over the same quaternion.
@@ -228,19 +238,46 @@ export const upgrade = (host) => {
228 238 markView('');
229 239 render();
230 240 };
241 // adoptZoom copies a sibling's camera distance but keeps this viewer's own
242 // line to its target, so two panes framed at different sizes still share the
243 // zoom rather than jumping to a position that only fits one of them. The
244 // controls report the copy as a change; adopting suppresses the publish that
245 // would send it straight back.
246 const adoptZoom = (distance) => {
247 if (!(distance > 0)) return;
248 const offset = camera.position.clone().sub(controls.target);
249 if (offset.lengthSq() === 0) offset.set(1.1, 0.75, 1.4);
250 adopting = true;
251 camera.position.copy(controls.target).add(offset.setLength(distance));
252 controls.update();
253 adopting = false;
254 };
231 255 const pair = pairFor(host);
232 const member = { adopt: adoptTurn };
256 const member = { adopt: adoptTurn, zoom: adoptZoom };
233 257 if (pair) pair.viewers.add(member);
234 258
235 // publish hands this viewer's turn to its siblings. A pair starts linked; the
236 // Sync button turns the link off.
237 const publish = () => {
259 // publish hands this viewer's turn and zoom to its siblings. A pair starts
260 // linked; the Sync button turns the link off. The two halves stay separate so
261 // a drag or a view snap publishes only the turn, and the controls publish
262 // only the zoom.
263 publishTurn = () => {
238 264 if (!pair || !pair.linked) return;
239 265 pair.turn = spin.quaternion.clone();
240 266 for (const other of pair.viewers) {
241 267 if (other !== member) other.adopt(pair.turn);
242 268 }
243 269 };
270 publishZoom = () => {
271 if (!pair || !pair.linked) return;
272 pair.distance = camera.position.distanceTo(controls.target);
273 for (const other of pair.viewers) {
274 if (other !== member) other.zoom(pair.distance);
275 }
276 };
277 const publish = () => {
278 publishTurn();
279 publishZoom();
280 };
244 281
245 282 // viewButtons lets the cube and its accessible list show which view the
246 283 // model is at. A drag off a view clears both, so neither claims an angle the
@@ -314,7 +351,7 @@ export const upgrade = (host) => {
314 351 spin.rotateOnWorldAxis(spinAxis, dx * (2 * Math.PI) / Math.max(1, canvas.clientWidth));
315 352 spin.rotateOnWorldAxis(screenRight, dy * (2 * Math.PI) / Math.max(1, canvas.clientHeight));
316 353 render();
317 publish();
354 publishTurn();
318 355 });
319 356
320 357 canvas.addEventListener('pointerup', endTurn);
@@ -357,7 +394,7 @@ export const upgrade = (host) => {
357 394 if (reduceMotion() || from.angleTo(target) < 0.001) {
358 395 spin.quaternion.copy(target);
359 396 render();
360 publish();
397 publishTurn();
361 398 return;
362 399 }
363 400 const start = performance.now();
@@ -367,7 +404,7 @@ export const upgrade = (host) => {
367 404 // stopping dead at it.
368 405 spin.quaternion.copy(from).slerp(target, progress * progress * (3 - 2 * progress));
369 406 render();
370 publish();
407 publishTurn();
371 408 snap = progress < 1 ? requestAnimationFrame(step) : 0;
372 409 };
373 410 snap = requestAnimationFrame(step);
@@ -389,7 +426,15 @@ export const upgrade = (host) => {
389 426 camera.position.copy(controls.target).add(direction);
390 427 camera.lookAt(controls.target);
391 428 camera.updateProjectionMatrix();
392 controls.update();
429 // Framing is this viewer's own doing, not a zoom the reader asked to share.
430 // Keep it off the pair while it moves the camera; a reset publishes the
431 // result once place has finished.
432 placing = true;
433 try {
434 controls.update();
435 } finally {
436 placing = false;
437 }
393 438 render();
394 439 };
395 440
@@ -479,7 +524,7 @@ export const upgrade = (host) => {
479 524 if (observer) observer.disconnect();
480 525 else window.removeEventListener('resize', resize);
481 526 host.removeEventListener('vole:dispose', dispose);
482 controls.removeEventListener('change', render);
527 controls.removeEventListener('change', onViewChange);
483 528 controls.dispose();
484 529 if (root) disposeTree(root);
485 530 renderer.dispose();
@@ -507,8 +552,12 @@ export const upgrade = (host) => {
507 552 fail('This model is empty.');
508 553 return;
509 554 }
510 // The other side may already have been turned while this one loaded.
511 if (pair && pair.linked && pair.turn) adoptTurn(pair.turn);
555 // The other side may already have been turned or zoomed while this one
556 // loaded.
557 if (pair && pair.linked) {
558 if (pair.turn) adoptTurn(pair.turn);
559 if (pair.distance) adoptZoom(pair.distance);
560 }
512 561 status.remove();
513 562 if (fallback) fallback.remove();
514 563
@@ -530,11 +579,11 @@ export const upgrade = (host) => {
530 579 });
531 580 tools.append(zoomOut, zoomIn, reset, wireframe);
532 581 // A diff holds two viewers of the same file, and a reader comparing them
533 // wants one angle in both. The button turns that link off; linking again
534 // makes this viewer's angle the shared one, so neither jumps on the next
535 // drag.
582 // wants one angle and one zoom in both. The button turns that link off;
583 // linking again makes this viewer's view the shared one, so neither jumps
584 // on the next drag.
536 585 if (pair) {
537 const sync = button('Sync', 'Turn both models together', () => {
586 const sync = button('Sync', 'Turn and zoom both models together', () => {
538 587 pair.linked = !pair.linked;
539 588 pressed(sync, pair.linked);
540 589 publish();