From 9c3ff9f6d6fadb5e46d18b55ead00ac0b718d17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E6=89=8B=E6=8D=A7=E9=B2=9C=E8=8A=B1?= <157215725@qq.com> Date: Tue, 14 Feb 2023 14:51:35 +0800 Subject: [PATCH] docs: context --- README.md | 17 ++++++++++++----- README.zh-CN.md | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ed7e0e6..e08f8fd 100644 --- a/README.md +++ b/README.md @@ -127,16 +127,23 @@ Quick screenshots per second by reusing context ```ts import { createContext, destroyContext, domToPng } from 'modern-screenshot' -createContext(document.querySelector('#app')).then(context => { - setInterval(() => { +async function screenshotsPerSecond() { + const context = awiat createContext(document.querySelector('#app')) + for (let i = 0; i < 10; i++) { domToPng(context).then(dataUrl => { const link = document.createElement('a') - link.download = 'screenshot.png' + link.download = `screenshot-${ i + 1 }.png` link.href = dataUrl link.click() + if (i + 1 === 10) { + destroyContext(context) + } }) - }, 1000) -}) + await new Promise(resolve => setTimeout(resolve, 1000)) + } +} + +screenshotsPerSecond() ``` See the [context.ts](src/context.ts) diff --git a/README.zh-CN.md b/README.zh-CN.md index 6e23ae8..8783038 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -127,16 +127,23 @@ DOM 转 HTMLElement ```ts import { createContext, destroyContext, domToPng } from 'modern-screenshot' -createContext(document.querySelector('#app')).then(context => { - setInterval(() => { +async function screenshotsPerSecond() { + const context = awiat createContext(document.querySelector('#app')) + for (let i = 0; i < 10; i++) { domToPng(context).then(dataUrl => { const link = document.createElement('a') - link.download = 'screenshot.png' + link.download = `screenshot-${ i + 1 }.png` link.href = dataUrl link.click() + if (i + 1 === 10) { + destroyContext(context) + } }) - }, 1000) -}) + await new Promise(resolve => setTimeout(resolve, 1000)) + } +} + +screenshotsPerSecond() ``` 请查看 [context.ts](src/context.ts)