-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScreenDrawer.js
More file actions
317 lines (274 loc) · 9.09 KB
/
Copy pathScreenDrawer.js
File metadata and controls
317 lines (274 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
export class ScreenDrawer {
rerender;
svgElement = null;
path = null;
strPath;
strokeWidth = 12;
strokeWidthRect = 6;
bufferSize = 4;
buffer = [];
startPoint = null;
tool = 'rect';
color = '#EB144C';
mouseDown = null;
mouseMove = null;
mouseUp = null;
resizeListener = null;
pathBuffer = [];
constructor(rerender, trackScroll = false) {
const self = this;
this.rerender = rerender;
this.svgElement = document.querySelector('.bb-capture-svg');
this.svgElement.style.minHeight = `${document.documentElement.scrollHeight}px`;
// The overlay is position: fixed, so drawings detach from the content they
// mark as soon as the page scrolls after drawing (scrollbar drag while
// marking, or scrolling while the feedback form is open) — the DOM
// snapshot then stores the new scroll position and the rendered screenshot
// shows the marks shifted by exactly that scroll delta. With tracking
// enabled the overlay works in document coordinates instead and is
// counter-shifted by the current scroll offset: the fixed element then
// behaves like a document-anchored layer that always covers the viewport
// in both scroll directions, and the inline transform serializes into the
// snapshot so the replayed screenshot stays aligned too. The tracker must
// outlive destroy(): it keeps the preview overlay glued to the content
// while the form is open, and is released via destroyScrollTracker() when
// the capture editor is removed.
this.trackScroll = !!trackScroll;
this.scrollListener = null;
if (this.trackScroll) {
this.scrollListener = function () {
self.svgElement.style.transform = `translate(${-window.scrollX}px, ${-window.scrollY}px)`;
self.svgElement.style.minWidth = `${document.documentElement.scrollWidth}px`;
};
this.scrollListener();
window.addEventListener('scroll', this.scrollListener, { passive: true });
}
// Window resize listener.
this.resizeListener = function (e) {
self.svgElement.style.minHeight = `${document.documentElement.scrollHeight}px`;
};
window.addEventListener('resize', this.resizeListener, true);
this.mouseDown = function (e) {
e.preventDefault();
const colorpicker = document.querySelector('.bb-capture-toolbar-item-colorpicker');
if (colorpicker) {
colorpicker.style.display = 'none';
}
self.fadeOutToolbar();
if (self.tool === 'pen' || self.tool === 'blur') {
self.mouseDownPen(e);
}
if (self.tool === 'rect') {
self.mouseDownRect(e);
}
};
this.mouseMove = function (e) {
e.preventDefault();
if (self.tool === 'pen' || self.tool === 'blur') {
self.mouseMovePen(e);
}
if (self.tool === 'rect') {
self.mouseMoveRect(e);
}
};
this.mouseUp = function (e) {
e.preventDefault();
self.fadeInToolbar();
if (self.tool === 'pen' || self.tool === 'blur') {
self.mouseUpPen(e);
}
if (self.tool === 'rect') {
self.mouseUpRect(e);
}
};
this.svgElement.addEventListener('mousedown', this.mouseDown);
this.svgElement.addEventListener('mousemove', this.mouseMove);
this.svgElement.addEventListener('mouseup', this.mouseUp);
this.svgElement.addEventListener('touchstart', this.mouseDown, false);
this.svgElement.addEventListener('touchmove', this.mouseMove, false);
this.svgElement.addEventListener('touchend', this.mouseUp, false);
setTimeout(() => {
this.rerender();
}, 100);
}
clear() {
if (this.svgElement) {
while (this.svgElement.firstChild) {
this.svgElement.firstChild.remove();
}
}
}
destroy() {
this.svgElement.removeEventListener('mousedown', this.mouseDown);
this.svgElement.removeEventListener('mousemove', this.mouseMove);
this.svgElement.removeEventListener('mouseup', this.mouseUp);
this.svgElement.removeEventListener('touchstart', this.mouseDown);
this.svgElement.removeEventListener('touchmove', this.mouseMove);
this.svgElement.removeEventListener('touchend', this.mouseUp);
window.removeEventListener('resize', this.resizeListener);
// The scroll tracker intentionally survives destroy(): the preview overlay
// must keep tracking scrolling while the feedback form is open.
}
destroyScrollTracker() {
if (this.scrollListener) {
window.removeEventListener('scroll', this.scrollListener);
this.scrollListener = null;
}
}
mouseUpPen() {
if (this.path) {
this.path = null;
}
}
mouseUpRect() {
if (this.path) {
this.path = null;
}
}
mouseMovePen(e) {
if (this.path) {
this.appendToBuffer(this.getMousePosition(e));
this.updateSvgPath();
}
}
mouseMoveRect(e) {
if (this.path) {
var p = this.getMousePosition(e);
var w = Math.abs(p.x - this.startPoint.x);
var h = Math.abs(p.y - this.startPoint.y);
var x = p.x;
var y = p.y;
if (p.x > this.startPoint.x) {
x = this.startPoint.x;
}
if (p.y > this.startPoint.y) {
y = this.startPoint.y;
}
this.path.setAttributeNS(null, 'x', x);
this.path.setAttributeNS(null, 'y', y);
this.path.setAttributeNS(null, 'width', w);
this.path.setAttributeNS(null, 'height', h);
}
}
mouseDownRect(e) {
this.path = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
this.path.setAttribute('fill', 'none');
this.path.setAttribute('stroke', this.color);
this.path.setAttribute('stroke-linecap', 'round');
this.path.setAttribute('stroke-width', this.strokeWidthRect);
this.startPoint = this.getMousePosition(e);
this.appendPathToSvg(this.path);
}
mouseDownPen(e) {
var color = this.color + 'AA';
var strokeWidth = this.strokeWidth;
if (this.tool === 'blur') {
color = '#000000';
strokeWidth = 40;
}
this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
this.path.setAttribute('fill', 'none');
this.path.setAttribute('stroke', color);
this.path.setAttribute('stroke-linecap', 'round');
this.path.setAttribute('stroke-width', strokeWidth);
this.buffer = [];
var pt = this.getMousePosition(e);
this.appendToBuffer(pt);
this.strPath = 'M' + pt.x + ' ' + pt.y;
this.path.setAttribute('d', this.strPath);
this.appendPathToSvg(this.path);
}
setTool(tool) {
this.tool = tool;
}
setColor(color) {
this.color = color;
}
getMousePosition(e) {
// When scroll tracking is active the overlay works in document
// coordinates (counter-shifted by the scroll offset), so map viewport
// coordinates into document space to keep drawing under the cursor.
const offsetX = this.trackScroll ? window.scrollX : 0;
const offsetY = this.trackScroll ? window.scrollY : 0;
if (e.touches && e.touches.length > 0) {
return {
x: e.touches[0].clientX + offsetX,
y: e.touches[0].clientY + offsetY,
};
}
return {
x: e.clientX + offsetX,
y: e.clientY + offsetY,
};
}
// Calculate the average point, starting at offset in the buffer
getAveragePoint(offset) {
var len = this.buffer.length;
if (len % 2 === 1 || len >= this.bufferSize) {
var totalX = 0;
var totalY = 0;
var pt, i;
var count = 0;
for (i = offset; i < len; i++) {
count++;
pt = this.buffer[i];
totalX += pt.x;
totalY += pt.y;
}
return {
x: totalX / count,
y: totalY / count,
};
}
return null;
}
updateSvgPath() {
var pt = this.getAveragePoint(0);
if (pt) {
// Get the smoothed part of the path that will not change
this.strPath += ' L' + pt.x + ' ' + pt.y;
// Get the last part of the path (close to the current mouse position)
// This part will change if the mouse moves again
var tmpPath = '';
for (var offset = 2; offset < this.buffer.length; offset += 2) {
pt = this.getAveragePoint(offset);
tmpPath += ' L' + pt.x + ' ' + pt.y;
}
// Set the complete current path coordinates
this.path.setAttribute('d', this.strPath + tmpPath);
}
}
appendToBuffer(pt) {
this.buffer.push(pt);
while (this.buffer.length > this.bufferSize) {
this.buffer.shift();
}
}
appendPathToSvg(path) {
this.svgElement.appendChild(path);
this.pathBuffer.push(path);
this.rerender();
}
removeLastAddedPathFromSvg() {
if (this.pathBuffer.length <= 0 || !this.svgElement) {
return;
}
this.svgElement.removeChild(this.pathBuffer[this.pathBuffer.length - 1]);
this.pathBuffer.pop();
this.rerender();
}
fadeOutToolbar() {
var fadeTarget = document.querySelector('.bb-capture-toolbar');
if (fadeTarget) {
fadeTarget.style.opacity = 0;
fadeTarget.style.pointerEvents = 'none';
}
}
fadeInToolbar() {
var fadeTarget = document.querySelector('.bb-capture-toolbar');
if (fadeTarget) {
fadeTarget.style.opacity = 1;
fadeTarget.style.pointerEvents = 'auto';
}
}
}