Check Canvas Blob Format Before Naming the Download
Force a fallback with an unsupported MIME type, then label the download from the returned Blob.


A download named sprite.webp can still contain PNG bytes. Canvas accepts a requested MIME type and falls back to PNG when that type is unsupported. Catch the fallback before passing the asset to your importer.
Run three exports from one canvas
- Draw a solid mark on a nonempty 32 by 32 canvas using local content. Call toBlob with image/png and record the returned type.
- Repeat with the deliberately unsupported image/x-tooliva-fixture. Expect image/png. Then request the intended delivery format, such as image/webp, and inspect that result.
- Save a copy and identify the format with a decoder. A PNG starts with 89 50 4E 47 0D 0A 1A 0A. A WebP container has RIFF at the start and WEBP at byte offset eight. RIFF alone cannot identify WebP.
Handle the result before naming the download
Map the returned Blob.type to an extension. If PNG is acceptable, name the file accordingly and display the actual format. If the receiving system requires WebP, show a clear export error when the browser falls back. For a null blob or a thrown error, check canvas dimensions and cross-origin image restrictions before testing the encoder again.
Prepare a representative asset with the asset compression tool, then exercise your own export and import pair. Log the requested type and the actual result, and confirm the delivered file opens in the receiving tool.
Ready to try "Check Canvas Blob Format Before Naming the Download"?
Use our online tools to apply this tutorial to your indie game project right away.
