|
1 | 1 | import { Options } from './options' |
2 | 2 | import { cloneNode } from './cloneNode' |
3 | 3 | import { embedImages } from './embedImages' |
4 | | -import { applyStyleWithOptions } from './applyStyleWithOptions' |
| 4 | +import { applyStyleFromOptions } from './applyStyleFromOptions' |
5 | 5 | import { embedWebFonts, getWebFontCSS } from './embedWebFonts' |
6 | 6 | import { |
7 | | - getNodeWidth, |
8 | | - getNodeHeight, |
| 7 | + getImageSize, |
9 | 8 | getPixelRatio, |
10 | 9 | createImage, |
11 | 10 | canvasToBlob, |
12 | 11 | nodeToDataURL, |
| 12 | + checkCanvasDimensions, |
13 | 13 | } from './util' |
14 | 14 |
|
15 | | -function getImageSize(node: HTMLElement, options: Options = {}) { |
16 | | - const width = options.width || getNodeWidth(node) |
17 | | - const height = options.height || getNodeHeight(node) |
18 | | - |
19 | | - return { width, height } |
20 | | -} |
21 | | - |
22 | 15 | export async function toSvg<T extends HTMLElement>( |
23 | 16 | node: T, |
24 | 17 | options: Options = {}, |
25 | 18 | ): Promise<string> { |
26 | 19 | const { width, height } = getImageSize(node, options) |
27 | | - |
28 | | - return Promise.resolve(node) |
29 | | - .then((nativeNode) => cloneNode(nativeNode, options, true)) |
30 | | - .then((clonedNode) => embedWebFonts(clonedNode!, options)) |
31 | | - .then((clonedNode) => embedImages(clonedNode, options)) |
32 | | - .then((clonedNode) => applyStyleWithOptions(clonedNode, options)) |
33 | | - .then((clonedNode) => nodeToDataURL(clonedNode, width, height)) |
| 20 | + const clonedNode = (await cloneNode(node, options, true)) as HTMLElement |
| 21 | + await embedWebFonts(clonedNode, options) |
| 22 | + await embedImages(clonedNode, options) |
| 23 | + applyStyleFromOptions(clonedNode, options) |
| 24 | + const datauri = await nodeToDataURL(clonedNode, width, height) |
| 25 | + return datauri |
34 | 26 | } |
35 | 27 |
|
36 | | -const dimensionCanvasLimit = 16384 // as per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas#maximum_canvas_size |
37 | | - |
38 | | -function checkCanvasDimensions(canvas: HTMLCanvasElement) { |
39 | | - if ( |
40 | | - canvas.width > dimensionCanvasLimit || |
41 | | - canvas.height > dimensionCanvasLimit |
42 | | - ) { |
43 | | - if ( |
44 | | - canvas.width > dimensionCanvasLimit && |
45 | | - canvas.height > dimensionCanvasLimit |
46 | | - ) { |
47 | | - if (canvas.width > canvas.height) { |
48 | | - canvas.height *= dimensionCanvasLimit / canvas.width |
49 | | - canvas.width = dimensionCanvasLimit |
50 | | - } else { |
51 | | - canvas.width *= dimensionCanvasLimit / canvas.height |
52 | | - canvas.height = dimensionCanvasLimit |
53 | | - } |
54 | | - } else if (canvas.width > dimensionCanvasLimit) { |
55 | | - canvas.height *= dimensionCanvasLimit / canvas.width |
56 | | - canvas.width = dimensionCanvasLimit |
57 | | - } else { |
58 | | - canvas.width *= dimensionCanvasLimit / canvas.height |
59 | | - canvas.height = dimensionCanvasLimit |
60 | | - } |
61 | | - } |
62 | | -} |
63 | 28 | export async function toCanvas<T extends HTMLElement>( |
64 | 29 | node: T, |
65 | 30 | options: Options = {}, |
66 | 31 | ): Promise<HTMLCanvasElement> { |
67 | | - return toSvg(node, options) |
68 | | - .then(createImage) |
69 | | - .then((img) => { |
70 | | - const canvas = document.createElement('canvas') |
71 | | - const context = canvas.getContext('2d')! |
72 | | - const ratio = options.pixelRatio || getPixelRatio() |
73 | | - const { width, height } = getImageSize(node, options) |
| 32 | + const svg = await toSvg(node, options) |
| 33 | + const img = await createImage(svg) |
74 | 34 |
|
75 | | - const canvasWidth = options.canvasWidth || width |
76 | | - const canvasHeight = options.canvasHeight || height |
| 35 | + const canvas = document.createElement('canvas') |
| 36 | + const context = canvas.getContext('2d')! |
| 37 | + const ratio = options.pixelRatio || getPixelRatio() |
| 38 | + const { width, height } = getImageSize(node, options) |
| 39 | + const canvasWidth = options.canvasWidth || width |
| 40 | + const canvasHeight = options.canvasHeight || height |
77 | 41 |
|
78 | | - canvas.width = canvasWidth * ratio |
79 | | - canvas.height = canvasHeight * ratio |
| 42 | + canvas.width = canvasWidth * ratio |
| 43 | + canvas.height = canvasHeight * ratio |
80 | 44 |
|
81 | | - if (!options.skipAutoScale) { |
82 | | - checkCanvasDimensions(canvas) |
83 | | - } |
84 | | - canvas.style.width = `${canvasWidth}` |
85 | | - canvas.style.height = `${canvasHeight}` |
| 45 | + if (!options.skipAutoScale) { |
| 46 | + checkCanvasDimensions(canvas) |
| 47 | + } |
| 48 | + canvas.style.width = `${canvasWidth}` |
| 49 | + canvas.style.height = `${canvasHeight}` |
86 | 50 |
|
87 | | - if (options.backgroundColor) { |
88 | | - context.fillStyle = options.backgroundColor |
89 | | - context.fillRect(0, 0, canvas.width, canvas.height) |
90 | | - } |
| 51 | + if (options.backgroundColor) { |
| 52 | + context.fillStyle = options.backgroundColor |
| 53 | + context.fillRect(0, 0, canvas.width, canvas.height) |
| 54 | + } |
91 | 55 |
|
92 | | - context.drawImage(img, 0, 0, canvas.width, canvas.height) |
| 56 | + context.drawImage(img, 0, 0, canvas.width, canvas.height) |
93 | 57 |
|
94 | | - return canvas |
95 | | - }) |
| 58 | + return canvas |
96 | 59 | } |
97 | 60 |
|
98 | 61 | export async function toPixelData<T extends HTMLElement>( |
99 | 62 | node: T, |
100 | 63 | options: Options = {}, |
101 | 64 | ): Promise<Uint8ClampedArray> { |
102 | 65 | const { width, height } = getImageSize(node, options) |
103 | | - return toCanvas(node, options).then((canvas) => { |
104 | | - const ctx = canvas.getContext('2d')! |
105 | | - return ctx.getImageData(0, 0, width, height).data |
106 | | - }) |
| 66 | + const canvas = await toCanvas(node, options) |
| 67 | + const ctx = canvas.getContext('2d')! |
| 68 | + return ctx.getImageData(0, 0, width, height).data |
107 | 69 | } |
108 | 70 |
|
109 | 71 | export async function toPng<T extends HTMLElement>( |
110 | 72 | node: T, |
111 | 73 | options: Options = {}, |
112 | 74 | ): Promise<string> { |
113 | | - return toCanvas(node, options).then((canvas) => canvas.toDataURL()) |
| 75 | + const canvas = await toCanvas(node, options) |
| 76 | + return canvas.toDataURL() |
114 | 77 | } |
115 | 78 |
|
116 | 79 | export async function toJpeg<T extends HTMLElement>( |
117 | 80 | node: T, |
118 | 81 | options: Options = {}, |
119 | 82 | ): Promise<string> { |
120 | | - return toCanvas(node, options).then((canvas) => |
121 | | - canvas.toDataURL('image/jpeg', options.quality || 1), |
122 | | - ) |
| 83 | + const canvas = await toCanvas(node, options) |
| 84 | + return canvas.toDataURL('image/jpeg', options.quality || 1) |
123 | 85 | } |
124 | 86 |
|
125 | 87 | export async function toBlob<T extends HTMLElement>( |
126 | 88 | node: T, |
127 | 89 | options: Options = {}, |
128 | 90 | ): Promise<Blob | null> { |
129 | | - return toCanvas(node, options).then(canvasToBlob) |
| 91 | + const canvas = await toCanvas(node, options) |
| 92 | + const blob = await canvasToBlob(canvas) |
| 93 | + return blob |
130 | 94 | } |
131 | 95 |
|
132 | 96 | export async function getFontEmbedCSS<T extends HTMLElement>( |
|
0 commit comments