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.

Frontend Slides: Create Stunning Web Presentations with AI Coding Agents

Frontend Slides: Create Stunning Web Presentations with AI Coding Agents

June 28, 2026

Frontend Slides is an innovative GitHub repository that empowers users to create beautiful web presentations using AI coding agents. It simplifies the design process by offering visual style discovery and can even convert existing PowerPoint files into elegant HTML slides. This project is ideal for non-designers seeking professional, dependency-free presentations.

AI SlidesClaude CodeGenerative UI
OrbitDB: Peer-to-Peer Databases for the Decentralized Web

OrbitDB: Peer-to-Peer Databases for the Decentralized Web

June 22, 2026

OrbitDB is a serverless, distributed, peer-to-peer database designed for the decentralized web. It leverages IPFS for data storage and Libp2p Pubsub for automatic synchronization, ensuring eventual consistency through Merkle-CRDTs. This makes OrbitDB an excellent choice for p2p, decentralized, blockchain, and local-first web applications, offering various database types like event logs, documents, and key-value stores.

JavaScriptDatabaseDecentralized
Open-Higgsfield-AI: Free, Self-Hosted AI Image Generation & Cinema Studio

Open-Higgsfield-AI: Free, Self-Hosted AI Image Generation & Cinema Studio

June 15, 2026

Open-Higgsfield-AI offers an open-source, self-hosted alternative for AI image generation and a cinema studio. It provides access to over 20 models, including Flux, SDXL, Midjourney, and Ideogram, allowing users to create stunning visuals and cinematic content. This MIT-licensed project is fully customizable and designed for local operation.

JavaScriptAIImage Generation
Nextcloud Office Online: Seamless Document Integration

Nextcloud Office Online: Seamless Document Integration

June 13, 2026

The `nextcloud/officeonline` repository provides an integration app for Nextcloud, enabling users to edit documents directly within their Nextcloud instance using an on-premise Office Online Server. This solution facilitates collaborative document editing and viewing, enhancing productivity for Nextcloud users. It specifically supports self-hosted Office Online Server deployments, not cloud-based Office 365.

JavaScriptNextcloudOffice Online

Source repository

Open the original repository on GitHub.

7 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 ❤️