@rig-c/review 2.4.6 → 2.6.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.
@@ -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
  }