-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode.ts
44 lines (35 loc) · 948 Bytes
/
code.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
/**
* VectorizeImage
*
* Node has to contain an image fill
*
* @param node
*/
async function VectorizeImage() {
const node: any = figma.currentPage.selection[0];
if (!node) {
return figma.closePlugin();
}
for (const paint of node.fills) {
if (paint.type !== "IMAGE") {
return figma.closePlugin();
}
// Get image data
const image = figma.getImageByHash(paint.imageHash);
const imageData = await image.getBytesAsync();
// Set up worker
figma.showUI(__html__, { visible: false });
figma.ui.postMessage({ type: "networkRequest", imageData });
// Listen to worker
figma.ui.onmessage = async (msg) => {
// Create vector node
const svgNode = figma.createNodeFromSvg(msg.data);
svgNode.name = `${node.name} (vectorized)`;
svgNode.x = node.x + node.width + 40;
svgNode.y = node.y;
// Done!
figma.closePlugin();
};
}
}
VectorizeImage();