• Stars
    star
    472
  • Rank 93,034 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📸 Quickly generate image from DOM node using HTML5 canvas and SVG

modern-screenshot

Minzip Version Downloads Issues License

Quickly generate image from DOM node using HTML5 canvas and SVG

Fork from html-to-image

English | 简体中文

📦 Install

npm i modern-screenshot

🦄 Usage

import { domToPng } from 'modern-screenshot'

domToPng(document.querySelector('#app')).then(dataUrl => {
  const link = document.createElement('a')
  link.download = 'screenshot.png'
  link.href = dataUrl
  link.click()
})
CDN
<script src="https://unpkg.com/modern-screenshot"></script>
<script>
  modernScreenshot.domToPng(document.querySelector('body')).then(dataUrl => {
    const link = document.createElement('a')
    link.download = 'screenshot.png'
    link.href = dataUrl
    link.click()
  })
</script>


Browser Console

⚠️ Partial embedding will fail due to CORS

const script = document.createElement('script')
script.src = "https://unpkg.com/modern-screenshot"
document.getElementsByTagName('head')[0].appendChild(script)

script.onload = () => {
  modernScreenshot
    .domToImage(document.querySelector('body'), {
      debug: true,
      progress: (current, total) => {
        console.log(`${ current }/${ total }`)
      }
    })
    .then(img => {
      const width = 600
      const height = img.height * (width / img.width)
      console.log('%c ', [
        `padding: 0 ${ width / 2 }px;`,
        `line-height: ${ height }px;`,
        `background-image: url('${ img.src }');`,
        `background-size: 100% 100%;`,
      ].join(''))
    })
}


Methods

method(node: Node, options?: Options)

DOM to dataURL

DOM to data

DOM to HTMLElement

Options

See the options.ts

Singleton context and web worker

Quick screenshots per second by reusing context and web worker

// use vite
import workerUrl from 'modern-screenshot/worker?url'
import { createContext, destroyContext, domToPng } from 'modern-screenshot'

async function screenshotsPerSecond() {
  const context = await createContext(document.querySelector('#app'), {
    workerUrl,
    workerNumber: 1,
  })
  for (let i = 0; i < 10; i++) {
    domToPng(context).then(dataUrl => {
      const link = document.createElement('a')
      link.download = `screenshot-${ i + 1 }.png`
      link.href = dataUrl
      link.click()
      if (i + 1 === 10) {
        destroyContext(context)
      }
    })
    await new Promise(resolve => setTimeout(resolve, 1000))
  }
}

screenshotsPerSecond()

See the context.ts

TODO

More Repositories

1

yh5

💬 Visual production tool, draggable, resizable, sketch for Vue 2.x
JavaScript
60
star
2

modern-gif

🚀 JavaScript GIF Codec.
TypeScript
28
star
3

ddoc

支持 Laravel/Lumen 下控制器注释生成API文档,生成数据库字典, 注册路由呈现
PHP
27
star
4

modern-rembg

🐘 Uses an AI model to remove image backgrounds. Only the ESM.
TypeScript
22
star
5

veno-ui

A Vue 3 UI Library. Vuetify 3 Variant
TypeScript
19
star
6

taobao-sdk

淘宝(淘宝客) SDK, 调用简单、语义化增强。支持 Laravel/Lumen。
PHP
17
star
7

jd-sdk

京东(京东联盟) SDK 封装, 调用简单、语义化增强。支持 Laravel/Lumen。
PHP
16
star
8

pdd-sdk

拼多多(多多客) SDK , 调用简单、语义化增强。支持 Laravel/Lumen。
PHP
14
star
9

dom-vcr

📸 Generates an video or GIF from a DOM node using HTML5 canvas and SVG
TypeScript
13
star
10

vite-plugin-iconify

🤹 A Vite Plugin for icons
TypeScript
12
star
11

modern-filters

A image / text / video filter collection
TypeScript
6
star
12

vuetify-gen

Vuetify extension components
JavaScript
5
star
13

deployment

部署 laravel5.5 及常用包,说明和介绍,自用.
4
star
14

dataoke-sdk

大淘客 SDK, 调用简单、语义化增强。支持 Laravel/Lumen。
PHP
4
star
15

modern-audio

🎵 A modern web audio library.
TypeScript
3
star
16

vue-multi-app

A modern Vue.js multiple pages cli (多页应用脚手架,支持多页转单页过渡,ejs配合后端模板)
JavaScript
3
star
17

modern-font

✍🏻 Convert and minify font supports WOFF TTF EOT.
TypeScript
3
star
18

venomancer

Node.js 下的 API 服务(可单独使用), 加 PHP 套件。HTML 渲染生成图片、URL 网页生成快照图片。
JavaScript
2
star
19

veno

The Golang Framework For Web
Go
2
star
20

yh5-demo

Vue
2
star
21

flutter-veno

A Flutter plugin packages.
Dart
2
star
22

homeiot

[WIP] 🏠 A Home IoT library
TypeScript
2
star
23

vue-crdt

CRDT in Vue.js
TypeScript
2
star
24

modern-mp4

🚀 JavaScript MP4 Codec and Muxer.
TypeScript
2
star
25

laravel-venomancer

Venomancer 的 Laravel/Lumen 套件。HTML 渲染生成图片、URL 网页生成快照图片。
PHP
2
star
26

modern-editor-core

🖋 A modern rich text editor core.
TypeScript
2
star
27

starter-veno-admin

Vue 3 + TypeScript Veno UI Admin Template.
TypeScript
2
star
28

modern-renderer

🔺 A WebGL low-level rendering tool library
TypeScript
1
star
29

starter-ts-cli

Starter template for TypeScript CLI library
TypeScript
1
star
30

modern-canvas

🎨 A data driven webgl canvas library
TypeScript
1
star
31

modern-fontmin

Fastest cropping font files in the browser
TypeScript
1
star
32

modern-text

🚀 Measure and render text in a way that describes the DOM.
TypeScript
1
star