@rogieking/figui3 6.4.1 → 6.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fig-lab.css +1 -0
- package/dist/fig-lab.js +14 -0
- package/fig-lab.css +338 -0
- package/fig-lab.js +1922 -0
- package/package.json +9 -3
package/fig-lab.js
ADDED
|
@@ -0,0 +1,1922 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FigUI3 Lab — Experimental components
|
|
3
|
+
*
|
|
4
|
+
* These components are unstable and may change or be removed without notice.
|
|
5
|
+
* Import alongside fig.js for opt-in access:
|
|
6
|
+
*
|
|
7
|
+
* <script src="fig.js"></script>
|
|
8
|
+
* <script src="fig-lab.js"></script>
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* Field + Slider wrapper */
|
|
12
|
+
class FigFieldSlider extends HTMLElement {
|
|
13
|
+
#field = null;
|
|
14
|
+
#label = null;
|
|
15
|
+
#slider = null;
|
|
16
|
+
#observer = null;
|
|
17
|
+
#managedSliderAttrs = new Set();
|
|
18
|
+
#steppersSyncFrame = 0;
|
|
19
|
+
#boundHandleSliderInput = null;
|
|
20
|
+
#boundHandleSliderChange = null;
|
|
21
|
+
#ignoredSliderAttrs = new Set(["variant", "color", "text", "full"]);
|
|
22
|
+
|
|
23
|
+
static get observedAttributes() {
|
|
24
|
+
return ["label", "direction"];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
connectedCallback() {
|
|
28
|
+
if (!this.#field) {
|
|
29
|
+
this.#initialize();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.#syncField();
|
|
33
|
+
this.#syncSliderAttributes();
|
|
34
|
+
this.#bindSliderEvents();
|
|
35
|
+
|
|
36
|
+
if (!this.#observer) {
|
|
37
|
+
this.#observer = new MutationObserver((mutations) => {
|
|
38
|
+
let syncField = false;
|
|
39
|
+
let syncSlider = false;
|
|
40
|
+
|
|
41
|
+
for (const mutation of mutations) {
|
|
42
|
+
if (mutation.type === "attributes") {
|
|
43
|
+
if (
|
|
44
|
+
mutation.attributeName &&
|
|
45
|
+
this.#ignoredSliderAttrs.has(mutation.attributeName)
|
|
46
|
+
) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (
|
|
50
|
+
mutation.attributeName === "label" ||
|
|
51
|
+
mutation.attributeName === "direction"
|
|
52
|
+
) {
|
|
53
|
+
syncField = true;
|
|
54
|
+
} else {
|
|
55
|
+
syncSlider = true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (syncField) this.#syncField();
|
|
61
|
+
if (syncSlider) this.#syncSliderAttributes();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.#observer.observe(this, { attributes: true });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
disconnectedCallback() {
|
|
69
|
+
this.#observer?.disconnect();
|
|
70
|
+
if (this.#steppersSyncFrame) {
|
|
71
|
+
cancelAnimationFrame(this.#steppersSyncFrame);
|
|
72
|
+
this.#steppersSyncFrame = 0;
|
|
73
|
+
}
|
|
74
|
+
this.#unbindSliderEvents();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
78
|
+
if (oldValue === newValue || !this.#field) return;
|
|
79
|
+
if (name === "label" || name === "direction") {
|
|
80
|
+
this.#syncField();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#initialize() {
|
|
85
|
+
const initialChildren = Array.from(this.childNodes).filter((node) => {
|
|
86
|
+
return (
|
|
87
|
+
node.nodeType !== Node.TEXT_NODE || Boolean(node.textContent?.trim())
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const field = document.createElement("fig-field");
|
|
92
|
+
const label = document.createElement("label");
|
|
93
|
+
const slider = document.createElement("fig-slider");
|
|
94
|
+
slider.setAttribute("text", "true");
|
|
95
|
+
for (const attrName of this.#getForwardedSliderAttrNames()) {
|
|
96
|
+
const value = this.getAttribute(attrName);
|
|
97
|
+
slider.setAttribute(attrName, value ?? "");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
field.append(label, slider);
|
|
101
|
+
|
|
102
|
+
this.#field = field;
|
|
103
|
+
this.#label = label;
|
|
104
|
+
this.#slider = slider;
|
|
105
|
+
|
|
106
|
+
this.replaceChildren(field);
|
|
107
|
+
|
|
108
|
+
for (const node of initialChildren) {
|
|
109
|
+
this.#slider.appendChild(node);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#syncField() {
|
|
114
|
+
if (!this.#field || !this.#label) return;
|
|
115
|
+
const hasLabelAttr = this.hasAttribute("label");
|
|
116
|
+
const rawLabel = this.getAttribute("label");
|
|
117
|
+
const isBlankLabel = hasLabelAttr && (rawLabel ?? "").trim() === "";
|
|
118
|
+
|
|
119
|
+
if (isBlankLabel) {
|
|
120
|
+
if (this.#label.parentElement === this.#field) {
|
|
121
|
+
this.#label.remove();
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
this.#label.textContent = hasLabelAttr ? (rawLabel ?? "") : "Label";
|
|
125
|
+
if (this.#label.parentElement !== this.#field) {
|
|
126
|
+
this.#field.prepend(this.#label);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.#field.setAttribute(
|
|
131
|
+
"direction",
|
|
132
|
+
this.getAttribute("direction") || "horizontal",
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#syncSliderAttributes() {
|
|
137
|
+
if (!this.#slider) return;
|
|
138
|
+
const hostAttrs = this.#getForwardedSliderAttrNames();
|
|
139
|
+
|
|
140
|
+
const nextManaged = new Set(hostAttrs.filter((name) => name !== "text"));
|
|
141
|
+
|
|
142
|
+
for (const attrName of this.#managedSliderAttrs) {
|
|
143
|
+
if (!nextManaged.has(attrName)) {
|
|
144
|
+
this.#slider.removeAttribute(attrName);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
for (const attrName of hostAttrs) {
|
|
149
|
+
if (attrName === "text") continue;
|
|
150
|
+
const value = this.getAttribute(attrName);
|
|
151
|
+
this.#slider.setAttribute(attrName, value ?? "");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.#slider.removeAttribute("variant");
|
|
155
|
+
this.#slider.removeAttribute("color");
|
|
156
|
+
this.#slider.removeAttribute("transform");
|
|
157
|
+
this.#slider.removeAttribute("full");
|
|
158
|
+
this.#slider.setAttribute("text", "true");
|
|
159
|
+
|
|
160
|
+
const sliderType = (this.getAttribute("type") || "range").toLowerCase();
|
|
161
|
+
if (sliderType === "delta" || sliderType === "stepper") {
|
|
162
|
+
this.#slider.setAttribute(
|
|
163
|
+
"default",
|
|
164
|
+
this.getAttribute("default") ?? "50",
|
|
165
|
+
);
|
|
166
|
+
} else if (!this.hasAttribute("default")) {
|
|
167
|
+
this.#slider.removeAttribute("default");
|
|
168
|
+
}
|
|
169
|
+
if (sliderType === "stepper") {
|
|
170
|
+
this.#slider.setAttribute("step", this.getAttribute("step") ?? "10");
|
|
171
|
+
} else if (!this.hasAttribute("step")) {
|
|
172
|
+
this.#slider.removeAttribute("step");
|
|
173
|
+
}
|
|
174
|
+
if (sliderType === "opacity") {
|
|
175
|
+
this.#slider.style.setProperty(
|
|
176
|
+
"--color",
|
|
177
|
+
"var(--figma-color-bg-tertiary)",
|
|
178
|
+
);
|
|
179
|
+
} else {
|
|
180
|
+
this.#slider.style.removeProperty("--color");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.#managedSliderAttrs = nextManaged;
|
|
184
|
+
this.#queueSteppersSync();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#getForwardedSliderAttrNames() {
|
|
188
|
+
const reserved = new Set([
|
|
189
|
+
"label",
|
|
190
|
+
"direction",
|
|
191
|
+
"oninput",
|
|
192
|
+
"onchange",
|
|
193
|
+
"steppers",
|
|
194
|
+
]);
|
|
195
|
+
return this.getAttributeNames().filter(
|
|
196
|
+
(name) => !reserved.has(name) && !this.#ignoredSliderAttrs.has(name),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#queueSteppersSync() {
|
|
201
|
+
if (this.#steppersSyncFrame) {
|
|
202
|
+
cancelAnimationFrame(this.#steppersSyncFrame);
|
|
203
|
+
}
|
|
204
|
+
this.#steppersSyncFrame = requestAnimationFrame(() => {
|
|
205
|
+
this.#steppersSyncFrame = 0;
|
|
206
|
+
this.#syncSteppersToNumberInput();
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
#syncSteppersToNumberInput() {
|
|
211
|
+
if (!this.#slider) return;
|
|
212
|
+
const numberInput = this.#slider.querySelector("fig-input-number");
|
|
213
|
+
if (!numberInput) return;
|
|
214
|
+
|
|
215
|
+
const hasSteppers =
|
|
216
|
+
this.hasAttribute("steppers") &&
|
|
217
|
+
this.getAttribute("steppers") !== "false";
|
|
218
|
+
if (!hasSteppers) {
|
|
219
|
+
numberInput.removeAttribute("steppers");
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const steppersValue = this.getAttribute("steppers");
|
|
224
|
+
numberInput.setAttribute("steppers", steppersValue ?? "");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#bindSliderEvents() {
|
|
228
|
+
if (!this.#slider) return;
|
|
229
|
+
if (!this.#boundHandleSliderInput) {
|
|
230
|
+
this.#boundHandleSliderInput = this.#forwardSliderEvent.bind(
|
|
231
|
+
this,
|
|
232
|
+
"input",
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (!this.#boundHandleSliderChange) {
|
|
236
|
+
this.#boundHandleSliderChange = this.#forwardSliderEvent.bind(
|
|
237
|
+
this,
|
|
238
|
+
"change",
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
this.#slider.addEventListener("input", this.#boundHandleSliderInput);
|
|
242
|
+
this.#slider.addEventListener("change", this.#boundHandleSliderChange);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#unbindSliderEvents() {
|
|
246
|
+
if (!this.#slider) return;
|
|
247
|
+
if (this.#boundHandleSliderInput) {
|
|
248
|
+
this.#slider.removeEventListener("input", this.#boundHandleSliderInput);
|
|
249
|
+
}
|
|
250
|
+
if (this.#boundHandleSliderChange) {
|
|
251
|
+
this.#slider.removeEventListener("change", this.#boundHandleSliderChange);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
#forwardSliderEvent(type, event) {
|
|
256
|
+
event.stopPropagation();
|
|
257
|
+
const detail =
|
|
258
|
+
event instanceof CustomEvent && event.detail !== undefined
|
|
259
|
+
? event.detail
|
|
260
|
+
: this.#slider?.value;
|
|
261
|
+
this.dispatchEvent(
|
|
262
|
+
new CustomEvent(type, {
|
|
263
|
+
detail,
|
|
264
|
+
bubbles: true,
|
|
265
|
+
cancelable: true,
|
|
266
|
+
composed: true,
|
|
267
|
+
}),
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
customElements.define("fig-field-slider", FigFieldSlider);
|
|
272
|
+
|
|
273
|
+
/* Canvas Control */
|
|
274
|
+
class FigCanvasControl extends HTMLElement {
|
|
275
|
+
static observedAttributes = [
|
|
276
|
+
"type",
|
|
277
|
+
"value",
|
|
278
|
+
"color",
|
|
279
|
+
"name",
|
|
280
|
+
"tooltips",
|
|
281
|
+
"disabled",
|
|
282
|
+
"drag-surface",
|
|
283
|
+
"snapping",
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
#x = 50;
|
|
287
|
+
#y = 50;
|
|
288
|
+
#x2 = 75;
|
|
289
|
+
#y2 = 75;
|
|
290
|
+
#radius = 0;
|
|
291
|
+
#radiusIsPercent = false;
|
|
292
|
+
#angle = 0;
|
|
293
|
+
#pointHandle = null;
|
|
294
|
+
#secondHandle = null;
|
|
295
|
+
#angleHandle = null;
|
|
296
|
+
#radiusSvg = null;
|
|
297
|
+
#angleSvg = null;
|
|
298
|
+
#pointTooltip = null;
|
|
299
|
+
#secondTooltip = null;
|
|
300
|
+
#radiusTooltip = null;
|
|
301
|
+
#angleTooltip = null;
|
|
302
|
+
#isDragging = false;
|
|
303
|
+
#isSecondDragging = false;
|
|
304
|
+
#isRadiusDragging = false;
|
|
305
|
+
#isAngleDragging = false;
|
|
306
|
+
#prevBodyCursor = "";
|
|
307
|
+
|
|
308
|
+
get #type() {
|
|
309
|
+
return this.getAttribute("type") || "point";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
get #hasRadius() {
|
|
313
|
+
return this.#type === "point-radius" || this.#type === "point-radius-angle";
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
get #hasAngle() {
|
|
317
|
+
return this.#type === "point-radius-angle";
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
get #hasSecondPoint() {
|
|
321
|
+
return this.#type === "point-point";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
get #hasLine() {
|
|
325
|
+
return this.#type === "point-radius-angle" || this.#type === "point-point";
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
get #tooltipsEnabled() {
|
|
329
|
+
const v = this.getAttribute("tooltips");
|
|
330
|
+
return v === null || v !== "false";
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
get #snappingMode() {
|
|
334
|
+
const raw = this.getAttribute("snapping");
|
|
335
|
+
if (raw === null) return "false";
|
|
336
|
+
const n = raw.trim().toLowerCase();
|
|
337
|
+
if (n === "modifier") return "modifier";
|
|
338
|
+
if (n === "" || n === "true") return "true";
|
|
339
|
+
return "false";
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
#shouldSnap(shiftKey) {
|
|
343
|
+
const mode = this.#snappingMode;
|
|
344
|
+
if (mode === "true") return true;
|
|
345
|
+
if (mode === "modifier") return !!shiftKey;
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
get #pointTipText() {
|
|
350
|
+
const name = this.getAttribute("name");
|
|
351
|
+
if (name) {
|
|
352
|
+
const parts = name.split(",");
|
|
353
|
+
return parts[0].trim();
|
|
354
|
+
}
|
|
355
|
+
return `${Math.round(this.#x)}%, ${Math.round(this.#y)}%`;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
get #secondTipText() {
|
|
359
|
+
const name = this.getAttribute("name");
|
|
360
|
+
if (name) {
|
|
361
|
+
const parts = name.split(",");
|
|
362
|
+
if (parts.length > 1) return parts[1].trim();
|
|
363
|
+
}
|
|
364
|
+
return `${Math.round(this.#x2)}%, ${Math.round(this.#y2)}%`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
get #dragSurface() {
|
|
368
|
+
return this.getAttribute("drag-surface") || "parent";
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
get #container() {
|
|
372
|
+
const surface = this.#dragSurface;
|
|
373
|
+
if (surface === "parent") return this.parentElement;
|
|
374
|
+
return this.closest(surface);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
get #handleDragSurface() {
|
|
378
|
+
const surface = this.#dragSurface;
|
|
379
|
+
if (surface === "parent") {
|
|
380
|
+
const container = this.parentElement;
|
|
381
|
+
if (container) {
|
|
382
|
+
container.setAttribute("data-fig-canvas-control-surface", "");
|
|
383
|
+
return "[data-fig-canvas-control-surface]";
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return surface;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
#resolveRadius(containerWidth) {
|
|
390
|
+
if (this.#radiusIsPercent) return (this.#radius / 100) * containerWidth;
|
|
391
|
+
return this.#radius;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
#formatRadius() {
|
|
395
|
+
if (this.#radiusIsPercent) return `Radius ${Math.round(this.#radius)}%`;
|
|
396
|
+
return `Radius ${Math.round(this.#radius)}`;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
connectedCallback() {
|
|
400
|
+
this.#parseValue();
|
|
401
|
+
this.#render();
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
disconnectedCallback() {
|
|
405
|
+
this.#teardownRadiusDrag();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
attributeChangedCallback(name, oldVal, newVal) {
|
|
409
|
+
if (oldVal === newVal) return;
|
|
410
|
+
if (
|
|
411
|
+
name === "value" &&
|
|
412
|
+
!this.#isDragging &&
|
|
413
|
+
!this.#isSecondDragging &&
|
|
414
|
+
!this.#isRadiusDragging &&
|
|
415
|
+
!this.#isAngleDragging
|
|
416
|
+
) {
|
|
417
|
+
this.#parseValue();
|
|
418
|
+
if (this.#pointHandle) this.#syncPositions();
|
|
419
|
+
else this.#render();
|
|
420
|
+
}
|
|
421
|
+
if (name === "type") {
|
|
422
|
+
this.#parseValue();
|
|
423
|
+
this.#render();
|
|
424
|
+
}
|
|
425
|
+
if (name === "color" && this.#pointHandle) {
|
|
426
|
+
if (newVal) this.#pointHandle.setAttribute("color", newVal);
|
|
427
|
+
else this.#pointHandle.removeAttribute("color");
|
|
428
|
+
}
|
|
429
|
+
if (name === "disabled") {
|
|
430
|
+
this.#render();
|
|
431
|
+
}
|
|
432
|
+
if (name === "tooltips") {
|
|
433
|
+
this.#render();
|
|
434
|
+
}
|
|
435
|
+
if (name === "snapping" && this.#pointHandle) {
|
|
436
|
+
this.#pointHandle.setAttribute("drag-snapping", newVal || "false");
|
|
437
|
+
if (this.#secondHandle)
|
|
438
|
+
this.#secondHandle.setAttribute("drag-snapping", newVal || "false");
|
|
439
|
+
}
|
|
440
|
+
if (name === "name") {
|
|
441
|
+
if (this.#pointTooltip)
|
|
442
|
+
this.#pointTooltip.setAttribute("text", this.#pointTipText);
|
|
443
|
+
if (this.#secondTooltip)
|
|
444
|
+
this.#secondTooltip.setAttribute("text", this.#secondTipText);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
#parseValue() {
|
|
449
|
+
const raw = this.getAttribute("value");
|
|
450
|
+
if (!raw) return;
|
|
451
|
+
try {
|
|
452
|
+
const v = JSON.parse(raw);
|
|
453
|
+
if (typeof v.x === "number") this.#x = v.x;
|
|
454
|
+
if (typeof v.y === "number") this.#y = v.y;
|
|
455
|
+
if (v.radius !== undefined) {
|
|
456
|
+
const rs = String(v.radius);
|
|
457
|
+
if (rs.endsWith("%")) {
|
|
458
|
+
this.#radiusIsPercent = true;
|
|
459
|
+
this.#radius = parseFloat(rs);
|
|
460
|
+
} else {
|
|
461
|
+
this.#radiusIsPercent = false;
|
|
462
|
+
this.#radius = parseFloat(rs);
|
|
463
|
+
}
|
|
464
|
+
if (!Number.isFinite(this.#radius)) this.#radius = 0;
|
|
465
|
+
}
|
|
466
|
+
if (typeof v.angle === "number") this.#angle = v.angle;
|
|
467
|
+
if (typeof v.x2 === "number") this.#x2 = v.x2;
|
|
468
|
+
if (typeof v.y2 === "number") this.#y2 = v.y2;
|
|
469
|
+
} catch {
|
|
470
|
+
/* ignore */
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get value() {
|
|
475
|
+
const v = { x: this.#x, y: this.#y };
|
|
476
|
+
if (this.#type === "color") {
|
|
477
|
+
const color =
|
|
478
|
+
this.getAttribute("color") || this.#pointHandle?.getAttribute("color");
|
|
479
|
+
if (color) v.color = color;
|
|
480
|
+
}
|
|
481
|
+
if (this.#hasRadius) {
|
|
482
|
+
v.radius = this.#radiusIsPercent ? `${this.#radius}%` : this.#radius;
|
|
483
|
+
}
|
|
484
|
+
if (this.#hasAngle) v.angle = this.#angle;
|
|
485
|
+
if (this.#hasSecondPoint) {
|
|
486
|
+
v.x2 = this.#x2;
|
|
487
|
+
v.y2 = this.#y2;
|
|
488
|
+
}
|
|
489
|
+
return v;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
set value(val) {
|
|
493
|
+
if (typeof val === "object") {
|
|
494
|
+
this.setAttribute("value", JSON.stringify(val));
|
|
495
|
+
} else if (typeof val === "string") {
|
|
496
|
+
this.setAttribute("value", val);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
#render() {
|
|
501
|
+
this.innerHTML = "";
|
|
502
|
+
this.#pointHandle = null;
|
|
503
|
+
this.#secondHandle = null;
|
|
504
|
+
this.#angleHandle = null;
|
|
505
|
+
this.#radiusSvg = null;
|
|
506
|
+
this.#angleSvg = null;
|
|
507
|
+
this.#pointTooltip = null;
|
|
508
|
+
this.#secondTooltip = null;
|
|
509
|
+
this.#radiusTooltip = null;
|
|
510
|
+
this.#angleTooltip = null;
|
|
511
|
+
|
|
512
|
+
const disabled = this.hasAttribute("disabled");
|
|
513
|
+
const type = this.#type;
|
|
514
|
+
const tooltips = this.#tooltipsEnabled;
|
|
515
|
+
|
|
516
|
+
const handleSurface = this.#handleDragSurface;
|
|
517
|
+
|
|
518
|
+
const handle = document.createElement("fig-handle");
|
|
519
|
+
handle.setAttribute("drag", "true");
|
|
520
|
+
handle.setAttribute("drag-surface", handleSurface);
|
|
521
|
+
handle.setAttribute("drag-axes", "x,y");
|
|
522
|
+
handle.setAttribute("drag-snapping", this.#snappingMode);
|
|
523
|
+
handle.setAttribute("value", `${this.#x}% ${this.#y}%`);
|
|
524
|
+
if (disabled) handle.setAttribute("disabled", "");
|
|
525
|
+
if (type === "color") {
|
|
526
|
+
handle.setAttribute("type", "color");
|
|
527
|
+
const color = this.getAttribute("color");
|
|
528
|
+
if (color) handle.setAttribute("color", color);
|
|
529
|
+
} else {
|
|
530
|
+
handle.setAttribute("type", "canvas");
|
|
531
|
+
}
|
|
532
|
+
if (this.#hasSecondPoint) {
|
|
533
|
+
handle.setAttribute("hit-area", "12 circle");
|
|
534
|
+
handle.setAttribute("hit-area-mode", "delegate");
|
|
535
|
+
}
|
|
536
|
+
this.#pointHandle = handle;
|
|
537
|
+
|
|
538
|
+
if (this.#hasRadius) {
|
|
539
|
+
this.#createRadiusSvg();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (this.#hasLine) {
|
|
543
|
+
this.#createAngleSvg();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (tooltips) {
|
|
547
|
+
const tip = document.createElement("fig-tooltip");
|
|
548
|
+
tip.setAttribute("action", "manual");
|
|
549
|
+
tip.setAttribute("theme", "canvas");
|
|
550
|
+
tip.setAttribute("pointer", "false");
|
|
551
|
+
tip.setAttribute("text", this.#pointTipText);
|
|
552
|
+
tip.appendChild(handle);
|
|
553
|
+
this.appendChild(tip);
|
|
554
|
+
this.#pointTooltip = tip;
|
|
555
|
+
} else {
|
|
556
|
+
this.appendChild(handle);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (this.#hasAngle) {
|
|
560
|
+
this.#createAngleHandle(disabled, tooltips, handleSurface);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (this.#hasSecondPoint) {
|
|
564
|
+
this.#createSecondHandle(disabled, tooltips, handleSurface);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
this.#setupEventListeners();
|
|
568
|
+
this.#wireHoverTooltips();
|
|
569
|
+
requestAnimationFrame(() => this.#syncPositions());
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
#wireHoverTooltip(target, getTooltip, getText, isDraggingRef) {
|
|
573
|
+
if (!target) return;
|
|
574
|
+
const show = () => {
|
|
575
|
+
if (isDraggingRef && isDraggingRef()) return;
|
|
576
|
+
const tip = getTooltip();
|
|
577
|
+
if (!tip) return;
|
|
578
|
+
if (getText) tip.setAttribute("text", getText());
|
|
579
|
+
tip.setAttribute("show", "true");
|
|
580
|
+
tip.showPopup?.();
|
|
581
|
+
};
|
|
582
|
+
const hide = () => {
|
|
583
|
+
if (isDraggingRef && isDraggingRef()) return;
|
|
584
|
+
const tip = getTooltip();
|
|
585
|
+
if (!tip) return;
|
|
586
|
+
tip.removeAttribute("show");
|
|
587
|
+
};
|
|
588
|
+
target.addEventListener("pointerenter", show);
|
|
589
|
+
target.addEventListener("pointerleave", hide);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
#wireHoverTooltips() {
|
|
593
|
+
if (this.#pointHandle) {
|
|
594
|
+
this.#wireHoverTooltip(
|
|
595
|
+
this.#pointHandle,
|
|
596
|
+
() => this.#pointTooltip,
|
|
597
|
+
() => this.#pointTipText,
|
|
598
|
+
() =>
|
|
599
|
+
this.#isDragging ||
|
|
600
|
+
!!this.#pointHandle?.querySelector("fig-color-tip"),
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
if (this.#angleHandle) {
|
|
604
|
+
this.#wireHoverTooltip(
|
|
605
|
+
this.#angleHandle,
|
|
606
|
+
() => this.#angleTooltip,
|
|
607
|
+
() => `Angle ${Math.round(this.#angle)}°`,
|
|
608
|
+
() => this.#isAngleDragging || this.#isRadiusDragging,
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
if (this.#secondHandle) {
|
|
612
|
+
this.#wireHoverTooltip(
|
|
613
|
+
this.#secondHandle,
|
|
614
|
+
() => this.#secondTooltip,
|
|
615
|
+
() => this.#secondTipText,
|
|
616
|
+
() => this.#isSecondDragging,
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
if (this.#radiusSvg) {
|
|
620
|
+
const hit = this.#radiusSvg.querySelector(
|
|
621
|
+
".fig-canvas-control-radius-hit",
|
|
622
|
+
);
|
|
623
|
+
this.#wireRadiusHoverTooltip(hit || this.#radiusSvg);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (this.#type === "color" && this.#pointHandle && this.#pointTooltip) {
|
|
627
|
+
const obs = new MutationObserver(() => {
|
|
628
|
+
if (this.#pointHandle?.querySelector("fig-color-tip")) {
|
|
629
|
+
this.#pointTooltip?.removeAttribute("show");
|
|
630
|
+
this.#pointTooltip?.hidePopup?.();
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
obs.observe(this.#pointHandle, { childList: true, subtree: true });
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
#setRadiusTooltipAnchorAt(clientX, clientY) {
|
|
638
|
+
const tip = this.#radiusTooltip;
|
|
639
|
+
if (!tip?.popup) return;
|
|
640
|
+
const y = clientY - 8;
|
|
641
|
+
tip.popup.anchor = {
|
|
642
|
+
getBoundingClientRect: () => ({
|
|
643
|
+
left: clientX,
|
|
644
|
+
top: y,
|
|
645
|
+
right: clientX,
|
|
646
|
+
bottom: y,
|
|
647
|
+
width: 0,
|
|
648
|
+
height: 0,
|
|
649
|
+
x: clientX,
|
|
650
|
+
y,
|
|
651
|
+
}),
|
|
652
|
+
};
|
|
653
|
+
tip.popup.queueReposition?.();
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
#wireRadiusHoverTooltip(target) {
|
|
657
|
+
if (!target) return;
|
|
658
|
+
target.addEventListener("pointerenter", (e) => {
|
|
659
|
+
const tip = this.#radiusTooltip;
|
|
660
|
+
if (!tip) return;
|
|
661
|
+
tip.setAttribute("text", this.#formatRadius());
|
|
662
|
+
tip.setAttribute("show", "true");
|
|
663
|
+
tip.showPopup?.();
|
|
664
|
+
this.#setRadiusTooltipAnchorAt(e.clientX, e.clientY);
|
|
665
|
+
});
|
|
666
|
+
target.addEventListener("pointermove", (e) => {
|
|
667
|
+
if (this.#isRadiusDragging) return;
|
|
668
|
+
this.#setRadiusTooltipAnchorAt(e.clientX, e.clientY);
|
|
669
|
+
});
|
|
670
|
+
target.addEventListener("pointerleave", () => {
|
|
671
|
+
if (this.#isRadiusDragging) return;
|
|
672
|
+
const tip = this.#radiusTooltip;
|
|
673
|
+
if (!tip) return;
|
|
674
|
+
tip.removeAttribute("show");
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
#createRadiusSvg() {
|
|
679
|
+
const ns = "http://www.w3.org/2000/svg";
|
|
680
|
+
const svg = document.createElementNS(ns, "svg");
|
|
681
|
+
svg.classList.add("fig-canvas-control-radius");
|
|
682
|
+
svg.setAttribute("overflow", "visible");
|
|
683
|
+
const hitCircle = document.createElementNS(ns, "circle");
|
|
684
|
+
hitCircle.classList.add("fig-canvas-control-radius-hit");
|
|
685
|
+
svg.appendChild(hitCircle);
|
|
686
|
+
const haloCircle = document.createElementNS(ns, "circle");
|
|
687
|
+
haloCircle.classList.add("fig-canvas-control-radius-halo");
|
|
688
|
+
svg.appendChild(haloCircle);
|
|
689
|
+
const circle = document.createElementNS(ns, "circle");
|
|
690
|
+
svg.appendChild(circle);
|
|
691
|
+
this.#radiusSvg = svg;
|
|
692
|
+
|
|
693
|
+
if (this.#tooltipsEnabled) {
|
|
694
|
+
const tip = document.createElement("fig-tooltip");
|
|
695
|
+
tip.setAttribute("action", "manual");
|
|
696
|
+
tip.setAttribute("theme", "canvas");
|
|
697
|
+
tip.setAttribute("pointer", "false");
|
|
698
|
+
tip.setAttribute("text", this.#formatRadius());
|
|
699
|
+
tip.appendChild(svg);
|
|
700
|
+
this.appendChild(tip);
|
|
701
|
+
this.#radiusTooltip = tip;
|
|
702
|
+
} else {
|
|
703
|
+
this.appendChild(svg);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
this.#setupRadiusDrag(hitCircle);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
#createAngleSvg() {
|
|
710
|
+
const ns = "http://www.w3.org/2000/svg";
|
|
711
|
+
const svg = document.createElementNS(ns, "svg");
|
|
712
|
+
svg.classList.add("fig-canvas-control-angle-svg");
|
|
713
|
+
svg.setAttribute("overflow", "visible");
|
|
714
|
+
svg.style.position = "absolute";
|
|
715
|
+
svg.style.pointerEvents = "none";
|
|
716
|
+
if (this.#hasSecondPoint) {
|
|
717
|
+
const hitLine = document.createElementNS(ns, "line");
|
|
718
|
+
hitLine.classList.add("fig-canvas-control-angle-line-hit");
|
|
719
|
+
hitLine.setAttribute("stroke", "transparent");
|
|
720
|
+
hitLine.setAttribute("stroke-width", "12");
|
|
721
|
+
hitLine.setAttribute("stroke-linecap", "round");
|
|
722
|
+
hitLine.style.pointerEvents = "stroke";
|
|
723
|
+
hitLine.style.cursor = "move";
|
|
724
|
+
svg.appendChild(hitLine);
|
|
725
|
+
this.#setupLineDrag(hitLine);
|
|
726
|
+
}
|
|
727
|
+
const haloLine = document.createElementNS(ns, "line");
|
|
728
|
+
haloLine.classList.add("fig-canvas-control-angle-line-halo");
|
|
729
|
+
svg.appendChild(haloLine);
|
|
730
|
+
const line = document.createElementNS(ns, "line");
|
|
731
|
+
line.classList.add("fig-canvas-control-angle-line");
|
|
732
|
+
svg.appendChild(line);
|
|
733
|
+
this.#angleSvg = svg;
|
|
734
|
+
this.appendChild(svg);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
#setupLineDrag(hitLine) {
|
|
738
|
+
hitLine.addEventListener("pointerdown", (e) => {
|
|
739
|
+
if (this.hasAttribute("disabled")) return;
|
|
740
|
+
e.preventDefault();
|
|
741
|
+
e.stopPropagation();
|
|
742
|
+
const container = this.#container;
|
|
743
|
+
if (!container) return;
|
|
744
|
+
const rect0 = container.getBoundingClientRect();
|
|
745
|
+
const startX = e.clientX;
|
|
746
|
+
const startY = e.clientY;
|
|
747
|
+
const x0 = this.#x;
|
|
748
|
+
const y0 = this.#y;
|
|
749
|
+
const x20 = this.#x2;
|
|
750
|
+
const y20 = this.#y2;
|
|
751
|
+
this.#isDragging = true;
|
|
752
|
+
this.#isSecondDragging = true;
|
|
753
|
+
const prevBodyCursor = document.body.style.cursor;
|
|
754
|
+
document.body.style.cursor = "move";
|
|
755
|
+
hitLine.style.pointerEvents = "none";
|
|
756
|
+
|
|
757
|
+
const onMove = (ev) => {
|
|
758
|
+
const rect = container.getBoundingClientRect();
|
|
759
|
+
const dxPctRaw =
|
|
760
|
+
rect.width > 0 ? ((ev.clientX - startX) / rect.width) * 100 : 0;
|
|
761
|
+
const dyPctRaw =
|
|
762
|
+
rect.height > 0 ? ((ev.clientY - startY) / rect.height) * 100 : 0;
|
|
763
|
+
const minDx = -Math.min(x0, x20);
|
|
764
|
+
const maxDx = 100 - Math.max(x0, x20);
|
|
765
|
+
const minDy = -Math.min(y0, y20);
|
|
766
|
+
const maxDy = 100 - Math.max(y0, y20);
|
|
767
|
+
const dxPct = Math.max(minDx, Math.min(maxDx, dxPctRaw));
|
|
768
|
+
const dyPct = Math.max(minDy, Math.min(maxDy, dyPctRaw));
|
|
769
|
+
this.#x = x0 + dxPct;
|
|
770
|
+
this.#y = y0 + dyPct;
|
|
771
|
+
this.#x2 = x20 + dxPct;
|
|
772
|
+
this.#y2 = y20 + dyPct;
|
|
773
|
+
this.#syncPositions();
|
|
774
|
+
this.#emitInput();
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
const onUp = () => {
|
|
778
|
+
document.body.style.cursor = prevBodyCursor;
|
|
779
|
+
hitLine.style.pointerEvents = "stroke";
|
|
780
|
+
this.#syncValueAttribute();
|
|
781
|
+
this.#emitChange();
|
|
782
|
+
window.removeEventListener("pointermove", onMove);
|
|
783
|
+
window.removeEventListener("pointerup", onUp);
|
|
784
|
+
requestAnimationFrame(() => {
|
|
785
|
+
this.#isDragging = false;
|
|
786
|
+
this.#isSecondDragging = false;
|
|
787
|
+
});
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
window.addEventListener("pointermove", onMove);
|
|
791
|
+
window.addEventListener("pointerup", onUp);
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
#createAngleHandle(disabled, tooltips, handleSurface) {
|
|
796
|
+
const handle = document.createElement("fig-handle");
|
|
797
|
+
handle.setAttribute("type", "canvas");
|
|
798
|
+
handle.setAttribute("drag", "true");
|
|
799
|
+
handle.setAttribute("drag-surface", handleSurface);
|
|
800
|
+
handle.setAttribute("drag-axes", "x,y");
|
|
801
|
+
handle.setAttribute("size", "small");
|
|
802
|
+
handle.setAttribute("hit-area", "12 circle");
|
|
803
|
+
handle.setAttribute("hit-area-mode", "delegate");
|
|
804
|
+
if (disabled) handle.setAttribute("disabled", "");
|
|
805
|
+
this.#angleHandle = handle;
|
|
806
|
+
|
|
807
|
+
if (tooltips) {
|
|
808
|
+
const tip = document.createElement("fig-tooltip");
|
|
809
|
+
tip.setAttribute("action", "manual");
|
|
810
|
+
tip.setAttribute("theme", "canvas");
|
|
811
|
+
tip.setAttribute("pointer", "false");
|
|
812
|
+
tip.setAttribute("text", `${Math.round(this.#angle)}°`);
|
|
813
|
+
tip.appendChild(handle);
|
|
814
|
+
this.appendChild(tip);
|
|
815
|
+
this.#angleTooltip = tip;
|
|
816
|
+
} else {
|
|
817
|
+
this.appendChild(handle);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
#createSecondHandle(disabled, tooltips, handleSurface) {
|
|
822
|
+
const handle = document.createElement("fig-handle");
|
|
823
|
+
handle.setAttribute("type", "canvas");
|
|
824
|
+
handle.setAttribute("drag", "true");
|
|
825
|
+
handle.setAttribute("drag-surface", handleSurface);
|
|
826
|
+
handle.setAttribute("drag-axes", "x,y");
|
|
827
|
+
handle.setAttribute("drag-snapping", this.#snappingMode);
|
|
828
|
+
handle.setAttribute("hit-area", "12 circle");
|
|
829
|
+
handle.setAttribute("hit-area-mode", "delegate");
|
|
830
|
+
handle.setAttribute("value", `${this.#x2}% ${this.#y2}%`);
|
|
831
|
+
if (disabled) handle.setAttribute("disabled", "");
|
|
832
|
+
this.#secondHandle = handle;
|
|
833
|
+
|
|
834
|
+
if (tooltips) {
|
|
835
|
+
const tip = document.createElement("fig-tooltip");
|
|
836
|
+
tip.setAttribute("action", "manual");
|
|
837
|
+
tip.setAttribute("theme", "canvas");
|
|
838
|
+
tip.setAttribute("pointer", "false");
|
|
839
|
+
tip.setAttribute("text", this.#secondTipText);
|
|
840
|
+
tip.appendChild(handle);
|
|
841
|
+
this.appendChild(tip);
|
|
842
|
+
this.#secondTooltip = tip;
|
|
843
|
+
} else {
|
|
844
|
+
this.appendChild(handle);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
#resizeCursorSvg(deg) {
|
|
849
|
+
const r = Math.round(deg);
|
|
850
|
+
return `url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cg transform='rotate(${r} 16 16)'%3E%3Cg filter='url(%23f)'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M11.1212 16.9998L11.5607 17.4394C12.1465 18.0252 12.1464 18.975 11.5606 19.5607C10.9748 20.1465 10.0251 20.1465 9.4393 19.5606L6.4393 16.5604C5.85354 15.9746 5.85357 15.0249 6.43938 14.4391L9.43938 11.4393C10.0252 10.8535 10.9749 10.8536 11.5607 11.4394C12.1465 12.0252 12.1464 12.9749 11.5606 13.5607L11.1215 13.9998L20.8786 13.9999L20.4394 13.5607C19.8536 12.9749 19.8535 12.0252 20.4393 11.4394C21.0251 10.8536 21.9749 10.8536 22.5606 11.4394L25.5606 14.4393C25.842 14.7206 26 15.1021 26 15.4999C26 15.8978 25.842 16.2793 25.5607 16.5606L22.5607 19.5607C21.9749 20.1465 21.0251 20.1465 20.4393 19.5607C19.8536 18.9749 19.8535 18.0252 20.4393 17.4394L20.8788 16.9999L11.1212 16.9998Z' fill='white'/%3E%3C/g%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M10.8536 12.1465C11.0488 12.3417 11.0488 12.6583 10.8535 12.8536L8.70715 14.9998L23.2929 14.9999L21.1465 12.8536C20.9512 12.6583 20.9512 12.3417 21.1464 12.1465C21.3417 11.9512 21.6583 11.9512 21.8535 12.1465L24.8535 15.1464C24.9473 15.2402 25 15.3673 25 15.4999C25 15.6326 24.9473 15.7597 24.8536 15.8535L21.8536 18.8536C21.6583 19.0488 21.3417 19.0488 21.1465 18.8536C20.9512 18.6583 20.9512 18.3417 21.1464 18.1465L23.2929 15.9999L8.70705 15.9998L10.8536 18.1465C11.0488 18.3417 11.0488 18.6583 10.8535 18.8536C10.6583 19.0488 10.3417 19.0488 10.1464 18.8535L7.14643 15.8533C6.95118 15.658 6.95119 15.3415 7.14646 15.1462L10.1465 12.1464C10.3417 11.9512 10.6583 11.9512 10.8536 12.1465Z' fill='black'/%3E%3C/g%3E%3Cdefs%3E%3Cfilter id='f' x='3' y='9' width='26' height='15' filterUnits='userSpaceOnUse' color-interpolation-filters='sRGB'%3E%3CfeFlood flood-opacity='0' result='a'/%3E%3CfeColorMatrix in='SourceAlpha' values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0' result='b'/%3E%3CfeOffset dy='1'/%3E%3CfeGaussianBlur stdDeviation='1.5'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.35 0'/%3E%3CfeBlend in2='a' result='c'/%3E%3CfeBlend in='SourceGraphic' in2='c'/%3E%3C/filter%3E%3C/defs%3E%3C/svg%3E") 16 16, nwse-resize`;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
#rotateCursorSvg(deg) {
|
|
854
|
+
const r = Math.round(deg - 45);
|
|
855
|
+
return `url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cg transform='rotate(${r} 16 16)'%3E%3Cg filter='url(%23f)'%3E%3Cpath d='M12.5607 22.4393L12.0216 21.9002C17.1558 21.2216 21.2216 17.1558 21.9002 12.0216L22.4393 12.5607C23.0251 13.1464 23.9749 13.1464 24.5607 12.5607C25.1464 11.9749 25.1464 11.0251 24.5607 10.4393L21.5607 7.43934C20.9749 6.85355 20.0251 6.85355 19.4393 7.43934L16.4393 10.4393C15.8536 11.0251 15.8536 11.9749 16.4393 12.5607C17.0251 13.1464 17.9749 13.1464 18.5607 12.5607L18.8056 12.3157C18.1013 15.5527 15.5527 18.1013 12.3157 18.8056L12.5607 18.5607C13.1464 17.9749 13.1464 17.0251 12.5607 16.4393C11.9749 15.8536 11.0251 15.8536 10.4393 16.4393L7.43934 19.4393C6.85356 20.0251 6.85356 20.9749 7.43934 21.5607L10.4393 24.5607C11.0251 25.1464 11.9749 25.1464 12.5607 24.5607C13.1464 23.9749 13.1464 23.0251 12.5607 22.4393Z' fill='white'/%3E%3C/g%3E%3Cpath d='M23.8536 11.8536C23.6583 12.0488 23.3417 12.0488 23.1464 11.8536L21 9.70711V10.5C21 16.299 16.299 21 10.5 21H9.70711L11.8536 23.1464C12.0488 23.3417 12.0488 23.6583 11.8536 23.8536C11.6583 24.0488 11.3417 24.0488 11.1464 23.8536L8.14645 20.8536C7.95119 20.6583 7.95119 20.3417 8.14645 20.1464L11.1464 17.1464C11.3417 16.9512 11.6583 16.9512 11.8536 17.1464C12.0488 17.3417 12.0488 17.6583 11.8536 17.8536L9.70711 20H10.5C15.7467 20 20 15.7467 20 10.5V9.70711L17.8536 11.8536C17.6583 12.0488 17.3417 12.0488 17.1464 11.8536C16.9512 11.6583 16.9512 11.3417 17.1464 11.1464L20.1464 8.14645C20.3417 7.95119 20.6583 7.95119 20.8536 8.14645L23.8536 11.1464C24.0488 11.3417 24.0488 11.6583 23.8536 11.8536Z' fill='black'/%3E%3C/g%3E%3Cdefs%3E%3Cfilter id='f' x='4' y='5' width='24' height='24' filterUnits='userSpaceOnUse' color-interpolation-filters='sRGB'%3E%3CfeFlood flood-opacity='0' result='a'/%3E%3CfeColorMatrix in='SourceAlpha' values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0' result='b'/%3E%3CfeOffset dy='1'/%3E%3CfeGaussianBlur stdDeviation='1.5'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.35 0'/%3E%3CfeBlend in2='a' result='c'/%3E%3CfeBlend in='SourceGraphic' in2='c'/%3E%3C/filter%3E%3C/defs%3E%3C/svg%3E") 16 16, pointer`;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
#syncAngleCursor() {
|
|
859
|
+
if (!this.#angleHandle || !this.#hasAngle) return;
|
|
860
|
+
const hitArea = this.#angleHandle.querySelector(".fig-handle-hit-area");
|
|
861
|
+
if (!hitArea) return;
|
|
862
|
+
hitArea.style.cursor = this.#rotateCursorSvg(this.#angle);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
#pointPointLineDeg() {
|
|
866
|
+
return (Math.atan2(this.#y2 - this.#y, this.#x2 - this.#x) * 180) / Math.PI;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
#syncPointPointCursors() {
|
|
870
|
+
if (!this.#hasSecondPoint) return;
|
|
871
|
+
const deg = this.#pointPointLineDeg();
|
|
872
|
+
const setHitCursor = (handle, rotateDeg) => {
|
|
873
|
+
if (!handle) return;
|
|
874
|
+
const hitArea = handle.querySelector(".fig-handle-hit-area");
|
|
875
|
+
if (hitArea) hitArea.style.cursor = this.#rotateCursorSvg(rotateDeg);
|
|
876
|
+
};
|
|
877
|
+
setHitCursor(this.#pointHandle, deg + 180);
|
|
878
|
+
setHitCursor(this.#secondHandle, deg);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
#positionHandle(handle, xPct, yPct, rect) {
|
|
882
|
+
handle.style.setProperty("--fig-handle-position-translate", "-50% -50%");
|
|
883
|
+
handle.style.left = `${(xPct / 100) * rect.width}px`;
|
|
884
|
+
handle.style.top = `${(yPct / 100) * rect.height}px`;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
#syncPositions() {
|
|
888
|
+
const container = this.#container;
|
|
889
|
+
if (!container || !this.#pointHandle) return;
|
|
890
|
+
const rect = container.getBoundingClientRect();
|
|
891
|
+
|
|
892
|
+
this.#positionHandle(this.#pointHandle, this.#x, this.#y, rect);
|
|
893
|
+
|
|
894
|
+
if (this.#radiusSvg) {
|
|
895
|
+
const cx = (this.#x / 100) * rect.width;
|
|
896
|
+
const cy = (this.#y / 100) * rect.height;
|
|
897
|
+
const r = this.#resolveRadius(rect.width);
|
|
898
|
+
const svg = this.#radiusSvg;
|
|
899
|
+
const d = Math.max(r * 2, 1);
|
|
900
|
+
svg.style.position = "absolute";
|
|
901
|
+
svg.style.width = `${d}px`;
|
|
902
|
+
svg.style.height = `${d}px`;
|
|
903
|
+
svg.style.left = `${cx - r}px`;
|
|
904
|
+
svg.style.top = `${cy - r}px`;
|
|
905
|
+
svg.setAttribute("viewBox", `0 0 ${d} ${d}`);
|
|
906
|
+
const circles = svg.querySelectorAll("circle");
|
|
907
|
+
for (const c of circles) {
|
|
908
|
+
c.setAttribute("cx", String(r));
|
|
909
|
+
c.setAttribute("cy", String(r));
|
|
910
|
+
c.setAttribute("r", String(Math.max(r - 1, 0)));
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
if (this.#angleSvg && this.#hasLine) {
|
|
915
|
+
const cx = (this.#x / 100) * rect.width;
|
|
916
|
+
const cy = (this.#y / 100) * rect.height;
|
|
917
|
+
let lx2, ly2;
|
|
918
|
+
if (this.#hasSecondPoint) {
|
|
919
|
+
lx2 = (this.#x2 / 100) * rect.width;
|
|
920
|
+
ly2 = (this.#y2 / 100) * rect.height;
|
|
921
|
+
} else {
|
|
922
|
+
const r = this.#resolveRadius(rect.width);
|
|
923
|
+
const angleRad = (this.#angle * Math.PI) / 180;
|
|
924
|
+
lx2 = cx + r * Math.cos(angleRad);
|
|
925
|
+
ly2 = cy + r * Math.sin(angleRad);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
const svg = this.#angleSvg;
|
|
929
|
+
svg.style.width = `${rect.width}px`;
|
|
930
|
+
svg.style.height = `${rect.height}px`;
|
|
931
|
+
svg.style.left = "0";
|
|
932
|
+
svg.style.top = "0";
|
|
933
|
+
svg.setAttribute("viewBox", `0 0 ${rect.width} ${rect.height}`);
|
|
934
|
+
const lines = svg.querySelectorAll(
|
|
935
|
+
".fig-canvas-control-angle-line, .fig-canvas-control-angle-line-halo",
|
|
936
|
+
);
|
|
937
|
+
for (const line of lines) {
|
|
938
|
+
line.setAttribute("x1", String(cx));
|
|
939
|
+
line.setAttribute("y1", String(cy));
|
|
940
|
+
line.setAttribute("x2", String(lx2));
|
|
941
|
+
line.setAttribute("y2", String(ly2));
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
if (this.#angleHandle && this.#hasAngle) {
|
|
946
|
+
const cx = (this.#x / 100) * rect.width;
|
|
947
|
+
const cy = (this.#y / 100) * rect.height;
|
|
948
|
+
const r = this.#resolveRadius(rect.width);
|
|
949
|
+
const angleRad = (this.#angle * Math.PI) / 180;
|
|
950
|
+
const ax = cx + r * Math.cos(angleRad);
|
|
951
|
+
const ay = cy + r * Math.sin(angleRad);
|
|
952
|
+
const pxPct = rect.width > 0 ? (ax / rect.width) * 100 : 0;
|
|
953
|
+
const pyPct = rect.height > 0 ? (ay / rect.height) * 100 : 0;
|
|
954
|
+
this.#positionHandle(this.#angleHandle, pxPct, pyPct, rect);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
if (this.#secondHandle && this.#hasSecondPoint) {
|
|
958
|
+
this.#positionHandle(this.#secondHandle, this.#x2, this.#y2, rect);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
this.#syncAngleCursor();
|
|
962
|
+
this.#syncPointPointCursors();
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
#emitInput() {
|
|
966
|
+
this.dispatchEvent(
|
|
967
|
+
new CustomEvent("input", { bubbles: true, detail: this.value }),
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
#emitChange() {
|
|
972
|
+
this.dispatchEvent(
|
|
973
|
+
new CustomEvent("change", { bubbles: true, detail: this.value }),
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
#syncValueAttribute() {
|
|
978
|
+
this.setAttribute("value", JSON.stringify(this.value));
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
#setupEventListeners() {
|
|
982
|
+
if (!this.#pointHandle) return;
|
|
983
|
+
|
|
984
|
+
this.#pointHandle.addEventListener("input", (e) => {
|
|
985
|
+
e.stopPropagation();
|
|
986
|
+
if (e.detail?.color) {
|
|
987
|
+
this.setAttribute("color", e.detail.color);
|
|
988
|
+
this.#emitInput();
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
if (!this.#isDragging && this.#hasSecondPoint) {
|
|
992
|
+
this.#prevBodyCursor = document.body.style.cursor;
|
|
993
|
+
}
|
|
994
|
+
this.#isDragging = true;
|
|
995
|
+
const px = e.detail?.px ?? this.#x / 100;
|
|
996
|
+
const py = e.detail?.py ?? this.#y / 100;
|
|
997
|
+
this.#x = Math.round(Math.max(0, Math.min(100, px * 100)));
|
|
998
|
+
this.#y = Math.round(Math.max(0, Math.min(100, py * 100)));
|
|
999
|
+
if (this.#pointTooltip) {
|
|
1000
|
+
this.#pointTooltip.removeAttribute("show");
|
|
1001
|
+
this.#pointTooltip.hidePopup?.();
|
|
1002
|
+
}
|
|
1003
|
+
this.#syncPositions();
|
|
1004
|
+
if (this.#hasSecondPoint) {
|
|
1005
|
+
document.body.style.cursor = this.#resizeCursorSvg(
|
|
1006
|
+
this.#pointPointLineDeg(),
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
this.#emitInput();
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
this.#pointHandle.addEventListener("change", (e) => {
|
|
1013
|
+
e.stopPropagation();
|
|
1014
|
+
if (e.detail?.color) {
|
|
1015
|
+
this.setAttribute("color", e.detail.color);
|
|
1016
|
+
this.#emitChange();
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const px = e.detail?.px ?? this.#x / 100;
|
|
1020
|
+
const py = e.detail?.py ?? this.#y / 100;
|
|
1021
|
+
this.#x = Math.round(Math.max(0, Math.min(100, px * 100)));
|
|
1022
|
+
this.#y = Math.round(Math.max(0, Math.min(100, py * 100)));
|
|
1023
|
+
if (this.#pointTooltip) this.#pointTooltip.removeAttribute("show");
|
|
1024
|
+
if (this.#hasSecondPoint) {
|
|
1025
|
+
document.body.style.cursor = this.#prevBodyCursor ?? "";
|
|
1026
|
+
}
|
|
1027
|
+
this.#syncPositions();
|
|
1028
|
+
this.#syncValueAttribute();
|
|
1029
|
+
this.#emitChange();
|
|
1030
|
+
requestAnimationFrame(() => {
|
|
1031
|
+
this.#isDragging = false;
|
|
1032
|
+
});
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
if (this.#angleHandle) {
|
|
1036
|
+
this.#angleHandle.addEventListener("input", (e) => {
|
|
1037
|
+
e.stopPropagation();
|
|
1038
|
+
this.#isAngleDragging = true;
|
|
1039
|
+
this.classList.add("fig-canvas-control-ring-active");
|
|
1040
|
+
const container = this.#container;
|
|
1041
|
+
if (!container) return;
|
|
1042
|
+
const rect = container.getBoundingClientRect();
|
|
1043
|
+
const cx = (this.#x / 100) * rect.width;
|
|
1044
|
+
const cy = (this.#y / 100) * rect.height;
|
|
1045
|
+
const hx = e.detail?.x ?? 0;
|
|
1046
|
+
const hy = e.detail?.y ?? 0;
|
|
1047
|
+
const hw = this.#angleHandle.offsetWidth / 2;
|
|
1048
|
+
const hh = this.#angleHandle.offsetHeight / 2;
|
|
1049
|
+
const dx = hx + hw - cx;
|
|
1050
|
+
const dy = hy + hh - cy;
|
|
1051
|
+
let angle = (Math.atan2(dy, dx) * 180) / Math.PI;
|
|
1052
|
+
if (this.#shouldSnap(e.detail?.shiftKey)) {
|
|
1053
|
+
angle = Math.round(angle / 15) * 15;
|
|
1054
|
+
}
|
|
1055
|
+
this.#angle = angle;
|
|
1056
|
+
|
|
1057
|
+
let dist = Math.sqrt(dx * dx + dy * dy);
|
|
1058
|
+
if (this.#shouldSnap(e.detail?.shiftKey)) {
|
|
1059
|
+
const step = this.#radiusIsPercent ? 5 : 10;
|
|
1060
|
+
if (this.#radiusIsPercent) {
|
|
1061
|
+
let pct = (dist / rect.width) * 100;
|
|
1062
|
+
pct = Math.round(pct / step) * step;
|
|
1063
|
+
dist = (pct / 100) * rect.width;
|
|
1064
|
+
} else {
|
|
1065
|
+
dist = Math.round(dist / step) * step;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
if (this.#radiusIsPercent) {
|
|
1069
|
+
this.#radius = Math.max(0, (dist / rect.width) * 100);
|
|
1070
|
+
} else {
|
|
1071
|
+
this.#radius = Math.max(0, dist);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
if (this.#angleTooltip) {
|
|
1075
|
+
this.#angleTooltip.setAttribute(
|
|
1076
|
+
"text",
|
|
1077
|
+
`Angle ${Math.round(this.#angle)}°`,
|
|
1078
|
+
);
|
|
1079
|
+
this.#angleTooltip.setAttribute("show", "true");
|
|
1080
|
+
this.#angleTooltip.showPopup?.();
|
|
1081
|
+
}
|
|
1082
|
+
this.#syncPositions();
|
|
1083
|
+
this.#emitInput();
|
|
1084
|
+
});
|
|
1085
|
+
|
|
1086
|
+
this.#angleHandle.addEventListener("change", (e) => {
|
|
1087
|
+
e.stopPropagation();
|
|
1088
|
+
this.classList.remove("fig-canvas-control-ring-active");
|
|
1089
|
+
if (this.#angleTooltip) this.#angleTooltip.removeAttribute("show");
|
|
1090
|
+
this.#syncPositions();
|
|
1091
|
+
this.#syncValueAttribute();
|
|
1092
|
+
this.#emitChange();
|
|
1093
|
+
requestAnimationFrame(() => {
|
|
1094
|
+
this.#isAngleDragging = false;
|
|
1095
|
+
});
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
this.#angleHandle.addEventListener("hitareadown", (e) => {
|
|
1099
|
+
e.stopPropagation();
|
|
1100
|
+
const origEvent = e.detail?.originalEvent;
|
|
1101
|
+
if (!origEvent) return;
|
|
1102
|
+
origEvent.preventDefault();
|
|
1103
|
+
this.#isAngleDragging = true;
|
|
1104
|
+
this.classList.add("fig-canvas-control-ring-active");
|
|
1105
|
+
const container = this.#container;
|
|
1106
|
+
if (!container) return;
|
|
1107
|
+
|
|
1108
|
+
if (this.#angleTooltip) {
|
|
1109
|
+
this.#angleTooltip.setAttribute("show", "true");
|
|
1110
|
+
this.#angleTooltip.showPopup?.();
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
const prevBodyCursor = document.body.style.cursor;
|
|
1114
|
+
let lastCursorDeg = Math.round(this.#angle);
|
|
1115
|
+
document.body.style.cursor = this.#rotateCursorSvg(lastCursorDeg);
|
|
1116
|
+
|
|
1117
|
+
const onMove = (ev) => {
|
|
1118
|
+
const rect = container.getBoundingClientRect();
|
|
1119
|
+
const cx = (this.#x / 100) * rect.width;
|
|
1120
|
+
const cy = (this.#y / 100) * rect.height;
|
|
1121
|
+
const dx = ev.clientX - rect.left - cx;
|
|
1122
|
+
const dy = ev.clientY - rect.top - cy;
|
|
1123
|
+
let angle = (Math.atan2(dy, dx) * 180) / Math.PI;
|
|
1124
|
+
if (this.#shouldSnap(ev.shiftKey)) {
|
|
1125
|
+
angle = Math.round(angle / 15) * 15;
|
|
1126
|
+
}
|
|
1127
|
+
this.#angle = angle;
|
|
1128
|
+
if (this.#angleTooltip)
|
|
1129
|
+
this.#angleTooltip.setAttribute(
|
|
1130
|
+
"text",
|
|
1131
|
+
`Angle ${Math.round(angle)}°`,
|
|
1132
|
+
);
|
|
1133
|
+
this.#syncPositions();
|
|
1134
|
+
const curDeg = Math.round(angle);
|
|
1135
|
+
if (curDeg !== lastCursorDeg) {
|
|
1136
|
+
lastCursorDeg = curDeg;
|
|
1137
|
+
document.body.style.cursor = this.#rotateCursorSvg(curDeg);
|
|
1138
|
+
}
|
|
1139
|
+
this.#emitInput();
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
const onUp = () => {
|
|
1143
|
+
this.#isAngleDragging = false;
|
|
1144
|
+
this.classList.remove("fig-canvas-control-ring-active");
|
|
1145
|
+
document.body.style.cursor = prevBodyCursor;
|
|
1146
|
+
if (this.#angleTooltip) this.#angleTooltip.removeAttribute("show");
|
|
1147
|
+
this.#syncValueAttribute();
|
|
1148
|
+
this.#emitChange();
|
|
1149
|
+
window.removeEventListener("pointermove", onMove);
|
|
1150
|
+
window.removeEventListener("pointerup", onUp);
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
window.addEventListener("pointermove", onMove);
|
|
1154
|
+
window.addEventListener("pointerup", onUp);
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
if (this.#secondHandle) {
|
|
1159
|
+
this.#secondHandle.addEventListener("input", (e) => {
|
|
1160
|
+
e.stopPropagation();
|
|
1161
|
+
if (!this.#isSecondDragging) {
|
|
1162
|
+
this.#prevBodyCursor = document.body.style.cursor;
|
|
1163
|
+
}
|
|
1164
|
+
this.#isSecondDragging = true;
|
|
1165
|
+
const px = e.detail?.px ?? this.#x2 / 100;
|
|
1166
|
+
const py = e.detail?.py ?? this.#y2 / 100;
|
|
1167
|
+
this.#x2 = Math.round(Math.max(0, Math.min(100, px * 100)));
|
|
1168
|
+
this.#y2 = Math.round(Math.max(0, Math.min(100, py * 100)));
|
|
1169
|
+
if (this.#secondTooltip) {
|
|
1170
|
+
this.#secondTooltip.removeAttribute("show");
|
|
1171
|
+
this.#secondTooltip.hidePopup?.();
|
|
1172
|
+
}
|
|
1173
|
+
this.#syncPositions();
|
|
1174
|
+
document.body.style.cursor = this.#resizeCursorSvg(
|
|
1175
|
+
this.#pointPointLineDeg(),
|
|
1176
|
+
);
|
|
1177
|
+
this.#emitInput();
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
this.#secondHandle.addEventListener("change", (e) => {
|
|
1181
|
+
e.stopPropagation();
|
|
1182
|
+
document.body.style.cursor = this.#prevBodyCursor ?? "";
|
|
1183
|
+
if (this.#secondTooltip) this.#secondTooltip.removeAttribute("show");
|
|
1184
|
+
this.#syncPositions();
|
|
1185
|
+
this.#syncValueAttribute();
|
|
1186
|
+
this.#emitChange();
|
|
1187
|
+
requestAnimationFrame(() => {
|
|
1188
|
+
this.#isSecondDragging = false;
|
|
1189
|
+
});
|
|
1190
|
+
});
|
|
1191
|
+
|
|
1192
|
+
this.#setupPointPointHitArea(this.#pointHandle, true);
|
|
1193
|
+
this.#setupPointPointHitArea(this.#secondHandle, false);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
#setupPointPointHitArea(handle, isFirst) {
|
|
1198
|
+
if (!handle) return;
|
|
1199
|
+
handle.addEventListener("hitareadown", (e) => {
|
|
1200
|
+
e.stopPropagation();
|
|
1201
|
+
const origEvent = e.detail?.originalEvent;
|
|
1202
|
+
if (!origEvent) return;
|
|
1203
|
+
origEvent.preventDefault();
|
|
1204
|
+
this.#isDragging = true;
|
|
1205
|
+
const container = this.#container;
|
|
1206
|
+
if (!container) return;
|
|
1207
|
+
const rect = container.getBoundingClientRect();
|
|
1208
|
+
|
|
1209
|
+
const pivotX = isFirst ? this.#x2 : this.#x;
|
|
1210
|
+
const pivotY = isFirst ? this.#y2 : this.#y;
|
|
1211
|
+
const movingX = isFirst ? this.#x : this.#x2;
|
|
1212
|
+
const movingY = isFirst ? this.#y : this.#y2;
|
|
1213
|
+
const pcx = (pivotX / 100) * rect.width;
|
|
1214
|
+
const pcy = (pivotY / 100) * rect.height;
|
|
1215
|
+
const mcx = (movingX / 100) * rect.width;
|
|
1216
|
+
const mcy = (movingY / 100) * rect.height;
|
|
1217
|
+
const fixedLen = Math.sqrt((mcx - pcx) ** 2 + (mcy - pcy) ** 2);
|
|
1218
|
+
|
|
1219
|
+
const tooltip = isFirst ? this.#pointTooltip : this.#secondTooltip;
|
|
1220
|
+
if (tooltip) {
|
|
1221
|
+
tooltip.removeAttribute("show");
|
|
1222
|
+
tooltip.hidePopup?.();
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
const prevBodyCursor = document.body.style.cursor;
|
|
1226
|
+
const initDeg = this.#pointPointLineDeg();
|
|
1227
|
+
let lastCursorDeg = Math.round(isFirst ? initDeg + 180 : initDeg);
|
|
1228
|
+
document.body.style.cursor = this.#rotateCursorSvg(lastCursorDeg);
|
|
1229
|
+
|
|
1230
|
+
const onMove = (ev) => {
|
|
1231
|
+
const r = container.getBoundingClientRect();
|
|
1232
|
+
const px = (pivotX / 100) * r.width;
|
|
1233
|
+
const py = (pivotY / 100) * r.height;
|
|
1234
|
+
const dx = ev.clientX - r.left - px;
|
|
1235
|
+
const dy = ev.clientY - r.top - py;
|
|
1236
|
+
let angle = Math.atan2(dy, dx);
|
|
1237
|
+
if (this.#shouldSnap(ev.shiftKey)) {
|
|
1238
|
+
const snapDeg = Math.round((angle * 180) / Math.PI / 15) * 15;
|
|
1239
|
+
angle = (snapDeg * Math.PI) / 180;
|
|
1240
|
+
}
|
|
1241
|
+
const nx = px + fixedLen * Math.cos(angle);
|
|
1242
|
+
const ny = py + fixedLen * Math.sin(angle);
|
|
1243
|
+
const newPctX = Math.max(0, Math.min(100, (nx / r.width) * 100));
|
|
1244
|
+
const newPctY = Math.max(0, Math.min(100, (ny / r.height) * 100));
|
|
1245
|
+
if (isFirst) {
|
|
1246
|
+
this.#x = newPctX;
|
|
1247
|
+
this.#y = newPctY;
|
|
1248
|
+
} else {
|
|
1249
|
+
this.#x2 = newPctX;
|
|
1250
|
+
this.#y2 = newPctY;
|
|
1251
|
+
}
|
|
1252
|
+
this.#syncPositions();
|
|
1253
|
+
const curDeg = Math.round(
|
|
1254
|
+
isFirst ? this.#pointPointLineDeg() + 180 : this.#pointPointLineDeg(),
|
|
1255
|
+
);
|
|
1256
|
+
if (curDeg !== lastCursorDeg) {
|
|
1257
|
+
lastCursorDeg = curDeg;
|
|
1258
|
+
document.body.style.cursor = this.#rotateCursorSvg(curDeg);
|
|
1259
|
+
}
|
|
1260
|
+
this.#emitInput();
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1263
|
+
const onUp = () => {
|
|
1264
|
+
this.#isDragging = false;
|
|
1265
|
+
document.body.style.cursor = prevBodyCursor;
|
|
1266
|
+
if (tooltip) tooltip.removeAttribute("show");
|
|
1267
|
+
this.#syncValueAttribute();
|
|
1268
|
+
this.#emitChange();
|
|
1269
|
+
window.removeEventListener("pointermove", onMove);
|
|
1270
|
+
window.removeEventListener("pointerup", onUp);
|
|
1271
|
+
};
|
|
1272
|
+
|
|
1273
|
+
window.addEventListener("pointermove", onMove);
|
|
1274
|
+
window.addEventListener("pointerup", onUp);
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
#setupRadiusDrag(circle) {
|
|
1279
|
+
if (!circle) return;
|
|
1280
|
+
circle.addEventListener("pointermove", (e) => {
|
|
1281
|
+
if (this.#isRadiusDragging) return;
|
|
1282
|
+
const container = this.#container;
|
|
1283
|
+
if (!container) return;
|
|
1284
|
+
const rect = container.getBoundingClientRect();
|
|
1285
|
+
const cx = (this.#x / 100) * rect.width;
|
|
1286
|
+
const cy = (this.#y / 100) * rect.height;
|
|
1287
|
+
const deg =
|
|
1288
|
+
(Math.atan2(e.clientY - rect.top - cy, e.clientX - rect.left - cx) *
|
|
1289
|
+
180) /
|
|
1290
|
+
Math.PI;
|
|
1291
|
+
circle.style.cursor = this.#resizeCursorSvg(deg);
|
|
1292
|
+
});
|
|
1293
|
+
const onDown = (e) => {
|
|
1294
|
+
if (this.hasAttribute("disabled")) return;
|
|
1295
|
+
e.preventDefault();
|
|
1296
|
+
e.stopPropagation();
|
|
1297
|
+
this.#isRadiusDragging = true;
|
|
1298
|
+
this.classList.add("fig-canvas-control-ring-active");
|
|
1299
|
+
const container = this.#container;
|
|
1300
|
+
if (!container) return;
|
|
1301
|
+
|
|
1302
|
+
if (this.#radiusTooltip) {
|
|
1303
|
+
this.#radiusTooltip.setAttribute("show", "true");
|
|
1304
|
+
this.#radiusTooltip.showPopup?.();
|
|
1305
|
+
this.#setRadiusTooltipAnchorAt(e.clientX, e.clientY);
|
|
1306
|
+
}
|
|
1307
|
+
if (this.#angleTooltip) {
|
|
1308
|
+
this.#angleTooltip.removeAttribute("show");
|
|
1309
|
+
this.#angleTooltip.hidePopup?.();
|
|
1310
|
+
}
|
|
1311
|
+
const prevAnglePointerEvents = this.#angleHandle?.style.pointerEvents;
|
|
1312
|
+
const angleHitArea = this.#angleHandle?.querySelector(
|
|
1313
|
+
".fig-handle-hit-area",
|
|
1314
|
+
);
|
|
1315
|
+
const prevAngleHitPointerEvents = angleHitArea?.style.pointerEvents;
|
|
1316
|
+
if (this.#angleHandle) {
|
|
1317
|
+
this.#angleHandle.style.pointerEvents = "none";
|
|
1318
|
+
}
|
|
1319
|
+
if (angleHitArea) {
|
|
1320
|
+
angleHitArea.style.pointerEvents = "none";
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
const prevBodyCursor = document.body.style.cursor;
|
|
1324
|
+
circle.style.pointerEvents = "none";
|
|
1325
|
+
const rect0 = container.getBoundingClientRect();
|
|
1326
|
+
const cx0 = (this.#x / 100) * rect0.width;
|
|
1327
|
+
const cy0 = (this.#y / 100) * rect0.height;
|
|
1328
|
+
const initDeg =
|
|
1329
|
+
(Math.atan2(e.clientY - rect0.top - cy0, e.clientX - rect0.left - cx0) *
|
|
1330
|
+
180) /
|
|
1331
|
+
Math.PI;
|
|
1332
|
+
let lastCursorDeg = Math.round(initDeg);
|
|
1333
|
+
document.body.style.cursor = this.#resizeCursorSvg(lastCursorDeg);
|
|
1334
|
+
|
|
1335
|
+
const onMove = (ev) => {
|
|
1336
|
+
const rect = container.getBoundingClientRect();
|
|
1337
|
+
const cx = (this.#x / 100) * rect.width;
|
|
1338
|
+
const cy = (this.#y / 100) * rect.height;
|
|
1339
|
+
const dx = ev.clientX - rect.left - cx;
|
|
1340
|
+
const dy = ev.clientY - rect.top - cy;
|
|
1341
|
+
const curDeg = Math.round((Math.atan2(dy, dx) * 180) / Math.PI);
|
|
1342
|
+
if (curDeg !== lastCursorDeg) {
|
|
1343
|
+
lastCursorDeg = curDeg;
|
|
1344
|
+
document.body.style.cursor = this.#resizeCursorSvg(curDeg);
|
|
1345
|
+
}
|
|
1346
|
+
let dist = Math.sqrt(dx * dx + dy * dy);
|
|
1347
|
+
if (this.#shouldSnap(ev.shiftKey)) {
|
|
1348
|
+
const step = this.#radiusIsPercent ? 5 : 10;
|
|
1349
|
+
if (this.#radiusIsPercent) {
|
|
1350
|
+
let pct = (dist / rect.width) * 100;
|
|
1351
|
+
pct = Math.round(pct / step) * step;
|
|
1352
|
+
dist = (pct / 100) * rect.width;
|
|
1353
|
+
} else {
|
|
1354
|
+
dist = Math.round(dist / step) * step;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
if (this.#radiusIsPercent) {
|
|
1358
|
+
this.#radius = Math.max(0, (dist / rect.width) * 100);
|
|
1359
|
+
} else {
|
|
1360
|
+
this.#radius = Math.max(0, dist);
|
|
1361
|
+
}
|
|
1362
|
+
if (this.#radiusTooltip) {
|
|
1363
|
+
this.#radiusTooltip.setAttribute("text", this.#formatRadius());
|
|
1364
|
+
this.#setRadiusTooltipAnchorAt(ev.clientX, ev.clientY);
|
|
1365
|
+
}
|
|
1366
|
+
this.#syncPositions();
|
|
1367
|
+
this.#emitInput();
|
|
1368
|
+
};
|
|
1369
|
+
|
|
1370
|
+
const onUp = () => {
|
|
1371
|
+
this.#isRadiusDragging = false;
|
|
1372
|
+
this.classList.remove("fig-canvas-control-ring-active");
|
|
1373
|
+
circle.style.pointerEvents = "";
|
|
1374
|
+
if (this.#angleHandle) {
|
|
1375
|
+
this.#angleHandle.style.pointerEvents = prevAnglePointerEvents ?? "";
|
|
1376
|
+
}
|
|
1377
|
+
if (angleHitArea) {
|
|
1378
|
+
angleHitArea.style.pointerEvents = prevAngleHitPointerEvents ?? "";
|
|
1379
|
+
}
|
|
1380
|
+
document.body.style.cursor = prevBodyCursor;
|
|
1381
|
+
if (this.#radiusTooltip) this.#radiusTooltip.removeAttribute("show");
|
|
1382
|
+
this.#syncValueAttribute();
|
|
1383
|
+
this.#emitChange();
|
|
1384
|
+
window.removeEventListener("pointermove", onMove);
|
|
1385
|
+
window.removeEventListener("pointerup", onUp);
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
window.addEventListener("pointermove", onMove);
|
|
1389
|
+
window.addEventListener("pointerup", onUp);
|
|
1390
|
+
};
|
|
1391
|
+
circle.addEventListener("pointerdown", onDown);
|
|
1392
|
+
this._radiusDragCleanup = () =>
|
|
1393
|
+
circle.removeEventListener("pointerdown", onDown);
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
#teardownRadiusDrag() {
|
|
1397
|
+
if (this._radiusDragCleanup) {
|
|
1398
|
+
this._radiusDragCleanup();
|
|
1399
|
+
this._radiusDragCleanup = null;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
customElements.define("fig-canvas-control", FigCanvasControl);
|
|
1404
|
+
|
|
1405
|
+
/* Angle Input */
|
|
1406
|
+
/**
|
|
1407
|
+
* A custom angle chooser input element.
|
|
1408
|
+
* @attr {number} value - The current angle of the handle in degrees.
|
|
1409
|
+
* @attr {number} precision - The number of decimal places for the output.
|
|
1410
|
+
* @attr {boolean} text - Whether to display a text input for the angle value.
|
|
1411
|
+
* @attr {boolean} dial - Whether to display the circular dial control. Defaults to true.
|
|
1412
|
+
* @attr {number} adjacent - The adjacent value of the angle.
|
|
1413
|
+
* @attr {number} opposite - The opposite value of the angle.
|
|
1414
|
+
* @attr {boolean} rotations - Whether to display a rotation count (×N) when rotations > 1. Defaults to false.
|
|
1415
|
+
*/
|
|
1416
|
+
class FigInputAngle extends HTMLElement {
|
|
1417
|
+
// Private fields
|
|
1418
|
+
#adjacent;
|
|
1419
|
+
#opposite;
|
|
1420
|
+
#prevRawAngle = null;
|
|
1421
|
+
#boundHandleRawChange;
|
|
1422
|
+
#boundHandleMouseDown;
|
|
1423
|
+
#boundHandleTouchStart;
|
|
1424
|
+
#boundHandleKeyDown;
|
|
1425
|
+
#boundHandleKeyUp;
|
|
1426
|
+
#boundHandleAngleInput;
|
|
1427
|
+
|
|
1428
|
+
constructor() {
|
|
1429
|
+
super();
|
|
1430
|
+
|
|
1431
|
+
this.angle = 0;
|
|
1432
|
+
this.#adjacent = 1;
|
|
1433
|
+
this.#opposite = 0;
|
|
1434
|
+
this.isDragging = false;
|
|
1435
|
+
this.isShiftHeld = false;
|
|
1436
|
+
this.handle = null;
|
|
1437
|
+
this.angleInput = null;
|
|
1438
|
+
this.plane = null;
|
|
1439
|
+
this.units = "°";
|
|
1440
|
+
this.min = null;
|
|
1441
|
+
this.max = null;
|
|
1442
|
+
this.dial = true;
|
|
1443
|
+
this.showRotations = false;
|
|
1444
|
+
this.rotationSpan = null;
|
|
1445
|
+
|
|
1446
|
+
this.#boundHandleRawChange = this.#handleRawChange.bind(this);
|
|
1447
|
+
this.#boundHandleMouseDown = this.#handleMouseDown.bind(this);
|
|
1448
|
+
this.#boundHandleTouchStart = this.#handleTouchStart.bind(this);
|
|
1449
|
+
this.#boundHandleKeyDown = this.#handleKeyDown.bind(this);
|
|
1450
|
+
this.#boundHandleKeyUp = this.#handleKeyUp.bind(this);
|
|
1451
|
+
this.#boundHandleAngleInput = this.#handleAngleInput.bind(this);
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
connectedCallback() {
|
|
1455
|
+
requestAnimationFrame(() => {
|
|
1456
|
+
this.precision = this.getAttribute("precision") || 1;
|
|
1457
|
+
this.precision = parseInt(this.precision);
|
|
1458
|
+
this.text = this.getAttribute("text") === "true";
|
|
1459
|
+
|
|
1460
|
+
let rawUnits = this.getAttribute("units") || "°";
|
|
1461
|
+
if (rawUnits === "deg") rawUnits = "°";
|
|
1462
|
+
this.units = rawUnits;
|
|
1463
|
+
|
|
1464
|
+
this.min = this.hasAttribute("min")
|
|
1465
|
+
? Number(this.getAttribute("min"))
|
|
1466
|
+
: null;
|
|
1467
|
+
this.max = this.hasAttribute("max")
|
|
1468
|
+
? Number(this.getAttribute("max"))
|
|
1469
|
+
: null;
|
|
1470
|
+
this.dial = this.#readBooleanAttribute("dial", true);
|
|
1471
|
+
this.showRotations = this.#readRotationsEnabled();
|
|
1472
|
+
|
|
1473
|
+
this.#render();
|
|
1474
|
+
this.#setupListeners();
|
|
1475
|
+
|
|
1476
|
+
this.#syncHandlePosition();
|
|
1477
|
+
if (this.text && this.angleInput) {
|
|
1478
|
+
this.angleInput.setAttribute(
|
|
1479
|
+
"value",
|
|
1480
|
+
this.angle.toFixed(this.precision),
|
|
1481
|
+
);
|
|
1482
|
+
}
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
disconnectedCallback() {
|
|
1487
|
+
this.#cleanupListeners();
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
#render() {
|
|
1491
|
+
this.innerHTML = this.#getInnerHTML();
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
#readBooleanAttribute(name, defaultValue = false) {
|
|
1495
|
+
const value = this.getAttribute(name);
|
|
1496
|
+
if (value === null) return defaultValue;
|
|
1497
|
+
const normalized = value.trim().toLowerCase();
|
|
1498
|
+
if (normalized === "" || normalized === "true") return true;
|
|
1499
|
+
if (normalized === "false") return false;
|
|
1500
|
+
return true;
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
#readRotationsEnabled() {
|
|
1504
|
+
if (this.hasAttribute("rotations")) {
|
|
1505
|
+
return this.#readBooleanAttribute("rotations", false);
|
|
1506
|
+
}
|
|
1507
|
+
if (this.hasAttribute("show-rotations")) {
|
|
1508
|
+
return this.#readBooleanAttribute("show-rotations", false);
|
|
1509
|
+
}
|
|
1510
|
+
return false;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
#getInnerHTML() {
|
|
1514
|
+
const step = this.#getStepForUnit();
|
|
1515
|
+
const minAttr = this.min !== null ? `min="${this.min}"` : "";
|
|
1516
|
+
const maxAttr = this.max !== null ? `max="${this.max}"` : "";
|
|
1517
|
+
return `
|
|
1518
|
+
${
|
|
1519
|
+
this.dial
|
|
1520
|
+
? `<div class="fig-input-angle-plane" tabindex="0">
|
|
1521
|
+
<div class="fig-input-angle-handle"></div>
|
|
1522
|
+
</div>`
|
|
1523
|
+
: ""
|
|
1524
|
+
}
|
|
1525
|
+
${
|
|
1526
|
+
this.text
|
|
1527
|
+
? `<fig-input-number
|
|
1528
|
+
name="angle"
|
|
1529
|
+
step="${step}"
|
|
1530
|
+
value="${this.angle}"
|
|
1531
|
+
${minAttr}
|
|
1532
|
+
${maxAttr}
|
|
1533
|
+
units="${this.units}">
|
|
1534
|
+
${this.showRotations ? `<span slot="append" class="fig-input-angle-rotations"></span>` : ""}
|
|
1535
|
+
</fig-input-number>`
|
|
1536
|
+
: ""
|
|
1537
|
+
}
|
|
1538
|
+
`;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
#getRotationCount() {
|
|
1542
|
+
const degrees = Math.abs(this.#toDegrees(this.angle));
|
|
1543
|
+
return Math.floor(degrees / 360);
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
#updateRotationDisplay() {
|
|
1547
|
+
if (!this.rotationSpan) return;
|
|
1548
|
+
const rotations = this.#getRotationCount();
|
|
1549
|
+
if (rotations > 1) {
|
|
1550
|
+
this.rotationSpan.textContent = `\u00d7${rotations}`;
|
|
1551
|
+
this.rotationSpan.style.display = "";
|
|
1552
|
+
} else {
|
|
1553
|
+
this.rotationSpan.textContent = "";
|
|
1554
|
+
this.rotationSpan.style.display = "none";
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
#getStepForUnit() {
|
|
1559
|
+
switch (this.units) {
|
|
1560
|
+
case "rad":
|
|
1561
|
+
return 0.01;
|
|
1562
|
+
case "turn":
|
|
1563
|
+
return 0.001;
|
|
1564
|
+
default:
|
|
1565
|
+
return 0.1;
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
#toDegrees(value) {
|
|
1570
|
+
switch (this.units) {
|
|
1571
|
+
case "rad":
|
|
1572
|
+
return (value * 180) / Math.PI;
|
|
1573
|
+
case "turn":
|
|
1574
|
+
return value * 360;
|
|
1575
|
+
default:
|
|
1576
|
+
return value;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
#fromDegrees(degrees) {
|
|
1581
|
+
switch (this.units) {
|
|
1582
|
+
case "rad":
|
|
1583
|
+
return (degrees * Math.PI) / 180;
|
|
1584
|
+
case "turn":
|
|
1585
|
+
return degrees / 360;
|
|
1586
|
+
default:
|
|
1587
|
+
return degrees;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
#convertAngle(value, fromUnit, toUnit) {
|
|
1592
|
+
let degrees;
|
|
1593
|
+
switch (fromUnit) {
|
|
1594
|
+
case "rad":
|
|
1595
|
+
degrees = (value * 180) / Math.PI;
|
|
1596
|
+
break;
|
|
1597
|
+
case "turn":
|
|
1598
|
+
degrees = value * 360;
|
|
1599
|
+
break;
|
|
1600
|
+
default:
|
|
1601
|
+
degrees = value;
|
|
1602
|
+
}
|
|
1603
|
+
switch (toUnit) {
|
|
1604
|
+
case "rad":
|
|
1605
|
+
return (degrees * Math.PI) / 180;
|
|
1606
|
+
case "turn":
|
|
1607
|
+
return degrees / 360;
|
|
1608
|
+
default:
|
|
1609
|
+
return degrees;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
#setupListeners() {
|
|
1614
|
+
this.handle = this.querySelector(".fig-input-angle-handle");
|
|
1615
|
+
this.plane = this.querySelector(".fig-input-angle-plane");
|
|
1616
|
+
this.angleInput = this.querySelector("fig-input-number[name='angle']");
|
|
1617
|
+
this.rotationSpan = this.querySelector(".fig-input-angle-rotations");
|
|
1618
|
+
this.#updateRotationDisplay();
|
|
1619
|
+
this.plane?.addEventListener("mousedown", this.#boundHandleMouseDown);
|
|
1620
|
+
this.plane?.addEventListener("touchstart", this.#boundHandleTouchStart);
|
|
1621
|
+
window.addEventListener("keydown", this.#boundHandleKeyDown);
|
|
1622
|
+
window.addEventListener("keyup", this.#boundHandleKeyUp);
|
|
1623
|
+
if (this.text && this.angleInput) {
|
|
1624
|
+
this.angleInput.addEventListener("input", this.#boundHandleAngleInput);
|
|
1625
|
+
}
|
|
1626
|
+
this.addEventListener("change", this.#boundHandleRawChange, true);
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
#cleanupListeners() {
|
|
1630
|
+
this.plane?.removeEventListener("mousedown", this.#boundHandleMouseDown);
|
|
1631
|
+
this.plane?.removeEventListener("touchstart", this.#boundHandleTouchStart);
|
|
1632
|
+
window.removeEventListener("keydown", this.#boundHandleKeyDown);
|
|
1633
|
+
window.removeEventListener("keyup", this.#boundHandleKeyUp);
|
|
1634
|
+
if (this.text && this.angleInput) {
|
|
1635
|
+
this.angleInput.removeEventListener("input", this.#boundHandleAngleInput);
|
|
1636
|
+
}
|
|
1637
|
+
this.removeEventListener("change", this.#boundHandleRawChange, true);
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
#handleRawChange(e) {
|
|
1641
|
+
if (!e.target?.matches?.("input")) return;
|
|
1642
|
+
const raw = e.target.value;
|
|
1643
|
+
const match = raw.match(/^(-?\d*\.?\d+)\s*(turn|rad|deg|°)$/i);
|
|
1644
|
+
if (match) {
|
|
1645
|
+
const num = parseFloat(match[1]);
|
|
1646
|
+
let fromUnit = match[2].toLowerCase();
|
|
1647
|
+
if (fromUnit === "deg") fromUnit = "°";
|
|
1648
|
+
if (fromUnit !== this.units) {
|
|
1649
|
+
const converted = this.#convertAngle(num, fromUnit, this.units);
|
|
1650
|
+
e.target.value = String(converted);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
#handleAngleInput(e) {
|
|
1656
|
+
e.stopPropagation();
|
|
1657
|
+
this.angle = Number(e.target.value);
|
|
1658
|
+
this.#calculateAdjacentAndOpposite();
|
|
1659
|
+
this.#syncHandlePosition();
|
|
1660
|
+
this.#updateRotationDisplay();
|
|
1661
|
+
this.#emitInputEvent();
|
|
1662
|
+
this.#emitChangeEvent();
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
#calculateAdjacentAndOpposite() {
|
|
1666
|
+
const degrees = this.#toDegrees(this.angle);
|
|
1667
|
+
const radians = (degrees * Math.PI) / 180;
|
|
1668
|
+
this.#adjacent = Math.cos(radians);
|
|
1669
|
+
this.#opposite = Math.sin(radians);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
#snapToIncrement(angle) {
|
|
1673
|
+
if (!this.isShiftHeld) return angle;
|
|
1674
|
+
const increment = 45;
|
|
1675
|
+
return Math.round(angle / increment) * increment;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
#getRawAngle(e) {
|
|
1679
|
+
const rect = this.plane.getBoundingClientRect();
|
|
1680
|
+
const centerX = rect.left + rect.width / 2;
|
|
1681
|
+
const centerY = rect.top + rect.height / 2;
|
|
1682
|
+
const deltaX = e.clientX - centerX;
|
|
1683
|
+
const deltaY = e.clientY - centerY;
|
|
1684
|
+
return (Math.atan2(deltaY, deltaX) * 180) / Math.PI;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
#updateAngle(e) {
|
|
1688
|
+
let rawAngle = this.#getRawAngle(e);
|
|
1689
|
+
let normalizedAngle = ((rawAngle % 360) + 360) % 360;
|
|
1690
|
+
normalizedAngle = this.#snapToIncrement(normalizedAngle);
|
|
1691
|
+
|
|
1692
|
+
const isBounded = this.min !== null || this.max !== null;
|
|
1693
|
+
|
|
1694
|
+
if (isBounded) {
|
|
1695
|
+
this.angle = this.#fromDegrees(normalizedAngle);
|
|
1696
|
+
} else {
|
|
1697
|
+
if (this.#prevRawAngle === null) {
|
|
1698
|
+
this.#prevRawAngle = normalizedAngle;
|
|
1699
|
+
const currentDeg = this.#toDegrees(this.angle);
|
|
1700
|
+
const currentMod = ((currentDeg % 360) + 360) % 360;
|
|
1701
|
+
let delta = normalizedAngle - currentMod;
|
|
1702
|
+
if (delta > 180) delta -= 360;
|
|
1703
|
+
if (delta < -180) delta += 360;
|
|
1704
|
+
this.angle += this.#fromDegrees(delta);
|
|
1705
|
+
} else {
|
|
1706
|
+
let delta = normalizedAngle - this.#prevRawAngle;
|
|
1707
|
+
if (delta > 180) delta -= 360;
|
|
1708
|
+
if (delta < -180) delta += 360;
|
|
1709
|
+
this.angle += this.#fromDegrees(delta);
|
|
1710
|
+
this.#prevRawAngle = normalizedAngle;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
this.#calculateAdjacentAndOpposite();
|
|
1715
|
+
|
|
1716
|
+
this.#syncHandlePosition();
|
|
1717
|
+
if (this.text && this.angleInput) {
|
|
1718
|
+
this.angleInput.setAttribute("value", this.angle.toFixed(this.precision));
|
|
1719
|
+
}
|
|
1720
|
+
this.#updateRotationDisplay();
|
|
1721
|
+
|
|
1722
|
+
this.#emitInputEvent();
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
#emitInputEvent() {
|
|
1726
|
+
this.dispatchEvent(
|
|
1727
|
+
new CustomEvent("input", {
|
|
1728
|
+
bubbles: true,
|
|
1729
|
+
cancelable: true,
|
|
1730
|
+
detail: { value: this.value, angle: this.angle },
|
|
1731
|
+
}),
|
|
1732
|
+
);
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
#emitChangeEvent() {
|
|
1736
|
+
this.dispatchEvent(
|
|
1737
|
+
new CustomEvent("change", {
|
|
1738
|
+
bubbles: true,
|
|
1739
|
+
cancelable: true,
|
|
1740
|
+
detail: { value: this.value, angle: this.angle },
|
|
1741
|
+
}),
|
|
1742
|
+
);
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
#syncHandlePosition() {
|
|
1746
|
+
if (this.handle) {
|
|
1747
|
+
const degrees = this.#toDegrees(this.angle);
|
|
1748
|
+
const radians = (degrees * Math.PI) / 180;
|
|
1749
|
+
const radius = this.plane.offsetWidth / 2 - this.handle.offsetWidth / 2;
|
|
1750
|
+
const x = Math.cos(radians) * radius;
|
|
1751
|
+
const y = Math.sin(radians) * radius;
|
|
1752
|
+
this.handle.style.transform = `translate(${x}px, ${y}px)`;
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
#handleMouseDown(e) {
|
|
1757
|
+
this.isDragging = true;
|
|
1758
|
+
this.#prevRawAngle = null;
|
|
1759
|
+
this.#updateAngle(e);
|
|
1760
|
+
|
|
1761
|
+
const handleMouseMove = (e) => {
|
|
1762
|
+
this.plane.classList.add("dragging");
|
|
1763
|
+
if (this.isDragging) this.#updateAngle(e);
|
|
1764
|
+
};
|
|
1765
|
+
|
|
1766
|
+
const handleMouseUp = () => {
|
|
1767
|
+
this.isDragging = false;
|
|
1768
|
+
this.#prevRawAngle = null;
|
|
1769
|
+
this.plane.classList.remove("dragging");
|
|
1770
|
+
window.removeEventListener("mousemove", handleMouseMove);
|
|
1771
|
+
window.removeEventListener("mouseup", handleMouseUp);
|
|
1772
|
+
this.#emitChangeEvent();
|
|
1773
|
+
};
|
|
1774
|
+
|
|
1775
|
+
window.addEventListener("mousemove", handleMouseMove);
|
|
1776
|
+
window.addEventListener("mouseup", handleMouseUp);
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
#handleTouchStart(e) {
|
|
1780
|
+
e.preventDefault();
|
|
1781
|
+
this.isDragging = true;
|
|
1782
|
+
this.#prevRawAngle = null;
|
|
1783
|
+
this.#updateAngle(e.touches[0]);
|
|
1784
|
+
|
|
1785
|
+
const handleTouchMove = (e) => {
|
|
1786
|
+
this.plane.classList.add("dragging");
|
|
1787
|
+
if (this.isDragging) this.#updateAngle(e.touches[0]);
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1790
|
+
const handleTouchEnd = () => {
|
|
1791
|
+
this.isDragging = false;
|
|
1792
|
+
this.#prevRawAngle = null;
|
|
1793
|
+
this.plane.classList.remove("dragging");
|
|
1794
|
+
window.removeEventListener("touchmove", handleTouchMove);
|
|
1795
|
+
window.removeEventListener("touchend", handleTouchEnd);
|
|
1796
|
+
this.#emitChangeEvent();
|
|
1797
|
+
};
|
|
1798
|
+
|
|
1799
|
+
window.addEventListener("touchmove", handleTouchMove);
|
|
1800
|
+
window.addEventListener("touchend", handleTouchEnd);
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
#handleKeyDown(e) {
|
|
1804
|
+
if (e.key === "Shift") this.isShiftHeld = true;
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
#handleKeyUp(e) {
|
|
1808
|
+
if (e.key === "Shift") this.isShiftHeld = false;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
focus() {
|
|
1812
|
+
this.plane?.focus();
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
static get observedAttributes() {
|
|
1816
|
+
return [
|
|
1817
|
+
"value",
|
|
1818
|
+
"precision",
|
|
1819
|
+
"text",
|
|
1820
|
+
"min",
|
|
1821
|
+
"max",
|
|
1822
|
+
"units",
|
|
1823
|
+
"dial",
|
|
1824
|
+
"rotations",
|
|
1825
|
+
"show-rotations",
|
|
1826
|
+
];
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
get value() {
|
|
1830
|
+
return this.angle;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
get adjacent() {
|
|
1834
|
+
return this.#adjacent;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
get opposite() {
|
|
1838
|
+
return this.#opposite;
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
set value(value) {
|
|
1842
|
+
if (isNaN(value)) {
|
|
1843
|
+
console.error("Invalid value: must be a number.");
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
this.angle = value;
|
|
1847
|
+
this.#calculateAdjacentAndOpposite();
|
|
1848
|
+
this.#syncHandlePosition();
|
|
1849
|
+
if (this.angleInput) {
|
|
1850
|
+
this.angleInput.setAttribute("value", this.angle.toFixed(this.precision));
|
|
1851
|
+
}
|
|
1852
|
+
this.#updateRotationDisplay();
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
1856
|
+
switch (name) {
|
|
1857
|
+
case "value":
|
|
1858
|
+
if (this.isDragging) break;
|
|
1859
|
+
this.value = Number(newValue);
|
|
1860
|
+
break;
|
|
1861
|
+
case "precision":
|
|
1862
|
+
this.precision = parseInt(newValue);
|
|
1863
|
+
break;
|
|
1864
|
+
case "text":
|
|
1865
|
+
if (newValue !== oldValue) {
|
|
1866
|
+
this.text = newValue?.toLowerCase() === "true";
|
|
1867
|
+
if (this.isConnected) {
|
|
1868
|
+
this.#render();
|
|
1869
|
+
this.#setupListeners();
|
|
1870
|
+
this.#syncHandlePosition();
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
break;
|
|
1874
|
+
case "dial":
|
|
1875
|
+
this.dial = this.#readBooleanAttribute("dial", true);
|
|
1876
|
+
if (this.isConnected) {
|
|
1877
|
+
this.#render();
|
|
1878
|
+
this.#setupListeners();
|
|
1879
|
+
this.#syncHandlePosition();
|
|
1880
|
+
}
|
|
1881
|
+
break;
|
|
1882
|
+
case "units": {
|
|
1883
|
+
let units = newValue || "°";
|
|
1884
|
+
if (units === "deg") units = "°";
|
|
1885
|
+
this.units = units;
|
|
1886
|
+
if (this.isConnected) {
|
|
1887
|
+
this.#render();
|
|
1888
|
+
this.#setupListeners();
|
|
1889
|
+
this.#syncHandlePosition();
|
|
1890
|
+
}
|
|
1891
|
+
break;
|
|
1892
|
+
}
|
|
1893
|
+
case "min":
|
|
1894
|
+
this.min = newValue !== null ? Number(newValue) : null;
|
|
1895
|
+
if (this.isConnected) {
|
|
1896
|
+
this.#render();
|
|
1897
|
+
this.#setupListeners();
|
|
1898
|
+
this.#syncHandlePosition();
|
|
1899
|
+
}
|
|
1900
|
+
break;
|
|
1901
|
+
case "max":
|
|
1902
|
+
this.max = newValue !== null ? Number(newValue) : null;
|
|
1903
|
+
if (this.isConnected) {
|
|
1904
|
+
this.#render();
|
|
1905
|
+
this.#setupListeners();
|
|
1906
|
+
this.#syncHandlePosition();
|
|
1907
|
+
}
|
|
1908
|
+
break;
|
|
1909
|
+
case "rotations":
|
|
1910
|
+
case "show-rotations":
|
|
1911
|
+
this.showRotations = this.#readRotationsEnabled();
|
|
1912
|
+
if (this.isConnected) {
|
|
1913
|
+
this.#render();
|
|
1914
|
+
this.#setupListeners();
|
|
1915
|
+
this.#syncHandlePosition();
|
|
1916
|
+
}
|
|
1917
|
+
break;
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
customElements.define("fig-input-angle", FigInputAngle);
|
|
1922
|
+
|