forked from qq15725/modern-screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-to-image.ts
22 lines (21 loc) · 999 Bytes
/
dom-to-image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import type { Context } from '../context'
import type { Options } from '../options'
import { orCreateContext } from '../create-context'
import { createImage } from '../utils'
import { domToDataUrl } from './dom-to-data-url'
import { domToSvg } from './dom-to-svg'
export async function domToImage<T extends Node>(node: T, options?: Options): Promise<HTMLImageElement>
export async function domToImage<T extends Node>(context: Context<T>): Promise<HTMLImageElement>
export async function domToImage(node: any, options?: any): Promise<HTMLImageElement> {
const context = await orCreateContext(node, options)
const { ownerDocument, width, height, scale, type } = context
const url = type === 'image/svg+xml'
? await domToSvg(context)
: await domToDataUrl(context)
const image = createImage(url, ownerDocument)
image.width = Math.floor(width * scale)
image.height = Math.floor(height * scale)
image.style.width = `${width}px`
image.style.height = `${height}px`
return image
}