forked from qq15725/modern-screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-to-blob.ts
31 lines (30 loc) · 1.3 KB
/
dom-to-blob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { Context } from '../context'
import type { Options } from '../options'
import { changeJpegDpi } from '../change-jpeg-dpi'
import { changePngDpi } from '../change-png-dpi'
import { orCreateContext } from '../create-context'
import { blobToArrayBuffer, canvasToBlob } from '../utils'
import { domToCanvas } from './dom-to-canvas'
export async function domToBlob<T extends Node>(node: T, options?: Options): Promise<Blob>
export async function domToBlob<T extends Node>(context: Context<T>): Promise<Blob>
export async function domToBlob(node: any, options?: any): Promise<Blob> {
const context = await orCreateContext(node, options)
const { log, type, quality, dpi } = context
const canvas = await domToCanvas(context)
log.time('canvas to blob')
const blob = await canvasToBlob(canvas, type, quality)
if (['image/png', 'image/jpeg'].includes(type) && dpi) {
const arrayBuffer = await blobToArrayBuffer(blob.slice(0, 33))
let uint8Array = new Uint8Array<any>(arrayBuffer)
if (type === 'image/png') {
uint8Array = changePngDpi(uint8Array, dpi)
}
else if (type === 'image/jpeg') {
uint8Array = changeJpegDpi(uint8Array, dpi)
}
log.timeEnd('canvas to blob')
return new Blob([uint8Array, blob.slice(33)], { type })
}
log.timeEnd('canvas to blob')
return blob
}