-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
254 lines (221 loc) · 6.38 KB
/
Copy pathcode.js
File metadata and controls
254 lines (221 loc) · 6.38 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
figma.showUI(__html__, { width: 920, height: 720, themeColors: true });
var lastSource = null;
var lastBounds = null;
function emitSelection() {
var sel = figma.currentPage.selection;
var name = "";
var supported = false;
if (sel.length > 0) {
name = sel[0].name;
supported =
!!sel[0].absoluteBoundingBox && typeof sel[0].exportAsync === "function";
}
figma.ui.postMessage({
type: "selection-info",
count: sel.length,
name: name,
supported: supported,
});
}
figma.on("selectionchange", emitSelection);
emitSelection();
function exportSelection() {
var sel = figma.currentPage.selection;
if (sel.length === 0) {
figma.notify("Select a layer first.");
return Promise.resolve(null);
}
var source = sel[0];
var bounds = source.absoluteBoundingBox;
if (!bounds || typeof source.exportAsync !== "function") {
figma.notify("This layer cannot be exported.");
return Promise.resolve(null);
}
var longSide = Math.max(bounds.width, bounds.height);
var scale = longSide >= 2000 ? 1 : 2;
var exportPromise = source.exportAsync({
format: "PNG",
constraint: { type: "SCALE", value: scale },
});
var timeoutPromise = new Promise(function (resolve) {
setTimeout(function () {
resolve({ __timeout: true });
}, 15000);
});
return Promise.race([exportPromise, timeoutPromise])
.then(function (result) {
if (result && result.__timeout) {
figma.notify("Export timed out after 15s. Try a smaller layer.");
return null;
}
lastSource = source;
lastBounds = bounds;
return {
bytes: result,
name: source.name,
width: bounds.width,
height: bounds.height,
};
})
.catch(function (err) {
figma.notify(
"Export failed: " + (err && err.message ? err.message : "unknown"),
);
return null;
});
}
function applyZRotation(degrees) {
var sel = figma.currentPage.selection;
if (sel.length === 0) {
figma.notify("Select a layer first.");
return false;
}
var radians = (degrees * Math.PI) / 180;
var cos = Math.cos(radians);
var sin = Math.sin(radians);
var count = 0;
for (var i = 0; i < sel.length; i += 1) {
var target = sel[i];
if (
!("relativeTransform" in target) ||
!("width" in target) ||
!("height" in target)
) {
continue;
}
var m = target.relativeTransform;
var w = target.width;
var h = target.height;
var cx = (m[0][0] * w) / 2 + (m[0][1] * h) / 2 + m[0][2];
var cy = (m[1][0] * w) / 2 + (m[1][1] * h) / 2 + m[1][2];
var a = m[0][0] * cos + m[0][1] * sin;
var b = -m[0][0] * sin + m[0][1] * cos;
var c = m[1][0] * cos + m[1][1] * sin;
var d = -m[1][0] * sin + m[1][1] * cos;
target.relativeTransform = [
[a, b, cx - (a * w) / 2 - (b * h) / 2],
[c, d, cy - (c * w) / 2 - (d * h) / 2],
];
count += 1;
}
return count > 0;
}
function applyRaster(message) {
if (!lastSource || lastSource.removed || !lastBounds) {
figma.notify("Source layer is no longer available.");
return false;
}
var image;
try {
image = figma.createImage(message.bytes);
} catch (e) {
figma.notify("Image too large to apply. Try a smaller layer.");
return false;
}
var rect = figma.createRectangle();
rect.name = (lastSource.name || "Layer") + " 3D Rotated";
rect.resize(message.width, message.height);
rect.fills = [{ type: "IMAGE", scaleMode: "FILL", imageHash: image.hash }];
var parent =
lastSource.parent && "insertChild" in lastSource.parent
? lastSource.parent
: figma.currentPage;
if (parent === figma.currentPage) {
figma.currentPage.appendChild(rect);
} else {
var index = parent.children.indexOf(lastSource);
parent.insertChild(index + 1, rect);
}
var gap = 24;
var parentOffsetX = 0;
var parentOffsetY = 0;
if (parent !== figma.currentPage && parent.absoluteBoundingBox) {
parentOffsetX = parent.absoluteBoundingBox.x;
parentOffsetY = parent.absoluteBoundingBox.y;
}
rect.x =
lastBounds.x - parentOffsetX + lastBounds.width / 2 - message.width / 2;
rect.y = lastBounds.y - parentOffsetY + lastBounds.height + gap;
figma.currentPage.selection = [rect];
return true;
}
function resetSelection() {
var sel = figma.currentPage.selection;
var count = 0;
for (var i = 0; i < sel.length; i += 1) {
var node = sel[i];
if (
!("relativeTransform" in node) ||
!("width" in node) ||
!("height" in node)
) {
continue;
}
var m = node.relativeTransform;
var cx =
(m[0][0] * node.width) / 2 + (m[0][1] * node.height) / 2 + m[0][2];
var cy =
(m[1][0] * node.width) / 2 + (m[1][1] * node.height) / 2 + m[1][2];
node.relativeTransform = [
[1, 0, cx - node.width / 2],
[0, 1, cy - node.height / 2],
];
count += 1;
}
return count;
}
figma.ui.onmessage = function (msg) {
if (msg.type === "preview-request") {
exportSelection()
.then(function (data) {
if (!data) {
figma.ui.postMessage({ type: "source-failed" });
return;
}
try {
figma.ui.postMessage({
type: "source-loaded",
bytes: data.bytes,
name: data.name,
width: data.width,
height: data.height,
});
} catch (e) {
figma.notify(
"Could not send preview to UI: " +
(e && e.message ? e.message : "too large"),
);
figma.ui.postMessage({ type: "source-failed" });
}
})
.catch(function (err) {
figma.notify(
"Preview error: " +
(err && err.message ? err.message : "unknown"),
);
figma.ui.postMessage({ type: "source-failed" });
});
return;
}
if (msg.type === "apply") {
var ok = false;
if (msg.axis === "z") {
ok = applyZRotation(msg.angle);
if (ok) figma.notify("Applied Z rotation.");
} else {
ok = applyRaster(msg);
if (ok) figma.notify("Applied 3D rotation. Original layer kept.");
}
figma.ui.postMessage({ type: "applied", success: ok });
return;
}
if (msg.type === "reset") {
var n = resetSelection();
figma.notify("Reset " + n + " layer" + (n === 1 ? "" : "s") + ".");
figma.ui.postMessage({ type: "reset-done", count: n });
return;
}
if (msg.type === "close") {
figma.closePlugin();
}
};