Skip to content

fix: forward type and quality options from toBlob to canvasToBlob - #590

Open
Kebechet wants to merge 2 commits into
bubkoo:masterfrom
Kebechet:fix/to-blob-type-quality
Open

fix: forward type and quality options from toBlob to canvasToBlob#590
Kebechet wants to merge 2 commits into
bubkoo:masterfrom
Kebechet:fix/to-blob-type-quality

Conversation

@Kebechet

@Kebechet Kebechet commented Aug 6, 2026

Copy link
Copy Markdown

Problem

toBlob forwards its options to toCanvas, but then calls canvasToBlob without them:

const canvas = await toCanvas(node, options)
const blob = await canvasToBlob(canvas)

canvasToBlob accepts type and quality and defaults them to image/png / 1, so toBlob(node, { type: 'image/jpeg', quality: 0.5 }) silently returns a PNG at quality 1 — the requested type and quality are dropped on the floor.

Fix

Pass options through to canvasToBlob. One line; canvasToBlob already implements the handling.

Tests

test/spec/to-blob.spec.ts:

  • default capture still produces image/png;
  • type: 'image/jpeg' produces a blob with type === 'image/jpeg' — on current master this fails with image/png;
  • a JPEG at quality: 0.01 is smaller than the same capture at quality: 1 — on current master both blobs are byte-identical PNGs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new tests likely violate existing spec-file lint conventions and include a null-dereference hazard, and the change exposes an existing quality: 0 handling bug that should be addressed for correctness.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR fixes toBlob so it correctly forwards type/quality export options through to canvasToBlob, aligning blob output with the options already respected by toCanvas and canvasToBlob.

Changes:

  • Pass options from toBlob into canvasToBlob so requested type/quality are honored.
  • Add new toBlob specs verifying default image/png, honoring type, and honoring quality (via size comparison).
File summaries
File Description
src/index.ts Forwards options into canvasToBlob so blob encoding honors type/quality.
test/spec/to-blob.spec.ts Adds regression coverage for toBlob defaults and option forwarding.
Review details

Suppressed comments (1)

test/spec/to-blob.spec.ts:40

  • This test dereferences lowQualityBlob/highQualityBlob with ! without asserting they are non-null first; if toBlob returns null (e.g., unsupported type), the test will throw a TypeError instead of failing with a clear expectation message.
      .then(([lowQualityBlob, highQualityBlob]) => {
        expect(lowQualityBlob!.size).toBeLessThan(highQualityBlob!.size)
        done()
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread test/spec/to-blob.spec.ts Outdated
Comment thread src/index.ts
Kebechet added a commit to Kebechet/Blazor.HtmlToImage that referenced this pull request Aug 6, 2026
Measured on a real iPhone: WebKit rasterizes the capture's intermediate SVG
before large embedded images have decoded, blanking them in the output, and a
plain retry or pre-decoding does not recover it - only comparing consecutive
captures until they agree does. Captures now repeat until two consecutive
results are identical (StabilizeAttempts, default 3), and every capture is
bounded by CaptureTimeoutMs because upstream's createImage never settles when
Safari rejects HTMLImageElement.decode() under memory pressure.

Both mitigations are proposed upstream:
bubkoo/html-to-image#589 (hang)
bubkoo/html-to-image#591 (stabilization)
bubkoo/html-to-image#590 (the toBlob type/quality
deviation this wrapper already works around)
Post-merge cleanup tracked in
#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants