-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexportStickerbook.ts
158 lines (137 loc) · 3.89 KB
/
exportStickerbook.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type {
BackgroundItem,
ExportFormat,
ForegroundItem,
Frame,
StickerItem,
} from "../types"
import { coverCanvas } from "./coverCanvas"
import { loadUrlAsImage } from "./loadUrlAsImage"
import { renderSticker } from "./renderSticker"
import { EXPORT_FORMATS } from "./consts"
interface ExportStickerbookOptions<T extends ExportFormat> {
/**
* A canvas element to draw to. If not provided, a new canvas will be created.
*/
canvas?: HTMLCanvasElement
backgrounds?: BackgroundItem[]
frames?: Frame[]
stickers?: StickerItem[]
foregrounds?: ForegroundItem[]
/**
* The width of the output image.
*/
outputWidth: number
/**
* The height of the output image.
*/
outputHeight: number
/**
* **`image`** will generate a url using `window.URL.createObjectURL`
* **`canvas`** will just return the provided `canvas` or a new one
* **`blob`** will return a `Blob` using `HTMLCanvasElement.toBlob()`
*/
format: T
}
// prettier-ignore
type ExportReturn<K extends ExportFormat> =
K extends "canvas" ? HTMLCanvasElement :
K extends "blob" ? Blob :
K extends "image" ? string :
never
/**
* Returns a representation of the stickerbook in the chosen `format`.
* @param {ExportStickerbookOptions[]}
* @returns {Promise<ExportReturn<T>>}
*/
export async function exportStickerbook<T extends ExportFormat>({
canvas,
backgrounds,
frames,
stickers,
foregrounds,
outputWidth,
outputHeight,
format,
}: ExportStickerbookOptions<T>): Promise<ExportReturn<T>> {
if (!outputWidth && !outputHeight)
throw Error("'outputWidth' and 'outputHeight' needs to be bigger than 0")
if (!EXPORT_FORMATS.includes(format))
throw Error(
`Invalid 'format'. 'format' must be one of: ${EXPORT_FORMATS.join(",")}`
)
// output canvas
const outputCanvas = canvas || document.createElement("canvas")
const outputCtx = outputCanvas.getContext("2d")!
outputCanvas.width = outputWidth
outputCanvas.height = outputHeight
// draw backgrounds
if (backgrounds && backgrounds.length) {
for (const background of backgrounds) {
if (!background.image) continue
await coverCanvas({
ctx: outputCtx,
img: background.image,
width: outputWidth,
height: outputHeight,
type: background.type,
})
}
}
// draw frame
if (frames && frames.length) {
for (const frame of frames) {
if (!frame.image) continue
await coverCanvas({
ctx: outputCtx,
img: frame.image,
width: outputWidth,
height: outputHeight,
})
}
}
if (stickers && stickers.length > 0) {
// sort by order
const sortedStickers = [...stickers].sort((a, b) => a.order - b.order)
// load all images to Image elements
const loadedStickers = await Promise.all(
sortedStickers.map(({ image }) => loadUrlAsImage(image))
)
for (let i = 0; i < loadedStickers.length; i++) {
const sticker = sortedStickers[i]
// Render the sticker as a stamp
const stamp = renderSticker(sticker, loadedStickers[i], [
outputWidth,
outputHeight,
])
// final draw
outputCtx.drawImage(stamp, 0, 0)
}
}
// draw foregrounds
if (foregrounds && foregrounds.length) {
for (const foreground of foregrounds) {
if (!foreground.image) continue
await coverCanvas({
ctx: outputCtx,
img: foreground.image,
width: outputWidth,
height: outputHeight,
})
}
}
// download
if (format === "canvas") return outputCanvas as ExportReturn<T>
const blob = await new Promise<Blob>((resolve, reject) => {
try {
outputCanvas.toBlob((blob) => {
if (!blob) throw Error("Failed to create blob")
resolve(blob)
})
} catch (err) {
reject(err)
}
})
if (format === "blob") return blob as ExportReturn<T>
return window.URL.createObjectURL(blob) as ExportReturn<T>
}