How to save HTML5 canvas data to file?

HTML5 canvas provides several methods to save canvas data to files. The approach depends on whether you're working in a browser or Node.js environment.

Method 1: Browser Environment - Download as File

In browsers, use toDataURL() or toBlob() to convert canvas data and trigger a download:

<!DOCTYPE html>
<html>
<head>
    <title>Canvas Save Example</title>
</head>
<body>
    <canvas id="myCanvas" width="200" height="100"></canvas>
    <button onclick="saveCanvas()">Save Canvas</button>

    <script>
        // Draw something on canvas
        const canvas = document.getElementById('myCanvas');
        const ctx = canvas.getContext('2d');
        ctx.fillStyle = '#FF0000';
        ctx.fillRect(10, 10, 150, 80);
        ctx.fillStyle = '#FFFFFF';
        ctx.font = '16px Arial';
        ctx.fillText('Hello Canvas!', 20, 40);

        function saveCanvas() {
            const link = document.createElement('a');
            link.download = 'canvas-image.png';
            link.href = canvas.toDataURL('image/png');
            link.click();
        }
    </script>
</body>
</html>

Method 2: Using Blob API (Browser)

The toBlob() method is more efficient for larger images:

<!DOCTYPE html>
<html>
<head>
    <title>Canvas Blob Save</title>
</head>
<body>
    <canvas id="canvas" width="300" height="150"></canvas>
    <button onclick="saveBlobCanvas()">Save as Blob</button>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        
        // Draw a circle
        ctx.beginPath();
        ctx.arc(150, 75, 50, 0, 2 * Math.PI);
        ctx.fillStyle = '#00FF00';
        ctx.fill();

        function saveBlobCanvas() {
            canvas.toBlob(function(blob) {
                const link = document.createElement('a');
                link.download = 'canvas-blob.png';
                link.href = URL.createObjectURL(blob);
                link.click();
                URL.revokeObjectURL(link.href); // Clean up
            }, 'image/png');
        }
    </script>
</body>
</html>

Method 3: Node.js Environment with Canvas Library

In Node.js, use the canvas library to save canvas data to the file system:

// First install: npm install canvas
const fs = require('fs');
const { createCanvas } = require('canvas');

// Create canvas and draw
const canvas = createCanvas(200, 100);
const ctx = canvas.getContext('2d');

ctx.fillStyle = '#0000FF';
ctx.fillRect(0, 0, 200, 100);
ctx.fillStyle = '#FFFFFF';
ctx.font = '20px Arial';
ctx.fillText('Node Canvas', 10, 50);

// Save as PNG using streams
const out = fs.createWriteStream(__dirname + '/canvas-output.png');
const stream = canvas.createPNGStream();

stream.on('data', function(chunk) {
    out.write(chunk);
});

stream.on('end', function() {
    console.log('PNG saved successfully!');
});

Method 4: Node.js Buffer Approach

Alternatively, convert canvas to buffer and write directly:

const fs = require('fs');
const { createCanvas } = require('canvas');

const canvas = createCanvas(200, 100);
const ctx = canvas.getContext('2d');

// Draw something
ctx.fillStyle = '#FF00FF';
ctx.fillRect(20, 20, 160, 60);

// Save using buffer
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync('./canvas-buffer.png', buffer);
console.log('Canvas saved using buffer method!');

Comparison of Methods

Method Environment File Size Performance
toDataURL() Browser Larger (Base64) Good for small images
toBlob() Browser Smaller (Binary) Better for large images
PNG Stream Node.js Small Memory efficient
Buffer Write Node.js Small Simple and fast

Conclusion

For browser applications, use toBlob() for better performance. In Node.js environments, both stream and buffer methods work well, with streams being more memory-efficient for large canvases.

Updated on: 2026-03-15T23:18:59+05:30

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements