Transparent PNGs are still the default export from a lot of design tools. They look right on any background, they are lossless, and they are often much larger than they need to be on a website. WebP and AVIF can shrink that payload, and both can keep an alpha channel — but only if you convert them carefully, and only if you check the result.

You do not need to upload the file to a server to do this. Modern browsers can decode a PNG and encode WebP entirely in the tab. AVIF is a different story, and I will be honest about that below.

This article walks through the actual browser APIs, the transparency traps, and a real (small) file-size measurement that surprised me.

What you are converting

A PNG with transparency is two things at once: colour pixels and an alpha channel. When a converter "supports transparency," it has to keep that alpha channel in the output, not composite the image onto white and throw the alpha away.

That rules out JPEG as a drop-in replacement. JPEG has no alpha. WebP does, in both lossy and lossless modes, and every current major browser can display it. AVIF can also store alpha, but encoding support in the browser is much patchier than display support.

The other misconception is that WebP or AVIF will always be smaller. They often are for photographs. For small, simple, already-efficient PNGs — a flat icon, a few colours, a lot of solid transparency — a default WebP export can come out larger than the original. If you do not measure the encoded file, you can make the page heavier and call it an optimisation.

The in-browser path for WebP

The reliable approach is:

  1. Decode the PNG in the browser (createImageBitmap or Image).
  2. Draw it to a canvas at the size you actually want.
  3. Encode with convertToBlob / toBlob and the type image/webp.
  4. Read blob.size so you can compare it with the original file.
  5. Offer a download only after you have looked at the pixels and the size.

Here is a compact version using OffscreenCanvas, which is available in modern browsers and keeps this work off the page canvas:

async function pngFileToWebp(file, { quality = 0.8, maxWidth = Infinity } = {}) { const bitmap = await createImageBitmap(file); const scale = Math.min(1, maxWidth / bitmap.width); const width = Math.max(1, Math.round(bitmap.width * scale)); const height = Math.max(1, Math.round(bitmap.height * scale)); const canvas = new OffscreenCanvas(width, height); const ctx = canvas.getContext("2d"); ctx.drawImage(bitmap, 0, 0, width, height); bitmap.close(); const blob = await canvas.convertToBlob({ type: "image/webp", quality, // ignored for a lossless-only request; used for lossy WebP }); return blob; } 

If you are in a page script and OffscreenCanvas is missing, the same idea works with a regular <canvas> and canvas.toBlob((blob) => …, "image/webp", quality).

Before you rely on this, check that the browser can actually encode WebP. A common feature test is:

function canEncodeWebp() { const canvas = document.createElement("canvas"); canvas.width = canvas.height = 1; return canvas.toDataURL("image/webp").startsWith("data:image/webp"); } 

createImageBitmap(file) will decode PNG (and JPEG, and usually WebP) from a File or Blob. It is a better default than inventing an <img> plus object URL, and you should call bitmap.close() when you are done so the decoded pixels can be released.

Keep the alpha channel, and then look at it

Drawing a transparent PNG onto a canvas preserves alpha as long as you do not first fill the canvas with an opaque colour. drawImage onto a freshly created canvas is the safe path. If you fill a white background "so it looks clearer" and then export WebP, you have destroyed the transparency.

After encoding, preview the result on more than one backdrop: a light background, a dark background, and a checkerboard. Semi-transparent edges (soft shadows, hair, glass, anti-aliased outlines) are where converters go wrong. A checkerboard makes baked-in "fake transparency" obvious — for example a chequered pattern that was flattened into the colour channels of the original file.

Two other details worth knowing:

  • The WebP quality argument applies to lossy encoding. If you want a lossless WebP, you need an encoder that exposes a lossless mode. The basic canvas API is lossy-quality-oriented; it is not a full cwebp replacement.
  • "Lossless" refers to decoded pixels, not to every PNG metadata field (text chunks, colour profiles, and so on). Do not assume a round-trip will preserve ancillary PNG data.

Measure the real file, not a quality slider

blob.size is the number that matters. Do not trust a quality label. Write a short comparison:

const original = file.size; const webp = await pngFileToWebp(file, { quality: 0.8 }); console.table({ originalBytes: original, webpBytes: webp.size, changePct: Math.round(((webp.size - original) / original) * 100), }); 

On 9 November 2026 I ran a simple check with one small synthetic transparent PNG, 640×480, 3,170 bytes. The default WebP export at the same dimensions was 4,460 bytes — about 41% larger, not smaller. A second WebP, resized to 320×240, was 2,602 bytes (about 18% smaller than the original PNG) but it was also a lower-resolution image, so that is not a fair same-size comparison. Both WebP files still had an alpha channel.

That was one sample, not a benchmark. The point is the habit: look at the encoded size and the pixels before you replace the PNG.

As a rough rule of thumb, not a guarantee:

  • Photographs and busy illustrations often shrink a lot as lossy WebP or AVIF, even with alpha.
  • Small icons, logos, and flat graphics with few colours are often excellent as PNG (or as SVG, if they are truly vector). A default WebP can lose on size.
  • If you can resize the image to the size you will actually display, do that first. A smaller PNG or WebP at the right dimensions usually beats a full-resolution conversion.

AVIF is harder in the browser

Display support for AVIF is now wide. Encoding support inside canvas.convertToBlob({ type: "image/avif" }) is not. In many browsers that call will fail or fall back in a way you must not treat as success. Never rename a PNG to .avif.

A safe pattern is:

async function tryEncodeAvif(canvas) { try { const blob = await canvas.convertToBlob({ type: "image/avif", quality: 0.6 }); if (!blob || blob.size === 0 || blob.type !== "image/avif") { return null; } return blob; } catch (err) { return null; } } 

If this returns null, keep the PNG, offer WebP instead, or use a dedicated encoder. Some tools fall back to a protected server-side encoder when the browser cannot produce AVIF. That is a reasonable fallback, but it is a different privacy model from a purely in-tab conversion, and it should be an explicit choice.

AVIF can also preserve transparency, but only if the encoder you actually used wrote an alpha channel. Check the output the same way you would for WebP: preview it on light, dark and checkerboard, and confirm the file type and size.

Putting it together in a small UI

A useful converter is not a single button. At minimum, let someone:

  • Drop in a PNG (and, ideally, reject files that are not images before you decode them).
  • Choose dimensions, or a max edge, so they can export the size they will use.
  • Choose a quality or a lossless option for WebP.
  • See a preview of transparency and the measured output size next to the original.
  • Download only the file they have inspected.

If you are building this yourself, keep large decodes off the main thread where you can (OffscreenCanvas in a worker). Honor a file-size and megapixel cap so a huge poster PNG cannot freeze the tab. And do not silently replace a 3 KB PNG with a 4 KB WebP.

A ready-to-use converter

If you want this behaviour without standing up the UI yourself, I use PNGPond's PNG to WebP tool, which runs in the browser, keeps transparency in the WebP export, and shows the actual encoded file size before you download. It also has an AVIF option; that path can use a protected server encoder when the browser cannot encode AVIF locally, which is the honest limitation described above.

Whether you build the converter or use one, the checklist is the same: preserve alpha, preview it, measure the bytes, and keep the PNG when it is already the better file.

Further reading