-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcode.js
More file actions
193 lines (193 loc) · 6.83 KB
/
code.js
File metadata and controls
193 lines (193 loc) · 6.83 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This plugin enables users to generate and insert QR codes from URLs into Figma
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser environment (see documentation).
// functions
// resize vector with padding relative to frame dimension
let resizeVector = (msg, node, vector) => {
let scale = node.width / msg.size.width;
let padding = msg.padding * scale;
let width = node.width - (padding * 2);
let height = node.height - (padding * 2);
vector.vectorPaths = [
{
windingRule: "NONZERO",
data: msg.svgPath
}
];
vector.fills = [
{
type: 'SOLID',
color: {
r: 0,
g: 0,
b: 0
}
}
];
vector.resize(width, height);
vector.strokes = [];
vector.x = padding;
vector.y = padding;
vector.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
};
// This shows the HTML page in "ui.html".
figma.showUI(__html__, { width: 350, height: 450 });
// Calls to "parent.postMessage" from within the HTML page will trigger this
// callback. The callback will be passed the "pluginMessage" property of the
// posted message.
figma.ui.onmessage = msg => {
// One way of distinguishing between different types of messages sent from
// your HTML page is to use an object with a "type" property like this.
if (msg.type === 'create-qr-code-vector') {
// get current selection
var currentSel = figma.currentPage.selection;
if (currentSel.length === 0) {
// insert fill to node
let frame = figma.createFrame();
let vector = figma.createVector();
let viewport = figma.viewport.center;
let padding = msg.padding;
// set frame
frame.fills = [
{
type: 'SOLID',
color: {
r: 1,
g: 1,
b: 1
}
}
];
frame.name = 'QR Code';
frame.resize(msg.size.width + (padding * 2), msg.size.height + (padding * 2));
frame.x = viewport.x;
frame.y = viewport.y;
frame.appendChild(vector);
// set vector
vector.vectorPaths = [
{
windingRule: "NONZERO",
data: msg.svgPath
}
];
vector.fills = [
{
type: 'SOLID',
color: {
r: 0,
g: 0,
b: 0
}
}
];
vector.resize(msg.size.width, msg.size.height);
vector.strokes = [];
vector.x = padding;
vector.y = padding;
vector.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
figma.currentPage.appendChild(frame);
}
else {
// delete current vector and replace with new one
currentSel.forEach(node => {
if (node.type === 'FRAME') {
node.fills = [
{
type: 'SOLID',
color: {
r: 1,
g: 1,
b: 1
}
}
];
node.name = 'QR Code';
// if node has no children
if (node.children.length === 0) {
// create and append new vector
let vector = figma.createVector();
node.appendChild(vector);
resizeVector(msg, node, vector);
}
// if node has child with type vector
else if (node.children.length === 1) {
if (node.children[0].type === 'VECTOR') {
// replace vector with new QR code
let vector = node.children[0];
resizeVector(msg, node, vector);
}
else {
figma.notify(`Select a Frame with a vector child`);
}
}
}
else {
figma.notify(`Please select a Frame`);
}
});
}
}
// press insert and triggers this function
if (msg.type === 'create-qr-code-raster') {
// get the current selection in Figma
var currentSel = figma.currentPage.selection;
var padding = msg.padding;
// if no selection
if (currentSel.length === 0) {
// get image uint8array from canvas
var buffer = msg.buffer;
// convert uint8array to hash
var hash = figma.createImage(buffer).hash;
// get center viewport coordinates
var viewport = figma.viewport.center;
// create a new rectangle
const rect = figma.createRectangle();
// set x and y viewport coordinates to the rectangle
rect.x = viewport.x;
rect.y = viewport.y;
rect.resize(msg.size.width + (padding * 2), msg.size.height + (padding * 2));
// set type to IMAGE and set fill with image hash data
rect.fills = [
{
type: 'IMAGE',
scaleMode: 'FILL',
imageHash: hash
}
];
// add image to Figma
figma.currentPage.appendChild(rect);
}
// else replace old raster with new data
else {
// loop nodes to check type
currentSel.forEach(node => {
if (node.type === 'FRAME' ||
node.type === 'ELLIPSE' ||
node.type === 'POLYGON' ||
node.type === 'RECTANGLE' ||
node.type === 'STAR' ||
node.type === 'VECTOR') {
// insert fill to node
var buffer = msg.buffer;
var hash = figma.createImage(buffer).hash;
node.fills = [
{ type: 'IMAGE', scaleMode: 'FILL', imageHash: hash }
];
}
else {
figma.notify(`Please select a fillable object`);
}
});
}
}
if (msg.type === 'msg') {
figma.notify(msg.message, msg.timeout);
}
};