Documentation Index
Fetch the complete documentation index at: https://mintlify.com/p2r3/convert/llms.txt
Use this file to discover all available pages before exploring further.
Overview
ThecanvasToBlobHandler provides image format conversion using the browser’s native Canvas API. It’s particularly useful for lightweight conversions and supports text-to-image rendering.
Supported Formats
Unlike other handlers, canvasToBlobHandler uses a fixed list of formats:Portable Network Graphics
- Bidirectional (read/write)
- Lossless
- Supports transparency
Joint Photographic Experts Group
- Bidirectional (read/write)
- Lossy compression
Web Picture format
- Bidirectional (read/write)
- Modern web format
Graphics Interchange Format
- Read-only (input only)
- Animated GIF support for reading
Scalable Vector Graphics
- Read-only (input only)
- Rasterized when converted to other formats
Plain text
- Bidirectional (read/write)
- Text-to-image and image-to-ASCII
Initialization
The handler creates an off-screen canvas element:Initialization Process
Conversion Process
Image to Image Conversion
For image-to-image conversions:- Create Blob from input bytes
- Generate object URL (or data URL for SVG)
- Load into Image element
- Draw to canvas
- Export via
canvas.toBlob()
Text to Image Conversion
When converting from plain text to images, the handler renders text on the canvas:Text Rendering Details
Font specification for text rendering
Extracted from font string (e.g.,
parseInt("48px sans-serif"))Bottom padding calculated as
fontSize * 0.5Text color (black text on white background)
Text outline color for better readability
Image to Text Conversion
When converting images to text, the handler uses ASCII art conversion:imageToText function from ./image-to-txt/src/convert.ts to generate ASCII art.
SVG Handling
SVG images require special handling to avoid tainted canvas errors:- Object URLs from
Blobcan cause CORS issues with SVG - Data URLs avoid “Tainted canvases may not be exported” errors
- Base64 encoding ensures SVG content is properly embedded
Canvas Sizing
For Images
Canvas is sized to match the natural dimensions of the input image:For Text
Canvas is sized based on text content:Output File Naming
Output files replace the input extension:photo.png → photo.jpeg
Error Handling
Uninitialized Handler
Image Loading Errors
Canvas Export Errors
Properties
Handler identifier
Fixed array of 6 supported formats (PNG, JPEG, WebP, GIF, SVG, TEXT)
true when canvas is initialized and handler is ready for conversionsUse Cases
Ideal for:- Simple image format conversions (PNG ↔ JPEG ↔ WebP)
- Text-to-image generation
- Image-to-ASCII art conversion
- SVG rasterization
- Lightweight conversions without heavy WASM dependencies
- Advanced image processing
- GIF or WebP animation (only reads first frame)
- Format-specific optimizations
- Batch processing of large files
Performance Considerations
- Lightweight - no WASM dependencies
- Fast initialization (just creates canvas element)
- Processes files individually
- Limited by browser Canvas API capabilities
- No native support for animated formats (GIF, APNG, WebP)
Limitations
- GIF Output: Cannot write GIF files (read-only)
- SVG Output: Cannot write SVG files (read-only)
- Animation: Only reads first frame of animated formats
- Quality: Limited to browser’s Canvas implementation
- Format Support: Only formats supported by Canvas API
Browser Compatibility
The handler requires:- Canvas API support
canvas.toBlob()methodCanvasRenderingContext2D.measureText()for text rendering- Image loading via Image element
Source Reference
Implementation:~/workspace/source/src/handlers/canvasToBlob.ts
Dependencies:
image-to-txt/src/convert.ts- ASCII art conversion