使用 HTML5 canvas 和 SVG 从 DOM 节点生成视频或 GIF
English | 简体中文
npm i dom-vcr
# 可选依赖
npm i modern-gif # 导出 GIF 需要
npm i modern-mp4 # 导出 MP4 需要
npm i mp4box
CDN
<script src="https://unpkg.com/dom-vcr"></script>
import { createVcr } from 'dom-vcr'
const dom = document.querySelector('#app')
const vcr = createVcr(dom, {
type: 'webm',
interval: 1000,
})
async function generate() {
dom.style.backgroundColor = 'red'
await vcr.addFrame()
dom.style.backgroundColor = 'yellow'
await vcr.addFrame()
dom.style.backgroundColor = 'green'
await vcr.addFrame()
const blob = await vcr.render()
window.open(URL.createObjectURL(blob))
}
generate()
MP4
需要安装
mp4box
、modern-mp4
import { createVcr } from 'dom-vcr'
const dom = document.querySelector('#app')
const vcr = createVcr(dom, {
type: 'mp4',
interval: 1000,
})
async function generate() {
dom.style.backgroundColor = 'red'
await vcr.addFrame()
dom.style.backgroundColor = 'yellow'
await vcr.addFrame()
dom.style.backgroundColor = 'green'
await vcr.addFrame()
const blob = await vcr.render()
window.open(URL.createObjectURL(blob))
}
generate()
GIF
需要安装
modern-gif
import { createVcr } from 'dom-vcr'
const dom = document.querySelector('#app')
const vcr = createVcr(dom, {
type: 'gif',
interval: 1000,
})
async function generate() {
dom.style.backgroundColor = 'red'
await vcr.addFrame()
dom.style.backgroundColor = 'yellow'
await vcr.addFrame()
dom.style.backgroundColor = 'green'
await vcr.addFrame()
const blob = await vcr.render()
window.open(URL.createObjectURL(blob))
}
generate()