Notes › Tech Notes › The PNG Quality Slider Spins for Nothing at Every Byte Size

The PNG Quality Slider Spins for Nothing at Every Byte Size

Measured in Chrome on one 213 B SVG: at 320×240 the PNG output stays 6737 B for q10, q50, q90 and q100, and toDataURL still receives 0.9; at 800×600 q90 the three exits return 22997, 11811 and 10006 B, while a -5×-4 size falls back to 300×150 yet the toast reports success.

Drag the Quality Slider to the End: the File Does Not Move by a Single Byte

Switch to the PNG tab and the quality slider is sitting right there in front of you, begging to be moved. Take the same self-made 213 B sample, whose header reads <svg width="320" height="240"> plus one 50% red rectangle and one 70% blue circle, with zero external dependencies, and convert it to PNG at 320×240: the artifact is 6737 B at q10, at q50, at q90 and at q100 alike, magic bytes 89 50 4e 47 at the front. The call chain never breaks along the way either — the vm trace shows the PNG branch still handing 0.9 to toDataURL, because parseInt is divided by 100 and the result is passed straight through, and it is only Chrome's PNG encoder that refuses to look at it. Hiding the quality section is a UI decision; the parameter keeps travelling the whole way.

The Initial State Lies: PNG Is Already Active, Yet the Quality Section Is Still Showing

On first paint the PNG button already carries active, but #quality-section has no inline style and its computed display reads back as block — do not click a single tab and the quality section stays fully exposed under the PNG format. The hide written at L175 only takes effect once a click actually happens: click JPG and it goes block, click back to PNG and it goes none, click WebP and it is block again, click PNG again and it is none again, click JPG once more and it is block once more. Five clicks, no accumulation at all, and whether the section shows is decided only by the last setFormat call.

Slider clamp ledger: min 10, max 100, default 90 10 100 90 5 → 10 150 → 100 89.4 → 89 99.6 → 100 abc → 55 Garbage falls back to midpoint 55, decimals snap to step

Figure 1: read-back values of a real range element — endpoint clamping and the midpoint fallback both happen inside the browser rather than in the page script.

The Slider Is a Native range: Out of Range Snaps to an Endpoint, Garbage Snaps to the Midpoint 55

Assign a value to the real slider and read it back: 5 turns into 10, 150 turns into 100, 111 turns into 100, 0 turns into 10 — anything out of range is clamped to the nearest endpoint; 99.6 turns into 100 and 89.4 turns into 89, snapped to the closest value with step=1; abc and the empty string both land on 55. The label has a trap of its own: after a scripted assignment #quality-val still shows 90%, and only once an input event is dispatched does it switch to reading 10% — the synchronisation hangs on oninput and nowhere else.

JPG Lays Down White, WebP Keeps Its Transparency All the Way Out

At L219 the jpeg branch hard-codes fillStyle to #ffffff and paints white underneath first, while the png and webp branches are handed transparent — the same thing as alpha=0, nothing is drawn there at all. Corner probe on the translucent sample: PNG and WebP stay at 255,0,0,128 at every quality step (0.5×255 lands on 128 and the alpha survives), whereas the JPG corner reads 255,127,126,255 and at q10 it drifts to 255,130,131. The centre pixel comes back as 45,0,210,217 for PNG and WebP but 76,39,217,255 for JPG. The preview side carries yet another hard-coded #fff background that has nothing to do with whichever format you export.

The Byte Ledger at Three Format Exits

TierPNGJPGWebP
320×240 q106737 B1667 B2862 B
320×240 q506737 B2462 B3200 B
320×240 q906737 B4358 B4016 B
320×240 q1006737 B11827 B4284 B
800×600 q9022997 B11811 B10006 B

One and the same sample: JPG swells 7.1 times from q10 to q100 while WebP only climbs 1.5 times, and at q90 PNG actually ends up larger than JPG. Growing 320×240 to 800×600 multiplies the pixel count by 6.25 but the byte counts by only 3.41, 2.71 and 2.49. The dataURL layer adds a further base64 tax on top: 6737 B encode into 8984 characters, a ratio of about 1.3335, and the prefix data:image/png;base64, occupies 22 characters of plaintext before any image byte appears; the download itself runs through a dataURL anchor, so the file on disk decodes back to the original bytes.

