rig-review 2.4.6 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,10 @@ import {enCatalog} from './catalog-en.ts';
4
4
  import {koCatalog} from './catalog-ko.ts';
5
5
  import {t} from './i18n.ts';
6
6
  import {JOB_HUB_READY, jobHubMessageMatches, type JobHubBinding} from './job-hub.ts';
7
+ import {recordJobRigDraw, freezeJobRigCapture, copyJobRigViewport, createJobRigSubmission, jobRigPixelPlacement,
8
+ type JobRigDraw, type JobRigDrawState} from './job-rig-capture.ts';
9
+ import {presentJobRigViewport, type JobRigPresentation} from './viewport-capture.ts';
10
+ import {CanvasRenderer} from '@rig-c/play';
7
11
 
8
12
  /** Imported runtimes may throw arbitrary prose. Only registered refusal copy reaches the window. */
9
13
  export function jobWindowRefusal(error: unknown, fallback = 'JOB_WINDOW_FAILED'): string {
@@ -125,6 +129,7 @@ export function createJobWindow<Prepared>(options: {
125
129
  }) {
126
130
  const { transport, adapter, pageId } = options;
127
131
  const pending = new Map<string, JobPendingAsk>();
132
+ const rigAttempts = new WeakSet<JobUISubmission>();
128
133
  const retainedDrafts: JobRetainedDraft[] = [];
129
134
  let generation = 0;
130
135
  let displayed: JobDisplayPublication | null = null;
@@ -256,14 +261,16 @@ export function createJobWindow<Prepared>(options: {
256
261
  if (closed)
257
262
  return { ok: false, error: { code: 'JOB_CLOSED', message: 'JOB_CLOSED' } };
258
263
  const ask = pending.get(input.askId);
264
+ const retry = input.capture !== undefined && rigAttempts.has(input);
259
265
  if (preparing || !displayed || !receipt ||
260
266
  input.publicationId !== displayed.publication.publicationId ||
261
267
  input.pageId !== pageId || input.displayedNonce !== receipt.displayedNonce ||
262
268
  input.expectedGeneration > generation ||
263
- ask && (ask.status !== 'pending' || ask.publicationId !== input.publicationId || ask.subject.kind !== input.kind)) {
269
+ ask && (ask.status !== 'pending' && !retry || ask.publicationId !== input.publicationId || ask.subject.kind !== input.kind)) {
264
270
  return { ok: false, error: { code: 'JOB_ACT_SOURCE_STALE', message: 'JOB_ACT_SOURCE_STALE' } };
265
271
  }
266
272
  // The backend authenticates the actual Request and validates the semantic value.
273
+ if (input.capture !== undefined) rigAttempts.add(input);
267
274
  return transport.submit(input);
268
275
  }
269
276
  return {
@@ -609,8 +616,9 @@ export interface JobPreparedInspection {
609
616
  assets: ModelAssets | null;
610
617
  player: RigPlayer | null;
611
618
  rigRoot: HTMLElement;
619
+ rigPresentation?: JobRigPresentation;
612
620
  inspection: PartInspection | null;
613
- setupPose(): void;
621
+ setupPose(setup?: boolean): void;
614
622
  dispose(): void;
615
623
  }
616
624
  async function jobBytes(url: string, signal: AbortSignal, maximum: number): Promise<Uint8Array<ArrayBuffer>> {
@@ -689,12 +697,19 @@ export async function prepareJobInspection(display: JobDisplayPublication, signa
689
697
  let assets: ModelAssets | null = null;
690
698
  let player: RigPlayer | null = null;
691
699
  let inspection: PartInspection | null = null;
692
- let resetToSetup = () => {};
700
+ let resetToSetup: (setup?: boolean) => void = () => {};
701
+ let rigPresentation: JobRigPresentation | undefined;
702
+ function clearRigPresentation(): void {
703
+ rigPresentation?.canvas.parentElement?.removeAttribute('data-job-rig-canonical');
704
+ rigPresentation?.canvas.remove();
705
+ rigPresentation = undefined;
706
+ }
693
707
  let disposed = false;
694
708
  const dispose = () => {
695
709
  if (disposed)
696
710
  return;
697
711
  disposed = true;
712
+ clearRigPresentation();
698
713
  for (const video of media) {
699
714
  video.pause();
700
715
  video.removeAttribute('src');
@@ -879,13 +894,17 @@ export async function prepareJobInspection(display: JobDisplayPublication, signa
879
894
  const loadedAssets = assets;
880
895
  const parts = await import('./part-selection.ts');
881
896
  const catalog = await parts.loadPartModel(modelUrl, modelHash, metadata.feedbackCatalog.slots ?? [], checkedFetch);
882
- resetToSetup = () => {
897
+ resetToSetup = (setup = true) => {
898
+ clearRigPresentation();
883
899
  player?.dispose();
884
900
  player = new runtime.RigPlayer(rigRoot, loadedAssets);
885
- player.draw();
886
901
  inspection = new parts.PartInspection();
887
902
  inspection.register('single', catalog.entries, player);
888
- inspection.present('single');
903
+ if (setup) {
904
+ player.clear(0, {mix: 0});
905
+ player.draw();
906
+ inspection.present('single');
907
+ }
889
908
  };
890
909
  resetToSetup();
891
910
  }
@@ -898,9 +917,14 @@ export async function prepareJobInspection(display: JobDisplayPublication, signa
898
917
  references.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id));
899
918
  return {
900
919
  display, metadata, interview, parts, partImages, interviewImages, imageSetImages, references, assets, rigRoot,
920
+ get rigPresentation() { return rigPresentation; },
921
+ set rigPresentation(value) {
922
+ clearRigPresentation();
923
+ rigPresentation = value;
924
+ },
901
925
  get player() { return player; },
902
926
  get inspection() { return inspection; },
903
- setupPose: () => resetToSetup(),
927
+ setupPose: (setup = true) => resetToSetup(setup),
904
928
  dispose,
905
929
  };
906
930
  }
@@ -974,12 +998,15 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
974
998
  read: () => JobUISubmission['value'];
975
999
  submissionId: string;
976
1000
  dirty: boolean;
1001
+ rigSubmission: ReturnType<typeof createJobRigSubmission>;
977
1002
  };
978
1003
  type PreparedWindow = JobPreparedInspection & {
979
1004
  element: HTMLElement;
980
1005
  forms: Map<string, JobForm>;
981
1006
  };
982
1007
  const forms = new Map<string, JobForm>();
1008
+ const drawn = new WeakMap<JobPreparedInspection, JobRigDraw>();
1009
+ let drawGeneration = 0;
983
1010
  const viewportState = createViewportState();
984
1011
  const el = <Element extends HTMLElement = HTMLElement>(id: string): Element => {
985
1012
  const element = workspace.querySelector<HTMLElement>('#' + id);
@@ -1003,6 +1030,89 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1003
1030
  fit.textContent = t(fit.textContent ?? 'Fit');
1004
1031
  };
1005
1032
  const viewport = createViewportController(provider, viewportState);
1033
+ const renderViewport = provider.render;
1034
+ provider.render = controller => {
1035
+ try {renderViewport(controller); rememberDraw();}
1036
+ catch (error) {
1037
+ if (current) {
1038
+ drawn.delete(current);
1039
+ current.rigPresentation = undefined;
1040
+ }
1041
+ throw error;
1042
+ }
1043
+ };
1044
+ function rigDrawState(): JobRigDrawState {
1045
+ const owner = current, player = owner?.player;
1046
+ if (!owner || !player || !hubVisible) throw new Error('JOB_STILL_CAPTURE_FAILED');
1047
+ const pane = el('rig-viewport'), root = owner.rigRoot, rect = pane.getBoundingClientRect();
1048
+ const ratio = globalThis.devicePixelRatio || 1;
1049
+ const frame = viewport.paneFrame('rig'), camera = viewport.numericView().cameras.rig;
1050
+ if (!frame || !camera || !pane.isConnected || !root.isConnected || el('rig-pane').hidden ||
1051
+ rect.width <= 0 || rect.height <= 0) throw new Error('JOB_STILL_CAPTURE_FAILED');
1052
+ const style = getComputedStyle(root), paneStyle = getComputedStyle(pane);
1053
+ const plain = style.filter === 'none' && style.perspective === 'none' && style.opacity === '1' &&
1054
+ paneStyle.filter === 'none' && paneStyle.transform === 'none' && paneStyle.perspective === 'none' && paneStyle.opacity === '1';
1055
+ const presentation = owner.rigPresentation;
1056
+ const canvas = presentation?.canvas;
1057
+ const pixels = canvas?.getBoundingClientRect();
1058
+ const canvasStyle = canvas ? getComputedStyle(canvas) : null;
1059
+ if (canvas && (!canvas.isConnected || canvasStyle?.visibility !== 'visible' ||
1060
+ canvasStyle.display === 'none' || canvasStyle.opacity !== '1' || canvasStyle.filter !== 'none' ||
1061
+ canvasStyle.transform !== 'none')) throw new Error('JOB_STILL_CAPTURE_FAILED');
1062
+ return {player, presentation, frameGeneration: owner.inspection?.frame('single')?.generation,
1063
+ source: {pageId, publicationId: owner.display.publication.publicationId,
1064
+ instanceKey: owner.display.publication.instanceKey, sourceHash: owner.display.publication.source.sourceHash},
1065
+ layout: JSON.stringify([
1066
+ pane.clientWidth, pane.clientHeight, rect.width, rect.height, ratio,
1067
+ root.getAttribute('style'), style.transform, camera, presentation?.area, presentation?.ratio,
1068
+ pixels ? jobRigPixelPlacement(rect, pixels, ratio) : null,
1069
+ pixels?.width, pixels?.height, canvas?.width, canvas?.height, canvas?.getAttribute('style'),
1070
+ ]),
1071
+ defaultCamera: plain && camera.zoom === 1 && camera.center.x === frame.center.x && camera.center.y === frame.center.y,
1072
+ wholeRig: !selectedPart && !owner.inspection?.value() && player.slots === null,
1073
+ playing: false};
1074
+ }
1075
+ function rememberDraw(): void {
1076
+ const owner = current;
1077
+ if (!owner) return;
1078
+ try {
1079
+ const player = owner.player;
1080
+ const posed = owner.inspection?.frame('single');
1081
+ const camera = viewport.numericView().cameras.rig;
1082
+ if (!hubVisible || !player || !owner.assets || !posed || !camera) {
1083
+ throw new Error('JOB_STILL_CAPTURE_FAILED');
1084
+ }
1085
+ const renderer = player.renderer;
1086
+ const pane = el('rig-viewport');
1087
+ const presentation = presentJobRigViewport({pane, root: owner.rigRoot, images: true,
1088
+ canvas: renderer instanceof CanvasRenderer ? renderer.canvas : undefined,
1089
+ backend: renderer instanceof CanvasRenderer ? renderer.backendActive : renderer.meshBackendActive},
1090
+ {camera, posed, assets: owner.assets});
1091
+ pane.append(presentation.canvas);
1092
+ owner.rigPresentation = presentation;
1093
+ pane.dataset.jobRigCanonical = '';
1094
+ drawn.set(owner, recordJobRigDraw(rigDrawState(), ++drawGeneration));
1095
+ } catch {
1096
+ drawn.delete(owner);
1097
+ owner.rigPresentation = undefined;
1098
+ }
1099
+ }
1100
+ function drawInspection(owner: JobPreparedInspection): void {
1101
+ drawn.delete(owner);
1102
+ owner.rigPresentation = undefined;
1103
+ owner.player?.draw();
1104
+ owner.inspection?.present('single');
1105
+ }
1106
+ function copyRigVerdict(ask: JobPendingAsk, owner: JobPreparedInspection) {
1107
+ if (owner !== current || ask.publicationId !== owner.display.publication.publicationId) {
1108
+ throw new Error('JOB_STILL_BINDING_MISMATCH');
1109
+ }
1110
+ assertJobAskSource(ask, owner.display);
1111
+ return freezeJobRigCapture(drawn.get(owner), rigDrawState(), ask.askId, () => {
1112
+ if (!owner.rigPresentation) throw new Error('JOB_STILL_CAPTURE_FAILED');
1113
+ return copyJobRigViewport(owner.rigPresentation);
1114
+ });
1115
+ }
1006
1116
  const referenceSelection = createJobReferenceSelection<JobReferenceView>({
1007
1117
  current: () => ({owner: current, time}),
1008
1118
  present(value) {
@@ -1225,7 +1335,8 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1225
1335
  const status = node('p');
1226
1336
  status.setAttribute('role', 'status');
1227
1337
  form.append(status);
1228
- const entry = { form, fieldset: fields, read, submissionId: crypto.randomUUID(), dirty: false };
1338
+ const entry: JobForm = { form, fieldset: fields, read, submissionId: crypto.randomUUID(), dirty: false,
1339
+ rigSubmission: createJobRigSubmission() };
1229
1340
  try {
1230
1341
  const saved = JSON.parse(localStorage.getItem(localDraftKey(prepared.display)) ?? 'null');
1231
1342
  if (saved?.publicationId === prepared.display.publication.publicationId &&
@@ -1248,25 +1359,36 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1248
1359
  }
1249
1360
  }
1250
1361
  catch { /* Invalid or unavailable local drafts grant no submission authority. */ }
1251
- form.oninput = () => { entry.dirty = true; entry.submissionId = crypto.randomUUID(); saveDraft(); };
1362
+ form.oninput = () => { entry.dirty = true; entry.submissionId = crypto.randomUUID(); entry.rigSubmission.reset(); saveDraft(); };
1252
1363
  form.onsubmit = async (event) => {
1253
1364
  event.preventDefault();
1254
1365
  const state = window.state();
1255
1366
  const receipt = state.receipt;
1256
1367
  if (!receipt || state.preparing || state.closed || !access.capability || !form.reportValidity())
1257
1368
  return;
1258
- fields.disabled = true;
1259
1369
  try {
1260
- const result = await window.submit({ kind: subject.kind, publicationId: ask.publicationId,
1370
+ const input = { kind: subject.kind, publicationId: ask.publicationId,
1261
1371
  askId: ask.askId, pageId, displayedNonce: receipt.displayedNonce,
1262
- expectedGeneration: state.generation, submissionId: entry.submissionId, value: read() } as JobUISubmission);
1372
+ expectedGeneration: state.generation, submissionId: entry.submissionId, value: read() } as JobUISubmission;
1373
+ const activeAsk = state.pending.find(item => item.askId === ask.askId);
1374
+ if (!activeAsk) throw new Error('JOB_ASK_INVALID');
1375
+ const pending = subject.kind === 'review-verdict' && subject.stage === 'rig-review'
1376
+ ? entry.rigSubmission.prepare(input, () => copyRigVerdict(activeAsk, prepared)) : Promise.resolve(input);
1377
+ fields.disabled = true;
1378
+ const frozen = await pending;
1379
+ if (entry.submissionId !== frozen.submissionId) throw new Error('JOB_STILL_BINDING_MISMATCH');
1380
+ const result = await window.submit(frozen);
1263
1381
  if (!result.ok)
1264
1382
  throw new Error(result.error.code);
1265
1383
  entry.dirty = false;
1384
+ entry.rigSubmission.reset();
1385
+ status.dataset.jobText = 'job.saved';
1266
1386
  status.textContent = t('job.saved');
1267
1387
  saveDraft();
1268
1388
  }
1269
1389
  catch (error) {
1390
+ status.dataset.jobText = error instanceof Error && Object.hasOwn(enCatalog, error.message) && Object.hasOwn(koCatalog, error.message)
1391
+ ? error.message : 'JOB_ACT_INVALID';
1270
1392
  status.textContent = jobWindowRefusal(error, 'JOB_ACT_INVALID');
1271
1393
  fields.disabled = false;
1272
1394
  }
@@ -1506,7 +1628,10 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1506
1628
  function selectPart(): void {
1507
1629
  if (!current?.inspection)
1508
1630
  return;
1631
+ drawn.delete(current);
1632
+ current.rigPresentation = undefined;
1509
1633
  current.inspection.select(selectedPart ? { side: 'single', slot: selectedPart, mode: partMode } : null);
1634
+ rememberDraw();
1510
1635
  setText('job-part-observation', selectedPart ? JSON.stringify(current.inspection.observation(), null, 2) : '');
1511
1636
  viewport.renderCameraControls();
1512
1637
  }
@@ -1536,14 +1661,17 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1536
1661
  return;
1537
1662
  }
1538
1663
  if (!animation && current.player?.current) current.setupPose();
1664
+ // Setup uses an empty composed track. Animation seek needs a fresh single track.
1665
+ if (animation && current.player?.composed) current.setupPose(false);
1539
1666
  if (current.player) {
1667
+ drawn.delete(current);
1668
+ current.rigPresentation = undefined;
1540
1669
  if (animation && current.player.current !== animation)
1541
1670
  current.player.play(animation, { loop: true });
1542
1671
  if (animation)
1543
1672
  current.player.seek(time);
1544
1673
  current.player.pause();
1545
- current.player.draw();
1546
- current.inspection?.present('single');
1674
+ drawInspection(current);
1547
1675
  current.inspection?.select(selectedPart ? { side: 'single', slot: selectedPart, mode: partMode } : null);
1548
1676
  setText('job-part-observation', selectedPart ? JSON.stringify(current.inspection?.observation(), null, 2) : '');
1549
1677
  }
@@ -1690,8 +1818,8 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1690
1818
  }
1691
1819
  await reference()?.seek(time, signal);
1692
1820
  if (prepared.player) {
1693
- prepared.player.draw();
1694
- prepared.inspection?.present('single');
1821
+ drawInspection(prepared);
1822
+ rememberDraw();
1695
1823
  }
1696
1824
  await new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
1697
1825
  if (signal.aborted)
@@ -1757,14 +1885,31 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1757
1885
  i18n.setLinkLanguage(null); i18n.setLanguagePreference(event.data.language);
1758
1886
  }
1759
1887
  pauseHiddenHubFrame();
1760
- if (hubVisible) current?.player?.draw();
1888
+ if (current && !hubVisible) {
1889
+ drawn.delete(current);
1890
+ current.rigPresentation = undefined;
1891
+ }
1892
+ if (hubVisible && current?.player) {drawInspection(current); rememberDraw();}
1761
1893
  };
1762
1894
  if (hubIncarnation) {globalThis.addEventListener('message', hubVisibility); announceHubFrame();}
1763
1895
  events = startJobWindowEvents({access, signal: abort.signal, member: !!hubIncarnation,
1764
1896
  event: event => window.event(event),
1765
1897
  refused: error => {notice.textContent = jobWindowRefusal(error, 'JOB_STREAM_FAILED');},
1766
1898
  });
1899
+ let resolution: MediaQueryList | undefined;
1900
+ const resolutionChanged = () => {
1901
+ resolution?.removeEventListener('change', resolutionChanged);
1902
+ resolution = globalThis.matchMedia(`(resolution:${globalThis.devicePixelRatio || 1}dppx)`);
1903
+ resolution.addEventListener('change', resolutionChanged);
1904
+ if (current) {
1905
+ drawn.delete(current);
1906
+ current.rigPresentation = undefined;
1907
+ viewport.fitCamera();
1908
+ }
1909
+ };
1910
+ resolutionChanged();
1767
1911
  const stop = () => {
1912
+ resolution?.removeEventListener('change', resolutionChanged);
1768
1913
  globalThis.removeEventListener('message', hubVisibility);
1769
1914
  saveDraft();
1770
1915
  referenceSelection.cancel();
package/web/parts.ts CHANGED
@@ -546,8 +546,13 @@ function setupPartsComments():void {
546
546
  if (typeof document !== 'undefined' && document.getElementById('parts-app')) void boot();
547
547
 
548
548
  function renderCorrections():void {
549
+ const data = state.data;
550
+ if (!stageEmbedded && (!data || (!Object.hasOwn(data, 'draft') && !Object.hasOwn(data, 'draftUnavailable')))) {
551
+ document.getElementById('pv-corrections')?.remove();
552
+ return;
553
+ }
549
554
  let host=document.getElementById('pv-corrections');if(!host){host=document.createElement('section');host.id='pv-corrections';document.querySelector('.pv-toolbar')!.after(host);}
550
- host.replaceChildren();const data=state.data,item=data?.items[state.selected],mapping=data?.draft?.mappings.find(m=>m.part===item?.name);
555
+ host.replaceChildren();const item=data?.items[state.selected],mapping=data?.draft?.mappings.find(m=>m.part===item?.name);
551
556
  const label=document.createElement('p');label.textContent=t(data?.draft&&!mapping?'STAGE_DRAFT_NO_MESH: this part has no mesh attachment; direct correction is unavailable.':data?.draft?.label??data?.draftUnavailable??'STAGE_DRAFT_UNAVAILABLE: no producer-compatible draft is supplied.');host.append(label);
552
557
  if(!mapping||!data?.draft)return;
553
558
  label.textContent+=` · ${mapping.part} · ${mapping.skin}/${mapping.slot}/${mapping.attachment} · v ${mapping.vertices} t ${mapping.triangles} · ${data.draft.buildId}`;
package/web/styles.css CHANGED
@@ -462,3 +462,17 @@ body.job-window{display:grid;grid-template-rows:auto auto minmax(0,1fr);height:1
462
462
  .job-window .job-part-images{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:6px}
463
463
  .job-window .job-part-images img{width:100%;height:auto;background:var(--rv-media-bg)}
464
464
  @media(max-width:780px){.job-window .job-inspection{grid-template-columns:160px minmax(0,1fr)}.job-window .job-stage #stage-layout{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}}
465
+
466
+
467
+ .job-window #rig-viewport[data-job-rig-canonical] > #rig-root {
468
+ visibility: hidden;
469
+ }
470
+ .job-window #rig-viewport > .job-rig-presentation {
471
+ position: absolute;
472
+ z-index: 0;
473
+ visibility: visible;
474
+ pointer-events: none;
475
+ user-select: none;
476
+ transform: none;
477
+ opacity: 1;
478
+ }
@@ -13,34 +13,81 @@ export function pixelAlignedCrop(area:CaptureRect,origin:{x:number;y:number},rat
13
13
  const x=Math.ceil((origin.x+area.x)*ratio)/ratio-origin.x,y=Math.ceil((origin.y+area.y)*ratio)/ratio-origin.y;
14
14
  return {x,y,width:Math.max(0,Math.floor((origin.x+area.x+area.width)*ratio)/ratio-origin.x-x),height:Math.max(0,Math.floor((origin.y+area.y+area.height)*ratio)/ratio-origin.y-y)};
15
15
  }
16
- export interface BackgroundLayer {rect:CaptureRect;color:string;image:string;size:string;position:string}
16
+ export interface BackgroundLayer {rect:CaptureRect;color:string;image:string;size:string;position:string;clip?:CaptureRect}
17
17
  export interface PixelLayer {image:CanvasImageSource;rect:CaptureRect;opacity:number}
18
- export interface ViewportCapture {pane:HTMLElement;root?:HTMLElement;images:boolean;canvas?:HTMLCanvasElement;backend:CanvasBackend;nearest?:boolean}
18
+ export interface ViewportCapture {pane:HTMLElement;root?:HTMLElement;images:boolean;canvas?:HTMLCanvasElement;backend:CanvasBackend;nearest?:boolean;strictActualPixels?:boolean;includeOverlays?:boolean}
19
19
  const transparent=(color:string)=>color==='transparent'||/^rgba\([^)]*,\s*0\s*\)$/.test(color);
20
+ const cssMatrix=(transform:string)=>transform==='none'?new DOMMatrix():new DOMMatrix(transform);
20
21
  /** Read only this app's computed backgrounds, in their actual viewport coordinates. */
21
- export function viewportBackground(pane:HTMLElement):BackgroundLayer[]{
22
+ export function viewportBackground(pane:HTMLElement,strict=false):BackgroundLayer[]{
22
23
  const origin=pane.getBoundingClientRect(),x=origin.left+pane.clientLeft,y=origin.top+pane.clientTop,result:BackgroundLayer[]=[];
23
- for(let node:HTMLElement|null=pane;node;node=node.parentElement){const style=getComputedStyle(node),rect=node.getBoundingClientRect();result.unshift({rect:{x:rect.left+node.clientLeft-x,y:rect.top+node.clientTop-y,width:node.clientWidth,height:node.clientHeight},color:style.backgroundColor,image:style.backgroundImage,size:style.backgroundSize,position:style.backgroundPosition});}
24
+ for(let node:HTMLElement|null=pane;node;node=node.parentElement){
25
+ const style=getComputedStyle(node),rect=node.getBoundingClientRect();
26
+ let positioning={x:rect.left+node.clientLeft-x,y:rect.top+node.clientTop-y,width:node.clientWidth,height:node.clientHeight};
27
+ let clip:CaptureRect|undefined;
28
+ if(strict){
29
+ if(style.transform!=='none')throw new Error('JOB_STILL_CAPTURE_FAILED');
30
+ const number=(value:string)=>parseFloat(value)||0;
31
+ const border={left:number(style.borderLeftWidth),right:number(style.borderRightWidth),top:number(style.borderTopWidth),bottom:number(style.borderBottomWidth)};
32
+ const padding={left:number(style.paddingLeft),right:number(style.paddingRight),top:number(style.paddingTop),bottom:number(style.paddingBottom)};
33
+ const borderBox={x:rect.left-x,y:rect.top-y,width:rect.width,height:rect.height};
34
+ const paddingBox={x:borderBox.x+border.left,y:borderBox.y+border.top,width:borderBox.width-border.left-border.right,height:borderBox.height-border.top-border.bottom};
35
+ const contentBox={x:paddingBox.x+padding.left,y:paddingBox.y+padding.top,width:paddingBox.width-padding.left-padding.right,height:paddingBox.height-padding.top-padding.bottom};
36
+ const boxes={'border-box':borderBox,'padding-box':paddingBox,'content-box':contentBox};
37
+ const originName=style.backgroundOrigin||'padding-box',clipName=style.backgroundClip||'border-box';
38
+ if(!Object.hasOwn(boxes,originName)||!Object.hasOwn(boxes,clipName))throw new Error('JOB_STILL_CAPTURE_FAILED');
39
+ positioning=boxes[originName as keyof typeof boxes];clip=boxes[clipName as keyof typeof boxes];
40
+ }
41
+ result.unshift({rect:positioning,color:style.backgroundColor,image:style.backgroundImage,size:style.backgroundSize,position:style.backgroundPosition,...(clip?{clip}:{})});
42
+ }
24
43
  return result;
25
44
  }
45
+ /** Rasterize a CSS hard-stop tile before repeating it. Coverage is sampled at device-pixel centres. */
46
+ function paintHardStopTile(ctx:CanvasRenderingContext2D,layer:BackgroundLayer,colors:string[],sx:number,sy:number,radius:number,ox:number,oy:number):void {
47
+ const ratio=globalThis.devicePixelRatio||1,width=sx*ratio,height=sy*ratio;
48
+ const stops=[...layer.image.matchAll(/\)\s+([\d.]+)px/g)].map(match=>Number(match[1]));
49
+ if(!Number.isInteger(width)||!Number.isInteger(height)||width<1||height<1||width>16384||height>16384||width*height>16777216||
50
+ stops.length!==2||stops.some(stop=>stop!==radius)||!transparent(colors[1]!))throw new Error('JOB_STILL_CAPTURE_FAILED');
51
+ const tile=document.createElement('canvas');tile.width=width;tile.height=height;
52
+ const pixels=tile.getContext('2d');if(!pixels)throw new Error('JOB_STILL_CAPTURE_FAILED');
53
+ pixels.fillStyle=colors[0]!;pixels.fillRect(0,0,width,height);
54
+ const image=pixels.getImageData(0,0,width,height);
55
+ for(let y=0;y<height;y++)for(let x=0;x<width;x++){
56
+ if(Math.hypot((x+.5)/ratio-sx/2,(y+.5)/ratio-sy/2)>radius)image.data.fill(0,(y*width+x)*4,(y*width+x+1)*4);
57
+ }
58
+ pixels.putImageData(image,0,0);
59
+ const clip=layer.clip??layer.rect;
60
+ const firstX=ox+Math.floor((clip.x-ox)/sx)*sx,firstY=oy+Math.floor((clip.y-oy)/sy)*sy;
61
+ ctx.save();
62
+ try{
63
+ ctx.imageSmoothingEnabled=false;
64
+ for(let y=firstY;y<clip.y+clip.height;y+=sy)for(let x=firstX;x<clip.x+clip.width;x+=sx)ctx.drawImage(tile,x,y,sx,sy);
65
+ }finally{ctx.restore();}
66
+ }
26
67
  /** These are the app's solid, stage-ellipse and grid backgrounds, not an HTML rasterizer. */
27
- export function paintBackground(ctx:CanvasRenderingContext2D,layers:BackgroundLayer[],area:CaptureRect):void {
28
- for(const layer of layers){const r=layer.rect;if(!transparent(layer.color)){ctx.fillStyle=layer.color;ctx.fillRect(r.x,r.y,r.width,r.height);}if(!layer.image||layer.image==='none')continue;
68
+ export function paintBackground(ctx:CanvasRenderingContext2D,layers:BackgroundLayer[],area:CaptureRect,strict=false):void {
69
+ for(const layer of layers){
70
+ if(strict){const clip=layer.clip??layer.rect;ctx.save();ctx.beginPath();ctx.rect(clip.x,clip.y,clip.width,clip.height);ctx.clip();}
71
+ try{
72
+ const r=layer.rect,fill=strict?(layer.clip??r):r;if(!transparent(layer.color)){ctx.fillStyle=layer.color;ctx.fillRect(fill.x,fill.y,fill.width,fill.height);}if(!layer.image||layer.image==='none')continue;
29
73
  const colors=layer.image.match(/rgba?\([^)]+\)|#[\da-f]+/gi)??[];if(!layer.image.startsWith('radial-gradient(')||colors.length!==2)throw new Error('COMMENT_BACKGROUND_UNAVAILABLE: unsupported viewport background');
30
74
  const sizes=layer.size.split(/\s+/).map(Number.parseFloat),radius=layer.image.match(/\)\s+([\d.]+)px/);
31
75
  if(radius&&sizes.length===2&&sizes.every(n=>Number.isFinite(n)&&n>0)){
32
76
  const [sx,sy]=sizes as [number,number],position=layer.position.split(/\s+/),offset=(v:string,extent:number,tile:number)=>v.endsWith('%')?(extent-tile)*parseFloat(v)/100:parseFloat(v);
33
- const ox=r.x+offset(position[0]??'0%',r.width,sx),oy=r.y+offset(position[1]??'0%',r.height,sy);ctx.save();ctx.beginPath();ctx.rect(r.x,r.y,r.width,r.height);ctx.clip();ctx.fillStyle=colors[0]!;
77
+ const ox=r.x+offset(position[0]??'0%',r.width,sx),oy=r.y+offset(position[1]??'0%',r.height,sy);
78
+ if(strict){paintHardStopTile(ctx,layer,colors,sx,sy,Number(radius[1]),ox,oy);continue;}
79
+ ctx.save();ctx.beginPath();ctx.rect(r.x,r.y,r.width,r.height);ctx.clip();ctx.fillStyle=colors[0]!;
34
80
  for(let y=oy+Math.floor((area.y-oy)/sy)*sy+sy/2;y<area.y+area.height+sy;y+=sy)for(let x=ox+Math.floor((area.x-ox)/sx)*sx+sx/2;x<area.x+area.width+sx;x+=sx){ctx.beginPath();ctx.arc(x,y,Number(radius[1]),0,Math.PI*2);ctx.fill();}ctx.restore();
35
81
  }else{
36
82
  const position=layer.image.match(/at\s+([\d.]+)%\s+([\d.]+)%/),end=layer.image.match(/([\d.]+)%\s*\)$/);if(!position||!end)throw new Error('COMMENT_BACKGROUND_UNAVAILABLE: unknown stage ellipse');
37
83
  const cx=r.width*Number(position[1])/100,cy=r.height*Number(position[2])/100,rx=Math.max(cx,r.width-cx)*Math.SQRT2,ry=Math.max(cy,r.height-cy)*Math.SQRT2;if(rx<=0||ry<=0)continue;
38
84
  ctx.save();ctx.translate(r.x+cx,r.y+cy);ctx.scale(rx,ry);const gradient=ctx.createRadialGradient(0,0,0,0,0,1);gradient.addColorStop(0,colors[0]!);gradient.addColorStop(Number(end[1])/100,colors[1]!);gradient.addColorStop(1,colors[1]!);ctx.fillStyle=gradient;ctx.fillRect(-cx/rx,-cy/ry,r.width/rx,r.height/ry);ctx.restore();
39
85
  }
86
+ }finally{if(strict)ctx.restore();}
40
87
  }
41
88
  }
42
- export function paintViewportLayers(ctx:CanvasRenderingContext2D,area:CaptureRect,background:BackgroundLayer[],images:boolean,drawImages:()=>void,overlays:PixelLayer[]):void {
43
- ctx.save();ctx.beginPath();ctx.rect(area.x,area.y,area.width,area.height);ctx.clip();paintBackground(ctx,background,area);if(images)drawImages();
89
+ export function paintViewportLayers(ctx:CanvasRenderingContext2D,area:CaptureRect,background:BackgroundLayer[],images:boolean,drawImages:()=>void,overlays:PixelLayer[],strict=false):void {
90
+ ctx.save();ctx.beginPath();ctx.rect(area.x,area.y,area.width,area.height);ctx.clip();paintBackground(ctx,background,area,strict);if(images)drawImages();
44
91
  for(const layer of overlays){ctx.save();ctx.globalAlpha=layer.opacity;ctx.drawImage(layer.image,layer.rect.x,layer.rect.y,layer.rect.width,layer.rect.height);ctx.restore();}ctx.restore();
45
92
  }
46
93
  function displayedPixels(canvas:HTMLCanvasElement,pane:HTMLElement,snap=true):PixelLayer|null {
@@ -52,23 +99,78 @@ function displayedRig(ctx:CanvasRenderingContext2D,capture:ViewportCapture,frame
52
99
  const root=capture.root;if(!root)return false;const children=[...root.children].filter((n):n is HTMLImageElement|HTMLCanvasElement=>n instanceof HTMLImageElement||n instanceof HTMLCanvasElement).filter(n=>getComputedStyle(n).display!=='none');
53
100
  const byZ=new Map<number,HTMLImageElement|HTMLCanvasElement>();for(const node of children){const style=getComputedStyle(node),z=Number(style.zIndex);if(style.filter!=='none'||style.transformOrigin!=='0px 0px'||!Number.isSafeInteger(z)||byZ.has(z))return false;byZ.set(z,node);}
54
101
  if(children.length!==frame.order.length||frame.order.some(row=>!byZ.has(row.z)))return false;
55
- const r=root.getBoundingClientRect(),p=capture.pane.getBoundingClientRect(),rootMatrix=new DOMMatrix(getComputedStyle(root).transform);if(!rootMatrix.is2D||rootMatrix.b!==0||rootMatrix.c!==0||rootMatrix.a<=0||rootMatrix.d<=0||getComputedStyle(root).transformOrigin!=='0px 0px')return false;
56
- const plans=frame.order.map(row=>{const node=byZ.get(row.z)!,style=getComputedStyle(node);return {row,node,style,m:new DOMMatrix(style.transform),width:parseFloat(style.width),height:parseFloat(style.height)};});if(plans.some(p=>!p.m.is2D||![p.width,p.height].every(n=>Number.isFinite(n)&&n>0)))return false;
102
+ const r=root.getBoundingClientRect(),p=capture.pane.getBoundingClientRect(),rootMatrix=cssMatrix(getComputedStyle(root).transform);if(!rootMatrix.is2D||rootMatrix.b!==0||rootMatrix.c!==0||rootMatrix.a<=0||rootMatrix.d<=0||getComputedStyle(root).transformOrigin!=='0px 0px')return false;
103
+ const plans=frame.order.map(row=>{const node=byZ.get(row.z)!,style=getComputedStyle(node);return {row,node,style,m:cssMatrix(style.transform),width:parseFloat(style.width),height:parseFloat(style.height)};});if(plans.some(p=>!p.m.is2D||![p.width,p.height].every(n=>Number.isFinite(n)&&n>0)))return false;
104
+ if(capture.strictActualPixels&&plans.some(plan=>plan.node instanceof HTMLCanvasElement&&
105
+ (plan.m.b!==0||plan.m.c!==0||plan.m.a<=0||plan.m.d<=0)))return false;
57
106
  for(const {row,node,style,m,width,height}of plans){
58
107
  ctx.save();if(row.clip){ctx.beginPath();if(row.clip.inverse)ctx.rect(0,0,camera.viewport.width,camera.viewport.height);for(let i=0;i<row.clip.polygon.length;i+=2){const at=projectMark({x:row.clip.polygon[i]!,y:row.clip.polygon[i+1]!},camera);if(i===0)ctx.moveTo(at.x,at.y);else ctx.lineTo(at.x,at.y);}ctx.closePath();ctx.clip(row.clip.inverse?'evenodd':'nonzero');}
59
108
  ctx.globalAlpha=Number(style.opacity);ctx.globalCompositeOperation=style.mixBlendMode==='plus-lighter'?'lighter':style.mixBlendMode==='normal'?'source-over':style.mixBlendMode as GlobalCompositeOperation;
60
- ctx.transform(rootMatrix.a*m.a,rootMatrix.d*m.b,rootMatrix.a*m.c,rootMatrix.d*m.d,r.left-p.left-capture.pane.clientLeft+rootMatrix.a*m.e,r.top-p.top-capture.pane.clientTop+rootMatrix.d*m.f);ctx.drawImage(node,0,0,width,height);ctx.restore();
109
+ if(capture.strictActualPixels&&node instanceof HTMLCanvasElement){
110
+ const rect=node.getBoundingClientRect();
111
+ const origin={x:p.left+capture.pane.clientLeft,y:p.top+capture.pane.clientTop};
112
+ const layer=getComputedStyle(root).transform==='none'&&style.transform==='none'
113
+ ?displayedCanvasRect({x:rect.left,y:rect.top,width:rect.width,height:rect.height},origin,globalThis.devicePixelRatio||1)
114
+ :{x:rect.left-origin.x,y:rect.top-origin.y,width:rect.width,height:rect.height};
115
+ ctx.drawImage(node,layer.x,layer.y,layer.width,layer.height);
116
+ }else{
117
+ ctx.transform(rootMatrix.a*m.a,rootMatrix.d*m.b,rootMatrix.a*m.c,rootMatrix.d*m.d,r.left-p.left-capture.pane.clientLeft+rootMatrix.a*m.e,r.top-p.top-capture.pane.clientTop+rootMatrix.d*m.f);
118
+ ctx.drawImage(node,0,0,width,height);
119
+ }
120
+ ctx.restore();
61
121
  }return true;
62
122
  }
63
123
  export function paintInspectionViewport(ctx:CanvasRenderingContext2D,area:MarkRegion,capture:ViewportCapture,pose:{camera:ControlNumericCamera;posed?:PosedFrame;assets?:ModelAssets;media?:HTMLVideoElement|HTMLImageElement}):void {
64
124
  if(capture.nearest)ctx.imageSmoothingEnabled=false;
65
- const overlays=[...capture.pane.querySelectorAll<HTMLCanvasElement>('canvas')].filter(node=>!capture.root?.contains(node)&&node!==capture.canvas).map(node=>displayedPixels(node,capture.pane)).filter((v):v is PixelLayer=>v!==null);
66
- paintViewportLayers(ctx,area,viewportBackground(capture.pane),capture.images,()=>{
67
- if(capture.canvas){const pixels=displayedPixels(capture.canvas,capture.pane,!capture.root?.contains(capture.canvas));if(pixels){ctx.save();ctx.globalAlpha=pixels.opacity;ctx.drawImage(pixels.image,pixels.rect.x,pixels.rect.y,pixels.rect.width,pixels.rect.height);ctx.restore();return;}}
125
+ const overlays=capture.includeOverlays===false?[]:[...capture.pane.querySelectorAll<HTMLCanvasElement>('canvas')].filter(node=>!capture.root?.contains(node)&&node!==capture.canvas).map(node=>displayedPixels(node,capture.pane)).filter((v):v is PixelLayer=>v!==null);
126
+ paintViewportLayers(ctx,area,viewportBackground(capture.pane,capture.strictActualPixels),capture.images,()=>{
127
+ if(capture.canvas){if(capture.strictActualPixels&&getComputedStyle(capture.canvas).filter!=='none')throw new Error('JOB_STILL_CAPTURE_FAILED');const pixels=displayedPixels(capture.canvas,capture.pane,!capture.root?.contains(capture.canvas));if(pixels){ctx.save();ctx.globalAlpha=pixels.opacity;ctx.drawImage(pixels.image,pixels.rect.x,pixels.rect.y,pixels.rect.width,pixels.rect.height);ctx.restore();return;}}
68
128
  if(pose.posed&&pose.assets){if(displayedRig(ctx,capture,pose.posed,pose.camera))return;
129
+ if(capture.strictActualPixels)throw new Error('JOB_STILL_CAPTURE_FAILED');
69
130
  if(capture.backend==='canvas2d'&&pose.posed.order.some(row=>row.tint.slice(0,3).some(c=>c!==1)||row.dark?.some(c=>c!==0)))throw new Error('COMMENT_CAPTURE_UNAVAILABLE: Canvas2D cannot reproduce active RGB/dark tint');
70
131
  const root=document.createElement('div'),renderer=new CanvasRenderer(root,pose.assets.pageImages);try{renderer.backend=capture.backend;renderer.viewport={...pose.camera.visible};renderer.pixelRatio=pose.camera.viewport.width/pose.camera.visible.width;renderer.render(pose.posed);ctx.drawImage(renderer.canvas,0,0,pose.camera.viewport.width,pose.camera.viewport.height);}finally{renderer.dispose();}
71
- }else if(pose.media){const media=pose.media,style=getComputedStyle(media),a=media.getBoundingClientRect(),b=capture.pane.getBoundingClientRect();if(!transparent(style.backgroundColor)){ctx.fillStyle=style.backgroundColor;ctx.fillRect(a.left-b.left-capture.pane.clientLeft,a.top-b.top-capture.pane.clientTop,a.width,a.height);}const w=media instanceof HTMLVideoElement?media.videoWidth:media.naturalWidth,h=media instanceof HTMLVideoElement?media.videoHeight:media.naturalHeight,first=projectMark({x:0,y:0},pose.camera),last=projectMark({x:w,y:h},pose.camera);ctx.drawImage(media,first.x,first.y,last.x-first.x,last.y-first.y);
132
+ }else if(capture.strictActualPixels){throw new Error('JOB_STILL_CAPTURE_FAILED');
133
+ }else if(pose.media){const media=pose.media,style=getComputedStyle(media),a=media.getBoundingClientRect(),b=capture.pane.getBoundingClientRect();if(!transparent(style.backgroundColor)){ctx.fillStyle=style.backgroundColor;ctx.fillRect(a.left-b.left-capture.pane.clientLeft,a.top-b.top-capture.pane.clientTop,a.width,a.height);}const w=media instanceof HTMLVideoElement?media.videoWidth:media.naturalWidth,h=media instanceof HTMLVideoElement?media.videoHeight:media.naturalHeight,first=projectMark({x:0,y:0},pose.camera),last=projectMark({x:w,y:h},pose.camera);ctx.drawImage(media,first.x,first.y,last.x-first.x,last.y-first.y);
72
134
  }else throw new Error('COMMENT_CROP_UNAVAILABLE: this viewport has no rendered pixels');
73
- },overlays);
135
+ },overlays,capture.strictActualPixels);
136
+ }
137
+
138
+
139
+ /** The committed normal Rig presentation, including every admitted background pixel. */
140
+ export interface JobRigPresentation {
141
+ readonly canvas: HTMLCanvasElement;
142
+ readonly width: number;
143
+ readonly height: number;
144
+ readonly ratio: number;
145
+ readonly area: Readonly<CaptureRect>;
146
+ }
147
+
148
+ /** Compose during normal presentation. The caller mounts only a completed bitmap. */
149
+ export function presentJobRigViewport(capture: ViewportCapture,
150
+ pose: {camera: ControlNumericCamera; posed?: PosedFrame; assets?: ModelAssets}): JobRigPresentation {
151
+ const {pane} = capture;
152
+ const rect = pane.getBoundingClientRect();
153
+ const ratio = globalThis.devicePixelRatio || 1;
154
+ const area = pixelAlignedCrop({x: 0, y: 0, width: pane.clientWidth, height: pane.clientHeight},
155
+ {x: rect.left + pane.clientLeft, y: rect.top + pane.clientTop}, ratio);
156
+ const width = Math.round(area.width * ratio);
157
+ const height = Math.round(area.height * ratio);
158
+ if (width < 1 || height < 1 || width > 16384 || height > 16384 || width * height > 16777216) {
159
+ throw new Error('JOB_STILL_CAPTURE_FAILED');
160
+ }
161
+ const canvas = document.createElement('canvas');
162
+ canvas.width = width;
163
+ canvas.height = height;
164
+ canvas.className = 'job-rig-presentation';
165
+ canvas.dataset.jobRigPresentation = '';
166
+ canvas.style.left = `${area.x}px`;
167
+ canvas.style.top = `${area.y}px`;
168
+ canvas.style.width = `${width / ratio}px`;
169
+ canvas.style.height = `${height / ratio}px`;
170
+ const context = canvas.getContext('2d');
171
+ if (!context) throw new Error('JOB_STILL_CAPTURE_FAILED');
172
+ context.scale(ratio, ratio);
173
+ context.translate(-area.x, -area.y);
174
+ paintInspectionViewport(context, area, {...capture, strictActualPixels: true, includeOverlays: false}, pose);
175
+ return Object.freeze({canvas, width, height, ratio, area: Object.freeze(area)});
74
176
  }