4.4.39 • Published 28 days ago

modern-screenshot v4.4.39

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

📦 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()
})
<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>

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

4.4.39

28 days ago

4.4.38

3 months ago

4.4.37

5 months ago

4.4.32

7 months ago

4.4.36

5 months ago

4.4.35

5 months ago

4.4.34

6 months ago

4.4.33

7 months ago

4.4.31

8 months ago

4.4.30

8 months ago

4.4.25

12 months ago

4.4.29

9 months ago

4.4.28

9 months ago

4.4.27

9 months ago

4.4.26

11 months ago

4.4.21

1 year ago

4.4.20

1 year ago

4.4.24

12 months ago

4.4.23

12 months ago

4.4.22

1 year ago

4.4.10

1 year ago

4.4.14

1 year ago

4.4.13

1 year ago

4.4.12

1 year ago

4.4.11

1 year ago

4.4.18

1 year ago

4.4.17

1 year ago

4.4.16

1 year ago

4.4.15

1 year ago

4.4.19

1 year ago

4.4.9

1 year ago

4.4.8

1 year ago

4.4.5

1 year ago

4.3.6

1 year ago

4.2.7

1 year ago

4.4.4

1 year ago

4.3.5

1 year ago

4.2.6

1 year ago

4.4.7

1 year ago

4.2.9

1 year ago

4.4.6

1 year ago

4.2.8

1 year ago

4.4.1

1 year ago

4.3.2

1 year ago

4.2.3

1 year ago

4.4.0

1 year ago

4.3.1

1 year ago

4.2.10

1 year ago

4.2.2

1 year ago

4.4.3

1 year ago

4.3.4

1 year ago

4.2.11

1 year ago

4.2.5

1 year ago

4.4.2

1 year ago

4.3.3

1 year ago

4.2.12

1 year ago

4.2.4

1 year ago

4.3.0

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.2.13

1 year ago

4.1.7

1 year ago

4.1.4

1 year ago

4.1.3

1 year ago

4.0.4

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

4.1.0

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.1.2

1 year ago

4.0.3

1 year ago

4.1.1

1 year ago

4.0.2

1 year ago

3.0.0

2 years ago