-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
147 lines (147 loc) · 6.19 KB
/
code.js
File metadata and controls
147 lines (147 loc) · 6.19 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
"use strict";
/// <reference types="@figma/plugin-typings" />
/* ────────────────────────────────────────────────────────────────
Raised-Intersection • headless plugin
Works on Vectors *and* Lines, regardless of nesting
© 2025 – feel free to MIT this!
──────────────────────────────────────────────────────────────── */
function run() {
/* ═════ helpers ══════════════════════════════════════════════ */
var _a, _b;
/** Convert Paint → “#rrggbb” */
const paintToHex = (paint) => {
const { r, g, b } = paint.color;
return ("#" +
[r, g, b]
.map(v => Math.round(v * 255).toString(16).padStart(2, "0"))
.join(""));
};
/** Convert “#rrggbb” → RGB 0-1 */
const hexToRgb = (hex) => {
const n = parseInt(hex.replace(/^#/, ""), 16);
return {
r: ((n >> 16) & 0xff) / 255,
g: ((n >> 8) & 0xff) / 255,
b: (n & 0xff) / 255
};
};
const apply = (m, p) => ({
x: m[0][0] * p.x + m[0][1] * p.y + m[0][2],
y: m[1][0] * p.x + m[1][1] * p.y + m[1][2],
});
/** Local-geometry pt → page pt (honours vectorTransform) */
const toAbsolute = (node, p) => {
var _a;
const vt = (_a = node.vectorTransform) !== null && _a !== void 0 ? _a : [[1, 0, 0], [0, 1, 0]]; // identity if undefined
return apply(node.absoluteTransform, apply(vt, p));
};
// /** Local pt → absolute (page) pt */
// const toAbsolute = (node: SceneNode, pt: Vector): Vector => {
// const m = node.absoluteTransform;
// return {
// x: m[0][0] * pt.x + m[0][1] * pt.y + m[0][2],
// y: m[1][0] * pt.x + m[1][1] * pt.y + m[1][2]
// };
// };
/** Magnitude + unit direction */
const dirLen = (p1, p2) => {
const dx = p2.x - p1.x, dy = p2.y - p1.y;
const len = Math.hypot(dx, dy);
return { dir: { x: dx / len, y: dy / len }, len };
};
/** Intersection of two infinite lines p+tv and q+sw */
const intersect = (p, v, q, w) => {
const denom = v.x * w.y - v.y * w.x;
if (Math.abs(denom) < 1e-4)
return null; // parallel
const t = ((q.x - p.x) * w.y - (q.y - p.y) * w.x) / denom;
return { x: p.x + t * v.x, y: p.y + t * v.y };
};
/** Build an upward-bulging semicircle using 2 cubic Béziers */
const makeArc = (center, radius, stroke, weight, rotationDeg) => {
const k = 0.5522847498; // circle constant (4*(√2-1)/3)
const d = [
`M ${-radius} 0`,
`C ${-radius} ${-k * radius} ${-k * radius} ${-radius} 0 ${-radius}`,
`C ${k * radius} ${-radius} ${radius} ${-k * radius} ${radius} 0`
].join(" ");
const v = figma.createVector();
v.vectorPaths = [{ windingRule: "NONZERO", data: d }];
v.strokes = [stroke];
v.strokeCap = "ROUND";
v.strokeWeight = weight;
// Node’s own geometry spans from –r…+r horizontally and –r…0 vertically,
// so its top-left corner is (center.x − r, center.y − r).
v.x = center.x - radius;
v.y = center.y - radius;
v.rotation = rotationDeg;
return v;
};
const isHorizontal = (v) => Math.abs(v.x) >= Math.abs(v.y);
/** Recursively collect single-segment vectors/lines inside any selection */
function collectLines(nodes) {
const found = [];
for (const n of nodes) {
if (n.type === "VECTOR") {
if (n.vectorNetwork.segments.length === 1)
found.push(n);
}
else if (n.type === "LINE") {
found.push(n);
}
else if ("children" in n) {
found.push(...collectLines(n.children));
}
}
return found;
}
/* ═════ main flow ═════════════════════════════════════════════ */
const lines = collectLines(figma.currentPage.selection);
if (lines.length !== 2) {
figma.notify("Select exactly two straight vector strokes 🙏");
figma.closePlugin();
return;
}
/* Extract absolute endpoints for each line / vector */
const endpoints = lines.map(line => {
if (line.type === "VECTOR") {
const v = line.vectorNetwork.vertices;
return [
toAbsolute(line, { x: v[0].x, y: v[0].y }),
toAbsolute(line, { x: v[1].x, y: v[1].y })
];
}
else { // LINE node
// Geometry for a Line is the segment (0,0) → (1,0) in local space.
return [
toAbsolute(line, { x: 0, y: 0 }),
toAbsolute(line, { x: 1, y: 0 })
];
}
});
const [p1, p2] = endpoints[0];
const [q1, q2] = endpoints[1];
const { dir: vDir } = dirLen(p1, p2);
const { dir: wDir } = dirLen(q1, q2);
const ip = intersect(p1, vDir, q1, wDir);
if (!ip) {
figma.notify("Those two lines don’t intersect 🤷♂️");
figma.closePlugin();
return;
}
/* Decide which stroke “jumps” */
const jumper = isHorizontal(vDir) ? lines[0] : lines[1];
const jumperStroke = (_b = (_a = jumper.strokes) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : {
type: "SOLID",
color: { r: 1, g: 0, b: 0 }
};
const weight = typeof jumper.strokeWeight === "number" ? jumper.strokeWeight : 2;
const radius = weight * 2;
const jumperDir = isHorizontal(vDir) ? vDir : wDir;
const rotation = (Math.atan2(jumperDir.y, jumperDir.x) * 180) / Math.PI;
const arc = makeArc(ip, radius, jumperStroke, weight, rotation);
figma.currentPage.appendChild(arc);
figma.notify("✨ Jump-over added!");
figma.closePlugin();
}
run(); // boot it