-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathmask-image.ts
51 lines (43 loc) · 1.18 KB
/
mask-image.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
import { buildXMLString } from '../utils.js'
import buildBackgroundImage from './background-image.js'
import type { MaskProperty } from '../parser/mask.js'
const genMaskImageId = (id: string) => `satori_mi-${id}`
export default async function buildMaskImage(
v: {
id: string
left: number
top: number
width: number
height: number
},
style: Record<string, string | number>,
inheritedStyle: Record<string, string | number>
): Promise<[string, string]> {
if (!style.maskImage) return ['', '']
const { left, top, width, height, id } = v
const maskImage = style.maskImage as unknown as MaskProperty[]
const length = maskImage.length
if (!length) return ['', '']
const miId = genMaskImageId(id)
let mask = ''
for (let i = 0; i < length; i++) {
const m = maskImage[i]
const [_id, def] = await buildBackgroundImage(
{ id: `${miId}-${i}`, left, top, width, height },
m,
inheritedStyle,
'mask'
)
mask +=
def +
buildXMLString('rect', {
x: left,
y: top,
width,
height,
fill: `url(#${_id})`,
})
}
mask = buildXMLString('mask', { id: miId }, mask)
return [miId, mask]
}