Skip to content

fix: prevent empty PNG export for large graphs - #95

Open
Mathew Benjamin (mathewtbenjamin) wants to merge 1 commit into
microsoft:mainfrom
mathewtbenjamin:fix/png-export-large-graphs
Open

Mathew Benjamin (mathewtbenjamin) wants to merge 1 commit into
microsoft:mainfrom
mathewtbenjamin:fix/png-export-large-graphs

Conversation

@mathewtbenjamin

Copy link
Copy Markdown

Summary

Addresses the PNG export produces an empty file bug (item 3) reported in #87.

handleDownload in src/components/OntologyGraph.tsx exported with a fixed cy.png({ scale: 2, full: true }). For large ontologies the rendered canvas exceeds the browser's maximum canvas size, and toDataURL() then silently returns the empty data URI (data:,) — which is exactly the 0-byte imported-ontology-graph.png the reporter saw. The catch { /* ignore */ } block hid the failure completely.

Changes

  • New src/lib/graphExport.ts:
    • computeExportScale(width, height) — clamps the export scale so the largest side of the canvas stays within a conservative 8192px cross-browser limit. Small/medium graphs keep the crisp 2× scale; large graphs degrade gracefully instead of failing.
    • isValidPngDataUri(value) — detects the empty-data-URI failure mode.
  • OntologyGraph.tsx: compute the scale from the graph's bounding box, validate the data URI before triggering the download (no more 0-byte files), and log actionable errors instead of swallowing them.
  • Tests: src/lib/graphExport.test.ts — 9 unit tests covering clamping, exact-limit, degenerate bounding boxes, and data-URI validation.

Validation

  • npx tsc --noEmit — clean
  • npx vitest run — 25 files, 403/403 tests pass (394 existing + 9 new)

Notes

  • The 8192px per-side cap is conservative; happy to tune it or add a user-facing toast for the failure path if preferred (kept out of scope to avoid touching App-level toast state).
  • Items 1 (overlay dismissal) and 2 (graph navigation) of Usability issues #87 are UX work and intentionally not addressed here.

Exporting the graph as PNG used a fixed scale of 2 with full:true. For
large ontologies the resulting canvas exceeds the browser's maximum
canvas size, and toDataURL() silently returns the empty data URI
('data:,'), producing a 0-byte .png file. The silent catch block hid
the failure entirely.

- Add computeExportScale: clamp the export scale so the largest side of
  the rendered canvas stays within a conservative 8192px cross-browser
  limit (small graphs keep the crisp 2x scale).
- Validate the data URI before downloading and log an actionable error
  instead of writing an empty file.
- Replace the silent catch with console.error.
- Unit tests for the new helpers (9 tests).

Addresses the PNG export bug (item 3) in microsoft#87
@mathewtbenjamin

Copy link
Copy Markdown
Author

Friendly nudge 🙂 — this PR has been open ~3 weeks, the CLA check is green, and it's currently mergeable with no conflicts. Is there anything I can do to help move it toward review (rebase, split, or extra context)? Happy to help. Thanks!

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