Three exits at 800×600 q90, same 213 B sample PNG 22997 B JPG 11811 B WebP 10006 B PNG stays 6737 B; JPG q10 is smallest

Figure 2: volume of the three formats at the same tier, bar lengths drawn proportional to bytes.

Two Rounding Rules Competing Over One Size Field

The aspect-ratio lock reverse-calculates with Math.round: type 777 into w and h gets written as 583; type 606 into h and w gets written as 808. Export, however, uses parseInt: the w box still holds 333.7, the linked field writes h=250, the value actually taken is 333, the canvas ends up 333×250, and the aspect ratio moves from 1.3333 to 1.332 — one decimal split apart by two rounding rules. Clearing w writes h as 0, and because 0 and the empty string are equally falsy both of them retreat to the original 320×240; uncheck the lock and tick it back on, 500.9 reverse-computes h=375 while the w box keeps displaying 500.9 — a single re-check truncates 0.9 pixels.

Negative Sizes and Absurd Sizes: Swallowed in Silence, Reported as Success Anyway

The size box declares type=number with min=1 and max=10000, yet those numbers only decorate validity: -5 reads back untouched with rangeUnderflow=true, 99999 is accepted with rangeOverflow=true, abc is washed into the empty string — the code reads .value and never once consults validity. Fill in width -5 and height -4 and press convert: canvas ignores the assignment, the read-back is the factory default 300×150, the artifact is a fully transparent blank PNG 2118 characters long, and the toast spells out, character for character, "Converted to PNG (-5×-4) and the download has started". Fill in a width of 100000000000 and the artifact is left with nothing but data:, those 6 characters of empty payload, while the wording still claims success. The single-sided boundary sits between 65535 and 65536: 65535×1 still produces a legitimate PNG 1754 characters long, and from 65536×1 onwards there is no payload at all.

The Fallback Chain and the Wording, Character for Character

Original-size fallback chain: width=-100 height=-50 is accepted straight into the box and shown as -100 × -50 px; 50% and 60% are gnawed by parseFloat down to their numeric parts only; 240.6 normalises to 241; with all three attributes missing the page falls back to 800 × 600. Three front-loaded failure messages stand on record: upload an SVG file or paste the code, no valid SVG tag found, load the SVG first. Nothing at all is listening on the textarea, no input event and no paste event, so it does not render until you press "Load SVG" — the live preview is button-driven, and the intro line about "10% to 100% quality control" carries no causal relationship with the PNG branch either. WebP at q90, run 3 times back to back, returns output that is identical character for character, 5379 characters long; the file names are hard-coded as converted.png, converted.jpg and converted.webp.

Cross References

What this piece measures is the exit ledger of the SVG to image page; if you only want to look without converting, tweaking styles against the source, use SVG preview; the account between viewBox and the real rendered size was measured in another piece and is not repeated here.

Q: I turned the quality slider all the way up, so why does the PNG artifact not change at all?

The parameter keeps travelling and the chain never breaks — the vm trace shows the PNG branch's toDataURL still receiving 0.9, it is only Chrome's PNG encoder that ignores it. On the 320×240 sample all four steps from q10 to q100 return 6737 B.

Q: What is that blank PNG I downloaded back from a negative size tier?

-5×-4 is silently ignored by canvas, the read-back is the factory default 300×150, and the artifact is a 2118-character fully transparent image; the toast still reports success using the numbers you typed. Those two negatives inside the parentheses are the only clue you get.

Q: Which tier should I pick if I want the smallest file?

On this sample JPG q10 at 1667 B is the cheapest, but it lays white underneath and its alpha is always 255; if the transparency has to survive, take WebP q10 at 2862 B instead.

📎 Related articles

JSON to YAML Reorders Keys and Quotes Numbers: Output Side and 4 Indent Tiers 2026-11-29 Charset Pool and Exclusions on the Random Password Page, Measured 2026-11-29 270° Clamps to 180°, Not -90°: The Angle Clamp and Canvas Ledger 2026-11-29 Text Set Operations Measured: Duplicates and Order 2026-11-28 HTML Beautify Deletes the Blank Line Inside pre 2026-11-28