Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

devxiyang/html2image

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ธ HTML2Image

๐Ÿš€ Lightning-fast HTML to Image conversion using HTML5 Canvas and SVG

Website NPM Version Bundle Size Downloads License TypeScript

Transform any DOM element into high-quality images with ease

๐ŸŒ Website โ€ข ๐ŸŽฏ Features โ€ข ๐Ÿ“ฆ Installation โ€ข ๐Ÿš€ Quick Start โ€ข ๐Ÿ“– Documentation โ€ข ๐Ÿ’ก Examples


๐ŸŒ Official Website

Visit our official website at html2image.pro for:

  • ๐ŸŽจ Live Demo & Playground
  • ๐Ÿ“š Comprehensive Documentation
  • ๐Ÿ’ก Advanced Usage Examples
  • ๐Ÿ› ๏ธ API Explorer
  • ๐Ÿ”ง Configuration Generator

โœจ Features

  • ๐ŸŽฏ Zero Dependencies - Lightweight and self-contained
  • ๐Ÿš€ High Performance - Optimized with Web Workers and context reuse
  • ๐ŸŽจ Multiple Formats - PNG, JPEG, WebP, SVG, Canvas, Blob support
  • ๐Ÿ“ฑ Cross-Platform - Works in all modern browsers
  • ๐Ÿ”ง TypeScript Ready - Full type definitions included
  • ๐ŸŒ Modern API - Promise-based with async/await support
  • ๐ŸŽ›๏ธ Highly Configurable - Extensive options for customization
  • ๐Ÿ“ Pixel Perfect - Maintains exact styling and layout

๐Ÿ“ฆ Installation

Package Manager

# npm
npm install @devxiyang/html2image

# yarn
yarn add @devxiyang/html2image

# pnpm
pnpm add @devxiyang/html2image

Try Online

Visit our Playground to try HTML2Image directly in your browser without installation.

๐Ÿš€ Quick Start

Basic Usage

import { domToPng } from '@devxiyang/html2image'

// Convert any element to PNG
const element = document.querySelector('#my-element')
const dataUrl = await domToPng(element)

// Download the image
const link = document.createElement('a')
link.download = 'screenshot.png'
link.href = dataUrl
link.click()

With Options

import { domToPng } from '@devxiyang/html2image'

const dataUrl = await domToPng(element, {
  width: 1920,
  height: 1080,
  scale: 2,
  backgroundColor: '#ffffff',
  style: {
    'transform': 'scale(1.5)',
    'transform-origin': 'top left'
  }
})

๐Ÿ“– API Reference

Core Methods

Method Output Type Description
domToPng(node, options?) Data URL Convert to PNG format
domToJpeg(node, options?) Data URL Convert to JPEG format
domToWebp(node, options?) Data URL Convert to WebP format
domToSvg(node, options?) Data URL Convert to SVG format
domToCanvas(node, options?) HTMLCanvasElement Convert to Canvas element
domToBlob(node, options?) Blob Convert to Blob object
domToPixel(node, options?) ImageData Convert to pixel data

Advanced Methods

Method Description
createContext(node, options?) Create reusable context for batch operations
destroyContext(context) Clean up context resources
domToForeignObjectSvg(node, options?) Convert using SVG foreignObject
domToImage(node, options?) Convert to HTMLImageElement

Options Interface

interface Options {
  // Dimensions
  width?: number
  height?: number
  scale?: number

  // Quality & Format
  quality?: number
  backgroundColor?: string

  // Filtering
  filter?: (node: Element) => boolean

  // Styling
  style?: Record<string, string>

  // Advanced
  cacheBust?: boolean
  imagePlaceholder?: string
  skipAutoScale?: boolean
  debug?: boolean

  // Callbacks
  progress?: (current: number, total: number) => void

  // Web Worker
  workerUrl?: string
  workerNumber?: number
}

๐Ÿ’ก Examples

๐Ÿ“Š Convert Chart to Image

import { domToPng } from '@devxiyang/html2image'

// Capture a chart with high DPI
const chart = document.querySelector('#my-chart')
const imageUrl = await domToPng(chart, {
  scale: 2, // 2x resolution for crisp images
  backgroundColor: 'white'
})

๐ŸŽจ Custom Styling

import { domToJpeg } from '@devxiyang/html2image'

const element = document.querySelector('#content')
const imageUrl = await domToJpeg(element, {
  quality: 0.95,
  style: {
    'transform': 'scale(0.8)',
    'border': '2px solid #333',
    'border-radius': '8px'
  }
})

โšก High-Performance Batch Processing

import { createContext, destroyContext, domToPng } from '@devxiyang/html2image'

// Create context once for multiple screenshots
const context = await createContext(document.querySelector('#app'), {
  workerUrl: '/html2image-worker.js',
  workerNumber: 2
})

// Take multiple screenshots efficiently
const screenshots = await Promise.all([
  domToPng(context),
  domToPng(context),
  domToPng(context)
])

// Clean up
destroyContext(context)

๐ŸŽฏ Progress Tracking

import { domToPng } from '@devxiyang/html2image'

const imageUrl = await domToPng(element, {
  progress: (current, total) => {
    console.log(`Processing: ${current}/${total} (${Math.round(current / total * 100)}%)`)
  }
})

๐Ÿ”ง Advanced Configuration

Custom Image Placeholder

const options = {
  imagePlaceholder: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCI+PC9zdmc+'
}

Element Filtering

const options = {
  filter: (node) => {
    // Skip elements with 'no-screenshot' class
    return !node.classList?.contains('no-screenshot')
  }
}

๐ŸŒŸ Use Cases

  • ๐Ÿ“Š Data Visualization - Export charts and graphs
  • ๐ŸŽจ Design Tools - Screenshot design components
  • ๐Ÿ“ฑ Social Media - Generate preview images
  • ๐Ÿ“‹ Reports - Convert HTML reports to images
  • ๐ŸŽฎ Gaming - Capture game states
  • ๐Ÿ“š Documentation - Generate visual documentation

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments



ยฉ 2024 HTML2Image. All rights reserved. Visit html2image.pro for more information.

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.