forked from playcanvas/supersplat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasso-selection.ts
157 lines (126 loc) · 5.05 KB
/
lasso-selection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Events } from '../events';
type Point = { x: number, y: number };
class LassoSelection {
activate: () => void;
deactivate: () => void;
constructor(events: Events, parent: HTMLElement, mask: { canvas: HTMLCanvasElement, context: CanvasRenderingContext2D }) {
let points: Point[] = [];
let currentPoint: Point = null;
let lastPointTime = 0;
// create svg
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.id = 'lasso-select-svg';
svg.classList.add('select-svg');
// create polygon element
const polygon = document.createElementNS(svg.namespaceURI, 'polygon') as SVGPolygonElement;
polygon.setAttribute('fill', 'none');
polygon.setAttribute('stroke-width', '1');
polygon.setAttribute('stroke-dasharray', '5, 5');
polygon.setAttribute('stroke-dashoffset', '0');
// create canvas
const { canvas, context } = mask;
const dist = (a: Point, b: Point) => {
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
};
const isClosed = () => {
return points.length > 1 && dist(currentPoint, points[0]) < 8;
};
const paint = () => {
polygon.setAttribute('points', [...points, currentPoint].reduce((prev, current) => `${prev}${current.x}, ${current.y} `, ''));
polygon.setAttribute('stroke', isClosed() ? '#fa6' : '#f60');
};
let dragId: number | undefined;
const update = (e: PointerEvent) => {
currentPoint = { x: e.offsetX, y: e.offsetY };
const distance = points.length === 0 ? 0 : dist(currentPoint, points[points.length - 1]);
const millis = Date.now() - lastPointTime;
const preventCorners = distance > 20;
const slowNarrowSpacing = millis > 500 && distance > 2;
const fasterMediumSpacing = millis > 200 && distance > 10;
const firstPoints = points.length === 0;
if (dragId !== undefined && (preventCorners || slowNarrowSpacing || fasterMediumSpacing || firstPoints)) {
points.push(currentPoint);
lastPointTime = Date.now();
}
paint();
};
const commitSelection = (e: PointerEvent) => {
// initialize canvas
if (canvas.width !== parent.clientWidth || canvas.height !== parent.clientHeight) {
canvas.width = parent.clientWidth;
canvas.height = parent.clientHeight;
}
// clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.fillStyle = '#f60';
context.beginPath();
points.forEach((p, idx) => {
if (idx === 0) {
context.moveTo(p.x, p.y);
} else {
context.lineTo(p.x, p.y);
}
});
context.closePath();
context.fill();
events.fire(
'select.byMask',
e.shiftKey ? 'add' : (e.ctrlKey ? 'remove' : 'set'),
canvas,
context
);
};
const pointerdown = (e: PointerEvent) => {
if (dragId === undefined && (e.pointerType === 'mouse' ? e.button === 0 : e.isPrimary)) {
e.preventDefault();
e.stopPropagation();
dragId = e.pointerId;
parent.setPointerCapture(dragId);
update(e);
}
};
const pointermove = (e: PointerEvent) => {
if (dragId !== undefined) {
e.preventDefault();
e.stopPropagation();
}
update(e);
};
const dragEnd = () => {
parent.releasePointerCapture(dragId);
dragId = undefined;
};
const pointerup = (e: PointerEvent) => {
if (e.pointerId === dragId) {
e.preventDefault();
e.stopPropagation();
dragEnd();
commitSelection(e);
points = [];
paint();
}
};
this.activate = () => {
svg.style.display = 'inline';
parent.style.display = 'block';
parent.addEventListener('pointerdown', pointerdown);
parent.addEventListener('pointermove', pointermove);
parent.addEventListener('pointerup', pointerup);
};
this.deactivate = () => {
// cancel active operation
if (dragId !== undefined) {
dragEnd();
}
svg.style.display = 'none';
parent.style.display = 'none';
parent.removeEventListener('pointerdown', pointerdown);
parent.removeEventListener('pointermove', pointermove);
parent.removeEventListener('pointerup', pointerup);
};
svg.appendChild(polygon);
parent.appendChild(svg);
}
}
export { LassoSelection };