react-lasso-select 1.2.2 → 2.0.0

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/index.js ADDED
@@ -0,0 +1,631 @@
1
+ var V = Object.defineProperty;
2
+ var I = (i, s, t) => s in i ? V(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
3
+ var a = (i, s, t) => I(i, typeof s != "symbol" ? s + "" : s, t);
4
+ import { jsx as g, jsxs as M } from "react/jsx-runtime";
5
+ import { Component as T, createRef as y, forwardRef as b } from "react";
6
+ import l from "prop-types";
7
+ const k = (i) => Object.keys(i).filter((s) => i[s]).join(" "), f = (i, s) => i.x === s.x && i.y === s.y, m = (i, s) => !i && s || i && !s || i.length !== s.length ? !1 : i.every((t, e) => f(t, s[e])), u = ({ x: i, y: s }, t = 1) => ({
8
+ x: Math.round((i + Number.EPSILON) * t) / t,
9
+ y: Math.round((s + Number.EPSILON) * t) / t
10
+ }), B = (i, s, t = 0) => {
11
+ const e = i.findIndex(
12
+ (o) => Math.max(Math.abs(o.x - s.x), Math.abs(o.y - s.y)) <= t
13
+ );
14
+ return { point: { ...i[e] || { x: NaN, y: NaN } }, index: e };
15
+ }, D = (i, s) => Math.hypot(s.x - i.x, s.y - i.y), S = (i, s) => Math.atan2(s.y - i.y, s.x - i.x), N = (i, s, t) => {
16
+ const e = D(i, s), o = S(i, s), n = Math.round(o / t) * t;
17
+ return {
18
+ x: i.x + e * Math.cos(n),
19
+ y: i.y + e * Math.sin(n)
20
+ };
21
+ }, F = (i, s, t) => {
22
+ const e = D(i, s), o = S(i, s), n = t.reduce(
23
+ (r, h) => Math.abs(h - o) < Math.abs(r - o) ? h : r,
24
+ 1 / 0
25
+ );
26
+ return n !== 1 / 0 && (s.x = i.x + e * Math.cos(n), s.y = i.y + e * Math.sin(n)), s;
27
+ }, G = (i) => {
28
+ const s = [];
29
+ for (let t = 1; t < i.length; t++) {
30
+ const e = Math.atan2(i[t].y - i[t - 1].y, i[t].x - i[t - 1].x), o = e + Math.PI;
31
+ s.push(e, o > Math.PI ? o - 2 * Math.PI : o);
32
+ }
33
+ return s.filter((t, e, o) => o.indexOf(t) === e);
34
+ };
35
+ function X(i, s, t, e = !0) {
36
+ const o = new Image();
37
+ o.crossOrigin = "Anonymous";
38
+ const n = document.createElement("canvas"), r = n.getContext("2d");
39
+ if (!r)
40
+ return t(new Error("CTX is null"), n);
41
+ o.onerror = () => {
42
+ t(new Error("Failed to load image"), n);
43
+ }, o.onload = () => {
44
+ try {
45
+ if (n.width = o.naturalWidth + 2, n.height = o.naturalHeight + 2, r.drawImage(o, 0, 0), s.length < 3) {
46
+ t(null, n);
47
+ return;
48
+ }
49
+ if (r.beginPath(), r.moveTo(0, 0), r.lineTo(n.width, 0), r.lineTo(n.width, n.height), r.lineTo(0, n.height), r.lineTo(0, 0), r.lineTo(s[0].x + 1, s[0].y + 1), s.slice(1).forEach(({ x: h, y: c }) => r.lineTo(h + 1, c + 1)), r.lineTo(s[0].x + 1, s[0].y + 1), r.lineTo(0, 0), r.closePath(), r.clip("evenodd"), r.globalCompositeOperation = "destination-out", r.fill(), e) {
50
+ const h = s.map(({ x: v }) => v + 1), c = s.map(({ y: v }) => v + 1), [d, P] = [Math.min.apply(null, h), Math.min.apply(null, c)], [R, O] = [Math.max.apply(null, h), Math.max.apply(null, c)], [x, w] = [R - d, O - P], A = r.getImageData(d, P, x, w);
51
+ n.width = x, n.height = w, r.putImageData(A, 0, 0);
52
+ }
53
+ t(null, n);
54
+ } catch (h) {
55
+ t(h instanceof Error ? h : new Error(String(h)), n);
56
+ }
57
+ }, o.src = i;
58
+ }
59
+ class C {
60
+ constructor(s) {
61
+ a(this, "getSvgElement");
62
+ this.getSvgElement = s;
63
+ }
64
+ getSvg() {
65
+ const s = this.getSvgElement();
66
+ if (!s) throw new Error("SVG is null");
67
+ return s;
68
+ }
69
+ getCTM() {
70
+ const s = this.getSvg();
71
+ let t = s.getCTM();
72
+ if (t === null && (t = s.querySelector('rect[visibility="hidden"]').getCTM()), !t) throw new Error("CTM is null");
73
+ return t;
74
+ }
75
+ getViewboxSize() {
76
+ return this.getSvg().viewBox.baseVal;
77
+ }
78
+ getRealSize() {
79
+ const s = this.getSvg();
80
+ return {
81
+ width: s.clientWidth,
82
+ height: s.clientHeight
83
+ };
84
+ }
85
+ getViewboxOffset() {
86
+ const s = this.getSvg(), { width: t, height: e } = this.getRealSize(), { width: o, height: n } = this.getViewboxSize(), r = Object.assign(s.createSVGPoint(), {
87
+ x: t,
88
+ y: e
89
+ }), h = this.getCTM(), { x: c, y: d } = r.matrixTransform(h.inverse());
90
+ return {
91
+ x: c - o,
92
+ y: d - n
93
+ };
94
+ }
95
+ convertViewboxPointsToReal(s) {
96
+ const t = this.getSvg(), e = this.getCTM();
97
+ return s.map(({ x: o, y: n }) => {
98
+ const r = Object.assign(t.createSVGPoint(), { x: o, y: n }).matrixTransform(e);
99
+ return u(r);
100
+ });
101
+ }
102
+ convertRealPointsToViewbox(s) {
103
+ const t = this.getSvg(), e = this.getCTM().inverse();
104
+ return s.map(({ x: o, y: n }) => {
105
+ const r = Object.assign(t.createSVGPoint(), { x: o, y: n }).matrixTransform(e);
106
+ return u(r, 1e3);
107
+ });
108
+ }
109
+ getBorderPoints(s = !0) {
110
+ const { width: t, height: e } = this.getViewboxSize(), { x: o, y: n } = this.getViewboxOffset(), r = [
111
+ { x: -o, y: -n },
112
+ { x: t + o, y: -n },
113
+ { x: t + o, y: e + n },
114
+ { x: -o, y: e + n }
115
+ ];
116
+ return s && r.push({ x: -o, y: -n }), r;
117
+ }
118
+ isAboveTheBorder({ x: s, y: t }) {
119
+ const { width: e, height: o } = this.getViewboxSize(), { x: n, y: r } = this.getViewboxOffset();
120
+ return s < -n || s > e + n || t < -r || t > o + r;
121
+ }
122
+ getMouseCoordinates(s) {
123
+ const t = s, { clientX: e, clientY: o } = t.changedTouches && t.touches ? t.changedTouches[0] || t.touches[0] : t, n = this.getSvg(), r = n.getScreenCTM();
124
+ if (!r) throw new Error("ScreenCTM is null");
125
+ const h = n.createSVGPoint();
126
+ h.x = e, h.y = o;
127
+ const { x: c, y: d } = h.matrixTransform(r.inverse());
128
+ return { x: c, y: d };
129
+ }
130
+ }
131
+ const L = (i) => class extends T {
132
+ constructor() {
133
+ super(...arguments);
134
+ a(this, "ref", y());
135
+ a(this, "svg", new C(() => {
136
+ var e, o;
137
+ return (o = (e = this.ref) == null ? void 0 : e.current) == null ? void 0 : o.ownerSVGElement;
138
+ }));
139
+ a(this, "dragLastPosition", null);
140
+ a(this, "wasMoved", !1);
141
+ a(this, "onMouseTouchDown", (e) => {
142
+ if (e.target === this.ref.current && this.props.draggable) {
143
+ e.stopImmediatePropagation(), e.preventDefault();
144
+ const o = e.target;
145
+ this.dragLastPosition = this.getMousePosition(e), o.ownerSVGElement && o.ownerSVGElement.focus({ preventScroll: !0 });
146
+ }
147
+ });
148
+ a(this, "onMouseTouchMove", (e) => {
149
+ if (this.dragLastPosition) {
150
+ e.stopImmediatePropagation(), e.preventDefault();
151
+ const { x: o, y: n } = this.getMousePosition(e), r = o - this.dragLastPosition.x, h = n - this.dragLastPosition.y;
152
+ !this.wasMoved && this.props.onDragStart && this.props.onDragStart({
153
+ x: this.dragLastPosition.x,
154
+ y: this.dragLastPosition.y,
155
+ dx: r,
156
+ dy: h
157
+ }), this.props.onDrag && this.props.onDrag({ dx: r, dy: h }), this.dragLastPosition = { x: o, y: n }, this.wasMoved = !0;
158
+ }
159
+ });
160
+ a(this, "onMouseTouchUp", (e) => {
161
+ this.dragLastPosition && this.wasMoved && (e.stopImmediatePropagation(), e.preventDefault(), (e instanceof MouseEvent || !e.touches) && window.addEventListener("click", (o) => o.stopPropagation(), {
162
+ capture: !0,
163
+ once: !0
164
+ }), this.props.onDragEnd && this.props.onDragEnd({
165
+ x: this.dragLastPosition.x,
166
+ y: this.dragLastPosition.y
167
+ })), this.dragLastPosition = null, this.wasMoved = !1;
168
+ });
169
+ }
170
+ render() {
171
+ const { draggable: e, onDrag: o, onDragStart: n, onDragEnd: r, ...h } = this.props;
172
+ return /* @__PURE__ */ g(i, { ref: this.ref, ...h, draggable: e });
173
+ }
174
+ componentDidUpdate(e) {
175
+ e.draggable && !this.props.draggable && this.dragLastPosition && this.wasMoved && (this.props.onDragEnd && this.props.onDragEnd({
176
+ x: this.dragLastPosition.x,
177
+ y: this.dragLastPosition.y
178
+ }), this.dragLastPosition = null, this.wasMoved = !1);
179
+ }
180
+ componentDidMount() {
181
+ window.addEventListener("mousedown", this.onMouseTouchDown, !0), window.addEventListener("mousemove", this.onMouseTouchMove, !0), window.addEventListener("mouseup", this.onMouseTouchUp, !0), window.addEventListener("touchstart", this.onMouseTouchDown, !0), window.addEventListener("touchmove", this.onMouseTouchMove, !0), window.addEventListener("touchend", this.onMouseTouchUp, !0);
182
+ }
183
+ componentWillUnmount() {
184
+ window.removeEventListener("mousedown", this.onMouseTouchDown), window.removeEventListener("mousemove", this.onMouseTouchMove), window.removeEventListener("mouseup", this.onMouseTouchUp), window.removeEventListener("touchstart", this.onMouseTouchDown), window.removeEventListener("touchmove", this.onMouseTouchMove), window.removeEventListener("touchend", this.onMouseTouchUp);
185
+ }
186
+ getMousePosition(e) {
187
+ const o = e;
188
+ return this.svg.getMouseCoordinates(o);
189
+ }
190
+ }, U = L(
191
+ b(function({ path: s, animate: t, draggable: e }, o) {
192
+ return /* @__PURE__ */ g(
193
+ "polyline",
194
+ {
195
+ ref: o,
196
+ style: { cursor: e ? "move" : "" },
197
+ points: s.map(({ x: n, y: r }) => `${n},${r}`).join(" "),
198
+ fill: "rgba(0,0,0,0)",
199
+ stroke: "white",
200
+ strokeWidth: "1.5",
201
+ shapeRendering: "geometricPrecision",
202
+ strokeDasharray: "3",
203
+ strokeDashoffset: "0",
204
+ vectorEffect: "non-scaling-stroke",
205
+ children: t && /* @__PURE__ */ g(
206
+ "animate",
207
+ {
208
+ attributeName: "stroke-dashoffset",
209
+ values: "0;1000;0",
210
+ dur: "100s",
211
+ repeatCount: "indefinite"
212
+ }
213
+ )
214
+ }
215
+ );
216
+ })
217
+ );
218
+ function H({ path: i }) {
219
+ return /* @__PURE__ */ g(
220
+ "polygon",
221
+ {
222
+ style: {
223
+ pointerEvents: "none",
224
+ transform: "translate(-1px, -1px)"
225
+ },
226
+ points: i.map(({ x: s, y: t }) => `${s},${t}`).join(" "),
227
+ fill: "rgba(0, 0, 0, 0.5)",
228
+ fillRule: "evenodd",
229
+ stroke: "null",
230
+ shapeRendering: "geometricPrecision"
231
+ }
232
+ );
233
+ }
234
+ const Y = L(
235
+ b(function({ x: s, y: t, onClickTouchEvent: e, draggable: o, style: n }, r) {
236
+ const { cursor: h = o ? "move" : "default", ...c } = n;
237
+ return /* @__PURE__ */ g(
238
+ "rect",
239
+ {
240
+ style: {
241
+ cursor: h,
242
+ ...c
243
+ },
244
+ ref: r,
245
+ x: s - 10,
246
+ y: t - 10,
247
+ onClick: (d) => {
248
+ d.stopPropagation(), d.preventDefault(), e(d);
249
+ },
250
+ onTouchEnd: (d) => {
251
+ d.stopPropagation(), d.preventDefault(), e(d);
252
+ },
253
+ width: "20px",
254
+ height: "20",
255
+ fill: "rgba(0, 0, 0, 0)",
256
+ stroke: "white",
257
+ strokeWidth: "1.25",
258
+ vectorEffect: "non-scaling-stroke"
259
+ }
260
+ );
261
+ })
262
+ );
263
+ var p = /* @__PURE__ */ ((i) => (i.ADD = "ADD", i.DELETE = "DELETE", i.MODIFY = "MODIFY", i.MOVE = "MOVE", i.RESET = "RESET", i.CHANGE = "CHANGE", i))(p || {});
264
+ function j(i, s) {
265
+ const t = i.points.length;
266
+ switch (s.type) {
267
+ case "ADD":
268
+ return i.closed ? [i, !1] : t > 0 && f(i.points[t - 1], s.payload) || t > 1 && f(i.points[t - 2], s.payload) ? [i, !1] : t > 2 && f(i.points[0], s.payload) ? [{ points: [...i.points], closed: !0 }, !0] : [{ points: [...i.points, s.payload], closed: !1 }, !0];
269
+ case "DELETE":
270
+ return [
271
+ {
272
+ points: [...i.points.filter((e, o) => s.payload !== o)],
273
+ closed: t > 4 && i.closed
274
+ },
275
+ !0
276
+ ];
277
+ case "MODIFY": {
278
+ const { x: e, y: o } = i.points[s.payload.index];
279
+ return [
280
+ { points: i.points.map(({ x: r, y: h }) => r === e && h === o ? {
281
+ x: s.payload.x,
282
+ y: s.payload.y
283
+ } : { x: r, y: h }), closed: i.closed },
284
+ !!(s.payload.x || s.payload.y)
285
+ ];
286
+ }
287
+ case "MOVE":
288
+ return [
289
+ {
290
+ points: i.points.map(({ x: e, y: o }) => ({
291
+ x: e + s.payload.x,
292
+ y: o + s.payload.y
293
+ })),
294
+ closed: i.closed
295
+ },
296
+ !!(s.payload.x || s.payload.y)
297
+ ];
298
+ case "CHANGE": {
299
+ const e = !m(s.payload, i.points);
300
+ return [
301
+ {
302
+ points: s.payload,
303
+ closed: e ? s.payload.length > 2 : i.closed
304
+ },
305
+ e
306
+ ];
307
+ }
308
+ case "RESET":
309
+ return [{ points: [], closed: !1 }, !!i.points.length];
310
+ default:
311
+ return [i, !1];
312
+ }
313
+ }
314
+ class E extends T {
315
+ constructor(t) {
316
+ super(t);
317
+ a(this, "state");
318
+ a(this, "imageRef", y());
319
+ a(this, "svgRef", y());
320
+ a(this, "svg", new C(() => {
321
+ var t;
322
+ return (t = this.svgRef) == null ? void 0 : t.current;
323
+ }));
324
+ a(this, "angles", []);
325
+ a(this, "path", {
326
+ points: [],
327
+ closed: !1
328
+ });
329
+ a(this, "lastEmittedPoints", []);
330
+ a(this, "lastUpdatedPoints", []);
331
+ a(this, "imgError", !1);
332
+ a(this, "setPathFromPropsOnMediaLoad", !0);
333
+ a(this, "hidePointer", () => {
334
+ const t = this.path.points[this.path.points.length - 1] || {
335
+ x: 0,
336
+ y: 0
337
+ };
338
+ this.setPointer({ ...t }, !0);
339
+ });
340
+ // Events
341
+ a(this, "onShapeDrag", ({ dx: t, dy: e }) => {
342
+ this.path.points.map(({ x: n, y: r }) => ({
343
+ x: n + t,
344
+ y: r + e
345
+ })).some((n) => this.svg.isAboveTheBorder(n)) || this.dispatchPathAction({
346
+ type: p.MOVE,
347
+ payload: { x: t, y: e }
348
+ });
349
+ });
350
+ a(this, "onPointDrag", (t, { dx: e, dy: o }) => {
351
+ const n = { ...this.path.points[t] };
352
+ n.x += e, n.y += o, this.svg.isAboveTheBorder(n) || this.dispatchPathAction({
353
+ type: p.MODIFY,
354
+ payload: { ...n, index: t }
355
+ });
356
+ });
357
+ a(this, "onPointClick", (t) => {
358
+ this.isLoaded() && !this.props.disabled && !this.path.closed && this.dispatchPathAction({
359
+ type: p.ADD,
360
+ payload: this.path.points[t]
361
+ });
362
+ });
363
+ a(this, "onDragEnd", () => {
364
+ this.checkIfPathUpdated(!1);
365
+ });
366
+ a(this, "onMediaLoaded", (t) => {
367
+ this.setPathFromPropsOnMediaLoad && (this.setPathStateFromProps(), this.setPathFromPropsOnMediaLoad = !1), this.imgError = !1, this.props.onImageLoad(t);
368
+ });
369
+ a(this, "onMediaError", (t) => {
370
+ this.dispatchPathAction({ type: p.RESET }), this.imgError = !0, this.props.onImageError(t);
371
+ });
372
+ a(this, "onClickTouchEvent", (t) => {
373
+ if (this.isLoaded() && !this.props.disabled) {
374
+ if (this.path.closed) {
375
+ t.target === this.svgRef.current && this.dispatchPathAction({
376
+ type: p.RESET
377
+ });
378
+ return;
379
+ }
380
+ const [e] = this.getMousePosition(t);
381
+ this.svg.isAboveTheBorder(e) ? this.hidePointer() : this.dispatchPathAction({
382
+ type: p.ADD,
383
+ payload: u(e, 1e3),
384
+ pointer: e
385
+ });
386
+ }
387
+ });
388
+ a(this, "onClick", (t) => {
389
+ this.onClickTouchEvent(t);
390
+ });
391
+ a(this, "onTouchEnd", (t) => {
392
+ t.cancelable && (t.preventDefault(), this.onClickTouchEvent(t)), this.hidePointer();
393
+ });
394
+ a(this, "onMouseTouchMove", (t) => {
395
+ if (this.isLoaded()) {
396
+ const [e] = this.getMousePosition(t);
397
+ this.setPointer(e);
398
+ }
399
+ });
400
+ a(this, "onContextMenu", (t) => {
401
+ if (this.isLoaded() && (t.preventDefault(), !this.props.disabled && !this.path.closed)) {
402
+ const [e, { index: o }] = this.getMousePosition(t);
403
+ o > -1 ? this.dispatchPathAction({
404
+ type: p.DELETE,
405
+ payload: o,
406
+ pointer: e
407
+ }) : this.setPointer(e);
408
+ }
409
+ });
410
+ this.state = {
411
+ path: {
412
+ points: [],
413
+ closed: !1
414
+ },
415
+ pointer: {
416
+ x: t.viewBox.width / 2,
417
+ y: t.viewBox.width / 2
418
+ }
419
+ };
420
+ }
421
+ render() {
422
+ return /* @__PURE__ */ M(
423
+ "div",
424
+ {
425
+ className: k({
426
+ ReactFreeSelect__Component: !0,
427
+ ReactFreeSelect__Closed: this.state.path.closed,
428
+ ReactFreeSelect__Disabled: this.props.disabled
429
+ }),
430
+ style: {
431
+ display: "inline-block",
432
+ position: "relative",
433
+ margin: "0",
434
+ padding: "0",
435
+ fontSize: "0",
436
+ cursor: this.props.disabled ? "not-allowed" : "default",
437
+ ...this.props.style
438
+ },
439
+ children: [
440
+ /* @__PURE__ */ g(
441
+ "img",
442
+ {
443
+ ref: this.imageRef,
444
+ src: this.props.src,
445
+ alt: this.props.imageAlt,
446
+ crossOrigin: this.props.crossOrigin,
447
+ style: this.props.imageStyle,
448
+ onLoad: this.onMediaLoaded,
449
+ onError: this.onMediaError
450
+ }
451
+ ),
452
+ /* @__PURE__ */ M(
453
+ "svg",
454
+ {
455
+ ref: this.svgRef,
456
+ style: {
457
+ position: "absolute",
458
+ top: "0",
459
+ left: "0",
460
+ width: "100%",
461
+ height: "100%",
462
+ overflow: "hidden",
463
+ userSelect: "none",
464
+ touchAction: "none"
465
+ },
466
+ viewBox: `0 0 ${this.props.viewBox.width} ${this.props.viewBox.height}`,
467
+ onMouseMove: this.onMouseTouchMove,
468
+ onTouchMove: this.onMouseTouchMove,
469
+ onClick: this.onClick,
470
+ onTouchEnd: this.onTouchEnd,
471
+ onContextMenu: this.onContextMenu,
472
+ onMouseLeave: this.hidePointer,
473
+ children: [
474
+ /* @__PURE__ */ g("rect", { visibility: "hidden" }),
475
+ !!this.state.path.points.length && /* @__PURE__ */ g(H, { path: this.getPolygonPoints() }),
476
+ /* @__PURE__ */ g(
477
+ U,
478
+ {
479
+ draggable: this.state.path.closed && !this.props.disabled,
480
+ onDrag: this.onShapeDrag,
481
+ onDragEnd: this.onDragEnd,
482
+ animate: !this.props.disabled,
483
+ path: this.getPolylinePoints()
484
+ }
485
+ ),
486
+ this.getRoundedPoints().map(({ x: t, y: e }, o) => /* @__PURE__ */ g(
487
+ Y,
488
+ {
489
+ x: t,
490
+ y: e,
491
+ draggable: !this.props.disabled && !this.props.disabledShapeChange,
492
+ style: {
493
+ cursor: !o && this.state.path.points.length > 2 && !this.state.path.closed ? "pointer" : void 0
494
+ },
495
+ onDrag: ({ dx: n, dy: r }) => this.onPointDrag(o, { dx: n, dy: r }),
496
+ onDragEnd: this.onDragEnd,
497
+ onClickTouchEvent: () => this.onPointClick(o)
498
+ },
499
+ o
500
+ ))
501
+ ]
502
+ }
503
+ )
504
+ ]
505
+ }
506
+ );
507
+ }
508
+ componentDidUpdate(t) {
509
+ !t.disabled && this.props.disabled && !this.path.closed && this.hidePointer(), t.src && t.src !== this.props.src ? this.dispatchPathAction({ type: p.RESET }) : m(t.value, this.props.value) || (this.isLoaded() ? this.setPathStateFromProps() : this.setPathFromPropsOnMediaLoad = !0);
510
+ }
511
+ convertPoints(t) {
512
+ const e = this.getAspectRatio();
513
+ return this.svg.convertViewboxPointsToReal(t).map(({ x: o, y: n }) => ({
514
+ x: Math.round(o / e.x),
515
+ y: Math.round(n / e.y)
516
+ }));
517
+ }
518
+ checkIfPathUpdated(t) {
519
+ if (this.path.closed || t) {
520
+ const e = this.convertPoints(this.path.points);
521
+ m(e, this.lastUpdatedPoints) || (this.emitOnComplete(e), this.lastUpdatedPoints = e.map(({ x: o, y: n }) => ({ x: o, y: n })));
522
+ }
523
+ }
524
+ emitOnChange({ points: t }) {
525
+ if (this.props.onChange) {
526
+ const e = this.convertPoints(t);
527
+ this.lastEmittedPoints = e, this.props.onChange(e);
528
+ }
529
+ }
530
+ emitOnComplete(t) {
531
+ this.props.onComplete && this.props.onComplete(t);
532
+ }
533
+ setPointer({ x: t, y: e }, o = !1) {
534
+ (o || !this.props.disabled) && this.setState({
535
+ path: this.path,
536
+ pointer: { x: t, y: e }
537
+ });
538
+ }
539
+ dispatchPathAction(t) {
540
+ const e = this.path.closed, [o, n] = j(this.path, t);
541
+ o.points = o.points.map((r) => u(r, 1e3)), n && (this.path = o, this.setState({
542
+ pointer: t.pointer || this.path.points[this.path.points.length - 1] || { x: 0, y: 0 },
543
+ path: o
544
+ }), this.angles = G(o.points), this.emitOnChange(o), [p.MODIFY, p.MOVE].includes(t.type) || this.checkIfPathUpdated(e));
545
+ }
546
+ isLoaded() {
547
+ if (this.imgError || !this.svgRef.current) return !1;
548
+ const t = this.svgRef.current;
549
+ return !!(t.width.baseVal.value && t.height.baseVal.value);
550
+ }
551
+ getAspectRatio() {
552
+ return this.imageRef.current ? {
553
+ x: this.imageRef.current.clientWidth / this.imageRef.current.naturalWidth,
554
+ y: this.imageRef.current.clientHeight / this.imageRef.current.naturalHeight
555
+ } : { x: NaN, y: NaN };
556
+ }
557
+ setPathStateFromProps() {
558
+ if (m(this.lastEmittedPoints, this.props.value)) return;
559
+ const t = this.getAspectRatio(), e = this.svg.convertRealPointsToViewbox(
560
+ this.props.value.map(({ x: o, y: n }) => ({
561
+ x: o * t.x,
562
+ y: n * t.y
563
+ }))
564
+ );
565
+ this.dispatchPathAction({
566
+ type: p.CHANGE,
567
+ payload: e
568
+ });
569
+ }
570
+ getRoundedPoints() {
571
+ return this.state.path.points.map((t) => u(t));
572
+ }
573
+ getBorder() {
574
+ return this.svg.getBorderPoints().map((t) => u(t)).map(({ x: t, y: e }) => ({ x: t - 1, y: e + 1 }));
575
+ }
576
+ getPolygonPoints() {
577
+ const t = this.getRoundedPoints(), e = this.getBorder();
578
+ return this.state.path.closed ? [...e, ...t, t[0], e[0]] : e;
579
+ }
580
+ getPolylinePoints() {
581
+ const t = this.getRoundedPoints();
582
+ return t.concat(
583
+ this.state.path.closed ? t[0] : u(this.state.pointer)
584
+ );
585
+ }
586
+ getMousePosition(t, e = !0, o = !0) {
587
+ let n = this.svg.getMouseCoordinates(t);
588
+ if (o) {
589
+ const c = navigator.platform.includes("Mac") ? t.metaKey : t.ctrlKey, d = this.path.points[this.path.points.length - 1];
590
+ c && d && (t.shiftKey ? n = F(d, n, this.angles) : n = N(d, n, Math.PI / 12));
591
+ }
592
+ const { point: r, index: h } = B(this.path.points, n, 10);
593
+ return e && h > -1 && (n = { ...r }), [n, { point: r, index: h }];
594
+ }
595
+ }
596
+ a(E, "propTypes", {
597
+ value: l.arrayOf(
598
+ l.exact({
599
+ x: l.number.isRequired,
600
+ y: l.number.isRequired
601
+ })
602
+ ),
603
+ style: l.shape({}),
604
+ viewBox: l.exact({
605
+ width: l.number.isRequired,
606
+ height: l.number.isRequired
607
+ }),
608
+ disabled: l.bool,
609
+ disabledShapeChange: l.bool,
610
+ onChange: l.func,
611
+ onComplete: l.func,
612
+ src: l.string.isRequired,
613
+ imageAlt: l.string,
614
+ crossOrigin: l.string,
615
+ imageStyle: l.shape({}),
616
+ onImageLoad: l.func,
617
+ onImageError: l.func
618
+ }), a(E, "defaultProps", {
619
+ value: [],
620
+ style: {},
621
+ imageStyle: {},
622
+ viewBox: { width: 1e3, height: 1e3 },
623
+ disabled: !1,
624
+ disabledShapeChange: !1,
625
+ onImageError: Function.prototype,
626
+ onImageLoad: Function.prototype
627
+ });
628
+ export {
629
+ E as ReactLassoSelect,
630
+ X as getCanvas
631
+ };