dom-to-image: Convert DOM Nodes to Images with JavaScript and HTML5 Canvas

This repository profile is provided by osrepos.com, an open source repository discovery platform.

dom-to-image: Convert DOM Nodes to Images with JavaScript and HTML5 Canvas

Summary

dom-to-image is a JavaScript library designed to transform any DOM node into a vector (SVG) or raster (PNG, JPEG) image. It leverages HTML5 canvas to provide a flexible solution for capturing web content. This tool is ideal for developers needing to generate visual representations of specific UI elements.

Repository Information

Analyzed by OSRepos on October 12, 2025

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

Introduction

dom-to-image is a powerful JavaScript library that enables developers to convert any DOM node into various image formats, including vector (SVG) and raster (PNG, JPEG) images. Building upon the foundational work of domvas by Paul Bakaus, this library has been completely rewritten to offer enhanced features like web font and image support, alongside significant bug fixes. It provides a robust solution for capturing and rendering dynamic web content as static images.

Installation

You can easily integrate dom-to-image into your project using either NPM or Bower.

NPM

npm install dom-to-image

Then, you can import it into your JavaScript files:

/* in ES 6 */
import domtoimage from 'dom-to-image';
/* in ES 5 */
var domtoimage = require('dom-to-image');

Bower

bower install dom-to-image

After installation, include either src/dom-to-image.js or dist/dom-to-image.min.js in your HTML page. This will make the domtoimage variable available globally.

<script src="path/to/dom-to-image.min.js"></script>
<script>
  domtoimage.toPng(node)
  //...
</script>

Examples

dom-to-image offers several methods to convert DOM nodes into different image formats, all returning promises that resolve with the corresponding data URLs or blobs.

Convert to PNG and Display

var node = document.getElementById('my-node');

domtoimage.toPng(node)
    .then(function (dataUrl) {
        var img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
    })
    .catch(function (error) {
        console.error('oops, something went wrong!', error);
    });

Convert to Blob and Download

You can also get a PNG image as a blob and download it, for example, using FileSaver.

domtoimage.toBlob(document.getElementById('my-node'))
    .then(function (blob) {
        window.saveAs(blob, 'my-node.png');
    });

Save as Compressed JPEG

domtoimage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })
    .then(function (dataUrl) {
        var link = document.createElement('a');
        link.download = 'my-image-name.jpeg';
        link.href = dataUrl;
        link.click();
    });

Convert to SVG with a Filter

Filter out specific elements, like <i> tags, when converting to SVG.

function filter (node) {
    return (node.tagName !== 'i');
}

domtoimage.toSvg(document.getElementById('my-node'), {filter: filter})
    .then(function (dataUrl) {
        /* do something */
    });

Get Raw Pixel Data

Retrieve the raw pixel data as a Uint8Array for advanced image manipulation.

var node = document.getElementById('my-node');

domtoimage.toPixelData(node)
    .then(function (pixels) {
        for (var y = 0; y < node.scrollHeight; ++y) {
          for (var x = 0; x < node.scrollWidth; ++x) {
            pixelAtXYOffset = (4 * y * node.scrollHeight) + (4 * x);
            /* pixelAtXY is a Uint8Array[4] containing RGBA values of the pixel at (x, y) in the range 0..255 */
            pixelAtXY = pixels.slice(pixelAtXYOffset, pixelAtXYOffset + 4);
          }
        }
    });

Why Use

dom-to-image stands out as an essential tool for web developers due to its versatility and robust feature set. It simplifies the complex task of rendering dynamic DOM content into static image formats, supporting both high-fidelity SVG vector images and widely used raster formats like PNG and JPEG. The library's promise-based API ensures asynchronous operations are handled gracefully, making integration into modern JavaScript applications seamless. With options for filtering elements, setting background colors, adjusting dimensions, and controlling JPEG quality, developers have fine-grained control over the output. This makes dom-to-image invaluable for tasks such as generating custom screenshots of UI components, creating shareable images of dynamic charts or graphs, or even for automated visual regression testing.

Links

For more detailed information, contributions, or to report issues, please visit the official repository.

  • GitHub Repository: tsayen/dom-to-image
  • Authors: Anatolii Saienko, Paul Bakaus (original idea)
  • License: MIT

Related repositories

Similar repositories that may be relevant next.

Agent-Memory: Persistent Memory for AI Coding Agents

Agent-Memory: Persistent Memory for AI Coding Agents

August 16, 2026

Agent-Memory provides persistent memory for AI coding agents, ensuring they remember past interactions and learned patterns across sessions. Built on the iii engine, it eliminates the need for re-explaining context, significantly improving agent efficiency and reducing token usage. This solution integrates seamlessly with various agents, offering a robust memory management system.

AIAgent MemoryLLM
Flask-Assets: Seamless Asset Management for Flask Applications

Flask-Assets: Seamless Asset Management for Flask Applications

July 26, 2026

Flask-Assets provides robust integration of the `webassets` library with Flask. It simplifies the process of merging, minifying, and compiling CSS and JavaScript files, significantly enhancing web application performance and streamlining development workflows.

PythonFlaskWeb Development
webassets: Asset Management for Python Web Development

webassets: Asset Management for Python Web Development

July 26, 2026

webassets is a robust library designed for asset management in Python web development. It simplifies the process of merging and compressing JavaScript and CSS files, enhancing application performance. This tool is essential for developers looking to optimize their frontend assets efficiently.

PythonWeb DevelopmentAsset Management
Become-A-Full-Stack-Web-Developer: Free Resources for Web Development

Become-A-Full-Stack-Web-Developer: Free Resources for Web Development

July 19, 2026

The Become-A-Full-Stack-Web-Developer repository is an extensive collection of free resources designed to guide aspiring developers through the entire journey of full-stack web development. It covers a wide array of topics, from foundational languages like HTML, CSS, and JavaScript to advanced frameworks such as React and Node.js, alongside databases, APIs, and career preparation. This resource is invaluable for anyone looking to build a strong skill set in modern web development.

Full StackWeb DevelopmentLearning Resources

Source repository

Open the original repository on GitHub.

17 counted GitHub visits

View on GitHub
OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️