Skip to content

fix(chrome): clip diagram snapshots against the resized layout - #57

Merged
rapatao merged 1 commit into
mainfrom
fix/diagram-snapshot-clip-alignment
Aug 13, 2026
Merged

fix(chrome): clip diagram snapshots against the resized layout#57
rapatao merged 1 commit into
mainfrom
fix/diagram-snapshot-clip-alignment

Conversation

@rapatao

@rapatao rapatao commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Follow-up to #56: large diagrams could still come out with the bottom cut, and with part of the surrounding page text baked into the top of the image.

Two causes

Clip measured against a stale layout. Emulation.setDeviceMetricsOverride is applied asynchronously, so measuring the diagram straight after resizing the viewport could read the pre-resize layout. Text above the diagram re-wraps when the viewport width changes, moving the diagram up or down; the clip then landed off it, pulling in surrounding page text at one edge and losing part of the diagram at the other. measureAfterResize now waits for window.innerWidth to report the new width before measuring.

Capture surface limit. Anything past the 4096 CSS px viewport cap relied on CaptureBeyondViewport, and past roughly 16384 px the browser stops growing its capture surface and returns an image cut off at the right and bottom. The viewport cap goes to 8192 CSS px, and captureScale lowers the device scale when 2x would exceed the surface limit, so an outsized diagram is captured whole (softer) rather than chopped.

Checks

case result
200-node tall flowchart 83x15366, last node (Node 200) present — was 194x35710, past the surface limit
40-node wide flowchart 13947x81, Node 40 present at the right edge
text + 6x11 grid, html -flatten and docx first node row at the top, last at the bottom, no page text bled in
testdata/complex.md (mermaid, d2, plantuml) to docx/epub/pdf unchanged

New TestCaptureScale; go test ./... green.

Two ways a diagram snapshot could still come out cut:

Emulation.setDeviceMetricsOverride is applied asynchronously, so measuring
the diagram right after resizing the viewport could read the pre-resize
layout. Text above the diagram re-wraps when the viewport width changes,
moving the diagram up or down, and the clip then landed off it: surrounding
page text pulled in at one edge, part of the diagram lost at the other.
Wait for window.innerWidth to report the new width before measuring.

A diagram past the viewport cap relied on CaptureBeyondViewport, and past
roughly 16384 px the browser stops growing its capture surface and returns
an image cut off at the right and bottom. Raise the viewport cap to 8192
CSS px and lower the device scale when 2x would exceed that surface limit,
so an outsized diagram is captured whole (softer) instead of chopped.
@rapatao
rapatao merged commit 70f40ed into main Aug 13, 2026
4 checks passed
@rapatao
rapatao deleted the fix/diagram-snapshot-clip-alignment branch August 13, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant