jellies-draw 0.3.2 → 0.3.4
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/package.json
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
|
2
|
+
<circle cx="12" cy="12" r="10" fill="rgba(243, 59, 41, 0.2)"/>
|
|
3
|
+
<circle cx="12" cy="12" r="6" fill="rgba(243, 59, 41, 0.5)"/>
|
|
4
|
+
<circle cx="12" cy="12" r="3" fill="rgb(255, 110, 90)"/>
|
|
5
|
+
</svg>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Konva from 'konva';
|
|
2
2
|
import Properties from './properties';
|
|
3
|
+
import laserCursorUrl from '../../assets/laser-cursor.svg';
|
|
3
4
|
|
|
4
5
|
const FADE_DURATION = 700;
|
|
5
6
|
const DRAG_THRESHOLD = 2;
|
|
@@ -23,6 +24,7 @@ export default {
|
|
|
23
24
|
_onMouseDown: null,
|
|
24
25
|
_onMouseMove: null,
|
|
25
26
|
_onMouseUp: null,
|
|
27
|
+
_onSelectStart: null,
|
|
26
28
|
_onTick: null,
|
|
27
29
|
|
|
28
30
|
initialize(stage, container) {
|
|
@@ -31,6 +33,7 @@ export default {
|
|
|
31
33
|
this._onMouseDown = this._handleMouseDown.bind(this);
|
|
32
34
|
this._onMouseMove = this._handleMouseMove.bind(this);
|
|
33
35
|
this._onMouseUp = this._handleMouseUp.bind(this);
|
|
36
|
+
this._onSelectStart = this._handleSelectStart.bind(this);
|
|
34
37
|
this._onTick = this._tick.bind(this);
|
|
35
38
|
this.layer = new Konva.Layer({ listening: false });
|
|
36
39
|
this.shape = new Konva.Shape({
|
|
@@ -62,8 +65,11 @@ export default {
|
|
|
62
65
|
window.addEventListener('pointermove', this._onMouseMove, true);
|
|
63
66
|
window.addEventListener('pointerup', this._onMouseUp, true);
|
|
64
67
|
window.addEventListener('pointercancel', this._onMouseUp, true);
|
|
65
|
-
this.
|
|
66
|
-
|
|
68
|
+
window.addEventListener('selectstart', this._onSelectStart, true);
|
|
69
|
+
this._cursorStyleEl = document.createElement('style');
|
|
70
|
+
this._cursorStyleEl.setAttribute('data-jellies-draw-laser-cursor', '');
|
|
71
|
+
this._cursorStyleEl.textContent = `*, *::before, *::after { cursor: url("${laserCursorUrl}") 12 12, auto !important; }`;
|
|
72
|
+
document.head.appendChild(this._cursorStyleEl);
|
|
67
73
|
this.rafId = requestAnimationFrame(this._onTick);
|
|
68
74
|
},
|
|
69
75
|
|
|
@@ -74,8 +80,11 @@ export default {
|
|
|
74
80
|
window.removeEventListener('pointermove', this._onMouseMove, true);
|
|
75
81
|
window.removeEventListener('pointerup', this._onMouseUp, true);
|
|
76
82
|
window.removeEventListener('pointercancel', this._onMouseUp, true);
|
|
77
|
-
|
|
78
|
-
this.
|
|
83
|
+
window.removeEventListener('selectstart', this._onSelectStart, true);
|
|
84
|
+
if (this._cursorStyleEl) {
|
|
85
|
+
this._cursorStyleEl.remove();
|
|
86
|
+
this._cursorStyleEl = null;
|
|
87
|
+
}
|
|
79
88
|
if (this.rafId !== null) {
|
|
80
89
|
cancelAnimationFrame(this.rafId);
|
|
81
90
|
this.rafId = null;
|
|
@@ -112,6 +121,10 @@ export default {
|
|
|
112
121
|
this.startY = pt.y;
|
|
113
122
|
this.lastX = pt.x;
|
|
114
123
|
this.lastY = pt.y;
|
|
124
|
+
this._downClientX = e.clientX;
|
|
125
|
+
this._downClientY = e.clientY;
|
|
126
|
+
e.stopPropagation();
|
|
127
|
+
e.preventDefault();
|
|
115
128
|
},
|
|
116
129
|
|
|
117
130
|
_handleMouseMove(e) {
|
|
@@ -125,7 +138,11 @@ export default {
|
|
|
125
138
|
if (!this.hasDragged) {
|
|
126
139
|
const dx = pt.x - this.startX;
|
|
127
140
|
const dy = pt.y - this.startY;
|
|
128
|
-
if (dx * dx + dy * dy < DRAG_THRESHOLD * DRAG_THRESHOLD)
|
|
141
|
+
if (dx * dx + dy * dy < DRAG_THRESHOLD * DRAG_THRESHOLD) {
|
|
142
|
+
e.stopPropagation();
|
|
143
|
+
e.preventDefault();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
129
146
|
this.hasDragged = true;
|
|
130
147
|
this.segments.push({
|
|
131
148
|
x1: this.startX, y1: this.startY,
|
|
@@ -146,12 +163,24 @@ export default {
|
|
|
146
163
|
},
|
|
147
164
|
|
|
148
165
|
_handleMouseUp(e) {
|
|
149
|
-
if (this.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
166
|
+
if (!this.isPressed) return;
|
|
167
|
+
const wasClick = !this.hasDragged && e.type === 'pointerup';
|
|
168
|
+
const downX = this._downClientX;
|
|
169
|
+
const downY = this._downClientY;
|
|
153
170
|
this.isPressed = false;
|
|
154
171
|
this.hasDragged = false;
|
|
172
|
+
e.stopPropagation();
|
|
173
|
+
e.preventDefault();
|
|
174
|
+
if (wasClick) {
|
|
175
|
+
const target = document.elementFromPoint(downX, downY);
|
|
176
|
+
if (target && typeof target.click === 'function') {
|
|
177
|
+
target.click();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
_handleSelectStart(e) {
|
|
183
|
+
e.preventDefault();
|
|
155
184
|
},
|
|
156
185
|
|
|
157
186
|
_tick() {
|
|
@@ -180,9 +209,16 @@ export default {
|
|
|
180
209
|
ctx.lineJoin = 'round';
|
|
181
210
|
|
|
182
211
|
ctx.beginPath();
|
|
183
|
-
|
|
212
|
+
let lastX = null;
|
|
213
|
+
let lastY = null;
|
|
184
214
|
for (let i = 0; i < segs.length; i++) {
|
|
185
|
-
|
|
215
|
+
const seg = segs[i];
|
|
216
|
+
if (seg.x1 !== lastX || seg.y1 !== lastY) {
|
|
217
|
+
ctx.moveTo(seg.x1, seg.y1);
|
|
218
|
+
}
|
|
219
|
+
ctx.lineTo(seg.x2, seg.y2);
|
|
220
|
+
lastX = seg.x2;
|
|
221
|
+
lastY = seg.y2;
|
|
186
222
|
}
|
|
187
223
|
|
|
188
224
|
const coreWidth = Properties.strokeWidth || 4;
|