-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
zoom.js
712 lines (596 loc) · 16.9 KB
/
zoom.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/**
* @author NHN. FE Development Team <[email protected]>
* @fileoverview Image zoom module (start zoom, end zoom)
*/
import fabric from 'fabric';
import Component from '@/interface/component';
import { clamp } from '@/util';
import { componentNames, eventNames, keyCodes, zoomModes } from '@/consts';
const MOUSE_MOVE_THRESHOLD = 10;
const DEFAULT_SCROLL_OPTION = {
left: 0,
top: 0,
width: 0,
height: 0,
stroke: '#000000',
strokeWidth: 0,
fill: '#000000',
opacity: 0.4,
evented: false,
selectable: false,
hoverCursor: 'auto',
};
const DEFAULT_VERTICAL_SCROLL_RATIO = {
SIZE: 0.0045,
MARGIN: 0.003,
BORDER_RADIUS: 0.003,
};
const DEFAULT_HORIZONTAL_SCROLL_RATIO = {
SIZE: 0.0066,
MARGIN: 0.0044,
BORDER_RADIUS: 0.003,
};
const DEFAULT_ZOOM_LEVEL = 1.0;
const {
ZOOM_CHANGED,
ADD_TEXT,
TEXT_EDITING,
OBJECT_MODIFIED,
KEY_DOWN,
KEY_UP,
HAND_STARTED,
HAND_STOPPED,
} = eventNames;
/**
* Zoom components
* @param {Graphics} graphics - Graphics instance
* @extends {Component}
* @class Zoom
* @ignore
*/
class Zoom extends Component {
constructor(graphics) {
super(componentNames.ZOOM, graphics);
/**
* zoomArea
* @type {?fabric.Rect}
* @private
*/
this.zoomArea = null;
/**
* Start point of zoom area
* @type {?{x: number, y: number}}
*/
this._startPoint = null;
/**
* Center point of every zoom
* @type {Array.<{prevZoomLevel: number, zoomLevel: number, x: number, y: number}>}
*/
this._centerPoints = [];
/**
* Zoom level (default: 100%(1.0), max: 400%(4.0))
* @type {number}
*/
this.zoomLevel = DEFAULT_ZOOM_LEVEL;
/**
* Zoom mode ('normal', 'zoom', 'hand')
* @type {string}
*/
this.zoomMode = zoomModes.DEFAULT;
/**
* Listeners
* @type {Object.<string, Function>}
* @private
*/
this._listeners = {
startZoom: this._onMouseDownWithZoomMode.bind(this),
moveZoom: this._onMouseMoveWithZoomMode.bind(this),
stopZoom: this._onMouseUpWithZoomMode.bind(this),
startHand: this._onMouseDownWithHandMode.bind(this),
moveHand: this._onMouseMoveWithHandMode.bind(this),
stopHand: this._onMouseUpWithHandMode.bind(this),
zoomChanged: this._changeScrollState.bind(this),
keydown: this._startHandModeWithSpaceBar.bind(this),
keyup: this._endHandModeWithSpaceBar.bind(this),
};
const canvas = this.getCanvas();
/**
* Width:Height ratio (ex. width=1.5, height=1 -> aspectRatio=1.5)
* @private
*/
this.aspectRatio = canvas.width / canvas.height;
/**
* vertical scroll bar
* @type {fabric.Rect}
* @private
*/
this._verticalScroll = new fabric.Rect(DEFAULT_SCROLL_OPTION);
/**
* horizontal scroll bar
* @type {fabric.Rect}
* @private
*/
this._horizontalScroll = new fabric.Rect(DEFAULT_SCROLL_OPTION);
canvas.on(ZOOM_CHANGED, this._listeners.zoomChanged);
this.graphics.on(ADD_TEXT, this._startTextEditingHandler.bind(this));
this.graphics.on(TEXT_EDITING, this._startTextEditingHandler.bind(this));
this.graphics.on(OBJECT_MODIFIED, this._stopTextEditingHandler.bind(this));
}
/**
* Attach zoom keyboard events
*/
attachKeyboardZoomEvents() {
fabric.util.addListener(document, KEY_DOWN, this._listeners.keydown);
fabric.util.addListener(document, KEY_UP, this._listeners.keyup);
}
/**
* Detach zoom keyboard events
*/
detachKeyboardZoomEvents() {
fabric.util.removeListener(document, KEY_DOWN, this._listeners.keydown);
fabric.util.removeListener(document, KEY_UP, this._listeners.keyup);
}
/**
* Handler when you started editing text
* @private
*/
_startTextEditingHandler() {
this.isTextEditing = true;
}
/**
* Handler when you stopped editing text
* @private
*/
_stopTextEditingHandler() {
this.isTextEditing = false;
}
/**
* Handler who turns on hand mode when the space bar is down
* @param {KeyboardEvent} e - Event object
* @private
*/
_startHandModeWithSpaceBar(e) {
if (this.withSpace || this.isTextEditing) {
return;
}
if (e.keyCode === keyCodes.SPACE) {
this.withSpace = true;
this.startHandMode();
}
}
/**
* Handler who turns off hand mode when space bar is up
* @param {KeyboardEvent} e - Event object
* @private
*/
_endHandModeWithSpaceBar(e) {
if (e.keyCode === keyCodes.SPACE) {
this.withSpace = false;
this.endHandMode();
}
}
/**
* Start zoom-in mode
*/
startZoomInMode() {
if (this.zoomArea) {
return;
}
this.endHandMode();
this.zoomMode = zoomModes.ZOOM;
const canvas = this.getCanvas();
this._changeObjectsEventedState(false);
this.zoomArea = new fabric.Rect({
left: 0,
top: 0,
width: 0.5,
height: 0.5,
stroke: 'black',
strokeWidth: 1,
fill: 'transparent',
hoverCursor: 'zoom-in',
});
canvas.discardActiveObject();
canvas.add(this.zoomArea);
canvas.on('mouse:down', this._listeners.startZoom);
canvas.selection = false;
canvas.defaultCursor = 'zoom-in';
}
/**
* End zoom-in mode
*/
endZoomInMode() {
this.zoomMode = zoomModes.DEFAULT;
const canvas = this.getCanvas();
const { startZoom, moveZoom, stopZoom } = this._listeners;
canvas.selection = true;
canvas.defaultCursor = 'auto';
canvas.off({
'mouse:down': startZoom,
'mouse:move': moveZoom,
'mouse:up': stopZoom,
});
this._changeObjectsEventedState(true);
canvas.remove(this.zoomArea);
this.zoomArea = null;
}
/**
* Start zoom drawing mode
*/
start() {
this.zoomArea = null;
this._startPoint = null;
this._startHandPoint = null;
}
/**
* Stop zoom drawing mode
*/
end() {
this.endZoomInMode();
this.endHandMode();
}
/**
* Start hand mode
*/
startHandMode() {
this.endZoomInMode();
this.zoomMode = zoomModes.HAND;
const canvas = this.getCanvas();
this._changeObjectsEventedState(false);
canvas.discardActiveObject();
canvas.off('mouse:down', this._listeners.startHand);
canvas.on('mouse:down', this._listeners.startHand);
canvas.selection = false;
canvas.defaultCursor = 'grab';
canvas.fire(HAND_STARTED);
}
/**
* Stop hand mode
*/
endHandMode() {
this.zoomMode = zoomModes.DEFAULT;
const canvas = this.getCanvas();
this._changeObjectsEventedState(true);
canvas.off('mouse:down', this._listeners.startHand);
canvas.selection = true;
canvas.defaultCursor = 'auto';
this._startHandPoint = null;
canvas.fire(HAND_STOPPED);
}
/**
* onMousedown handler in fabric canvas
* @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event
* @private
*/
_onMouseDownWithZoomMode({ target, e }) {
if (target) {
return;
}
const canvas = this.getCanvas();
canvas.selection = false;
this._startPoint = canvas.getPointer(e);
this.zoomArea.set({ width: 0, height: 0 });
const { moveZoom, stopZoom } = this._listeners;
canvas.on({
'mouse:move': moveZoom,
'mouse:up': stopZoom,
});
}
/**
* onMousemove handler in fabric canvas
* @param {{e: MouseEvent}} fEvent - Fabric event
* @private
*/
_onMouseMoveWithZoomMode({ e }) {
const canvas = this.getCanvas();
const pointer = canvas.getPointer(e);
const { x, y } = pointer;
const { zoomArea, _startPoint } = this;
const deltaX = Math.abs(x - _startPoint.x);
const deltaY = Math.abs(y - _startPoint.y);
if (deltaX + deltaY > MOUSE_MOVE_THRESHOLD) {
canvas.remove(zoomArea);
zoomArea.set(this._calcRectDimensionFromPoint(x, y));
canvas.add(zoomArea);
}
}
/**
* Get rect dimension setting from Canvas-Mouse-Position(x, y)
* @param {number} x - Canvas-Mouse-Position x
* @param {number} y - Canvas-Mouse-Position Y
* @returns {{left: number, top: number, width: number, height: number}}
* @private
*/
_calcRectDimensionFromPoint(x, y) {
const canvas = this.getCanvas();
const canvasWidth = canvas.getWidth();
const canvasHeight = canvas.getHeight();
const { x: startX, y: startY } = this._startPoint;
const { min } = Math;
const left = min(startX, x);
const top = min(startY, y);
const width = clamp(x, startX, canvasWidth) - left; // (startX <= x(mouse) <= canvasWidth) - left
const height = clamp(y, startY, canvasHeight) - top; // (startY <= y(mouse) <= canvasHeight) - top
return { left, top, width, height };
}
/**
* onMouseup handler in fabric canvas
* @private
*/
_onMouseUpWithZoomMode() {
let { zoomLevel } = this;
const { zoomArea } = this;
const { moveZoom, stopZoom } = this._listeners;
const canvas = this.getCanvas();
const center = this._getCenterPoint();
const { x, y } = center;
if (!this._isMaxZoomLevel()) {
this._centerPoints.push({
x,
y,
prevZoomLevel: zoomLevel,
zoomLevel: zoomLevel + 1,
});
zoomLevel += 1;
canvas.zoomToPoint({ x, y }, zoomLevel);
this._fireZoomChanged(canvas, zoomLevel);
this.zoomLevel = zoomLevel;
}
canvas.off({
'mouse:move': moveZoom,
'mouse:up': stopZoom,
});
canvas.remove(zoomArea);
this._startPoint = null;
}
/**
* Get center point
* @returns {{x: number, y: number}}
* @private
*/
_getCenterPoint() {
const { left, top, width, height } = this.zoomArea;
const { x, y } = this._startPoint;
const { aspectRatio } = this;
if (width < MOUSE_MOVE_THRESHOLD && height < MOUSE_MOVE_THRESHOLD) {
return { x, y };
}
return width > height
? { x: left + (aspectRatio * height) / 2, y: top + height / 2 }
: { x: left + width / 2, y: top + width / aspectRatio / 2 };
}
/**
* Zoom the canvas
* @param {{x: number, y: number}} center - center of zoom
* @param {?number} zoomLevel - zoom level
*/
zoom({ x, y }, zoomLevel = this.zoomLevel) {
const canvas = this.getCanvas();
const centerPoints = this._centerPoints;
for (let i = centerPoints.length - 1; i >= 0; i -= 1) {
if (centerPoints[i].zoomLevel < zoomLevel) {
break;
}
const { x: prevX, y: prevY, prevZoomLevel } = centerPoints.pop();
canvas.zoomToPoint({ x: prevX, y: prevY }, prevZoomLevel);
this.zoomLevel = prevZoomLevel;
}
canvas.zoomToPoint({ x, y }, zoomLevel);
if (!this._isDefaultZoomLevel(zoomLevel)) {
this._centerPoints.push({
x,
y,
zoomLevel,
prevZoomLevel: this.zoomLevel,
});
}
this.zoomLevel = zoomLevel;
this._fireZoomChanged(canvas, zoomLevel);
}
/**
* Zoom out one step
*/
zoomOut() {
const centerPoints = this._centerPoints;
if (!centerPoints.length) {
return;
}
const canvas = this.getCanvas();
const point = centerPoints.pop();
const { x, y, prevZoomLevel } = point;
if (this._isDefaultZoomLevel(prevZoomLevel)) {
canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
} else {
canvas.zoomToPoint({ x, y }, prevZoomLevel);
}
this.zoomLevel = prevZoomLevel;
this._fireZoomChanged(canvas, this.zoomLevel);
}
/**
* Zoom reset
*/
resetZoom() {
const canvas = this.getCanvas();
canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
this.zoomLevel = DEFAULT_ZOOM_LEVEL;
this._centerPoints = [];
this._fireZoomChanged(canvas, this.zoomLevel);
}
/**
* Whether zoom level is max (5.0)
* @returns {boolean}
* @private
*/
_isMaxZoomLevel() {
return this.zoomLevel >= 5.0;
}
/**
* Move point of zoom
* @param {{x: number, y: number}} delta - move amount
* @private
*/
_movePointOfZoom({ x: deltaX, y: deltaY }) {
const centerPoints = this._centerPoints;
if (!centerPoints.length) {
return;
}
const canvas = this.getCanvas();
const { zoomLevel } = this;
const point = centerPoints.pop();
const { x: originX, y: originY, prevZoomLevel } = point;
const x = originX - deltaX;
const y = originY - deltaY;
canvas.zoomToPoint({ x: originX, y: originY }, prevZoomLevel);
canvas.zoomToPoint({ x, y }, zoomLevel);
centerPoints.push({ x, y, prevZoomLevel, zoomLevel });
this._fireZoomChanged(canvas, zoomLevel);
}
/**
* onMouseDown handler in fabric canvas
* @param {{target: fabric.Object, e: MouseEvent}} fEvent - Fabric event
* @private
*/
_onMouseDownWithHandMode({ target, e }) {
if (target) {
return;
}
const canvas = this.getCanvas();
if (this.zoomLevel <= DEFAULT_ZOOM_LEVEL) {
return;
}
canvas.selection = false;
this._startHandPoint = canvas.getPointer(e);
const { moveHand, stopHand } = this._listeners;
canvas.on({
'mouse:move': moveHand,
'mouse:up': stopHand,
});
}
/**
* onMouseMove handler in fabric canvas
* @param {{e: MouseEvent}} fEvent - Fabric event
* @private
*/
_onMouseMoveWithHandMode({ e }) {
const canvas = this.getCanvas();
const { x, y } = canvas.getPointer(e);
const deltaX = x - this._startHandPoint.x;
const deltaY = y - this._startHandPoint.y;
this._movePointOfZoom({ x: deltaX, y: deltaY });
}
/**
* onMouseUp handler in fabric canvas
* @private
*/
_onMouseUpWithHandMode() {
const canvas = this.getCanvas();
const { moveHand, stopHand } = this._listeners;
canvas.off({
'mouse:move': moveHand,
'mouse:up': stopHand,
});
this._startHandPoint = null;
}
/**
* onChangeZoom handler in fabric canvas
* @private
*/
_changeScrollState({ viewport, zoomLevel }) {
const canvas = this.getCanvas();
canvas.remove(this._verticalScroll);
canvas.remove(this._horizontalScroll);
if (this._isDefaultZoomLevel(zoomLevel)) {
return;
}
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const { tl, tr, bl } = viewport;
const viewportWidth = tr.x - tl.x;
const viewportHeight = bl.y - tl.y;
const horizontalScrollWidth = (viewportWidth * viewportWidth) / canvasWidth;
const horizontalScrollHeight = viewportHeight * DEFAULT_HORIZONTAL_SCROLL_RATIO.SIZE;
const horizontalScrollLeft = clamp(
tl.x + (tl.x / canvasWidth) * viewportWidth,
tl.x,
tr.x - horizontalScrollWidth
);
const horizontalScrollMargin = viewportHeight * DEFAULT_HORIZONTAL_SCROLL_RATIO.MARGIN;
const horizontalScrollBorderRadius =
viewportHeight * DEFAULT_HORIZONTAL_SCROLL_RATIO.BORDER_RADIUS;
this._horizontalScroll.set({
left: horizontalScrollLeft,
top: bl.y - horizontalScrollHeight - horizontalScrollMargin,
width: horizontalScrollWidth,
height: horizontalScrollHeight,
rx: horizontalScrollBorderRadius,
ry: horizontalScrollBorderRadius,
});
const verticalScrollWidth = viewportWidth * DEFAULT_VERTICAL_SCROLL_RATIO.SIZE;
const verticalScrollHeight = (viewportHeight * viewportHeight) / canvasHeight;
const verticalScrollTop = clamp(
tl.y + (tl.y / canvasHeight) * viewportHeight,
tr.y,
bl.y - verticalScrollHeight
);
const verticalScrollMargin = viewportWidth * DEFAULT_VERTICAL_SCROLL_RATIO.MARGIN;
const verticalScrollBorderRadius = viewportWidth * DEFAULT_VERTICAL_SCROLL_RATIO.BORDER_RADIUS;
this._verticalScroll.set({
left: tr.x - verticalScrollWidth - verticalScrollMargin,
top: verticalScrollTop,
width: verticalScrollWidth,
height: verticalScrollHeight,
rx: verticalScrollBorderRadius,
ry: verticalScrollBorderRadius,
});
this._addScrollBar();
}
/**
* Change objects 'evented' state
* @param {boolean} [evented=true] - objects 'evented' state
*/
_changeObjectsEventedState(evented = true) {
const canvas = this.getCanvas();
canvas.forEachObject((obj) => {
// {@link https://fabricjs.com/docs/fabric.Object.html#evented}
obj.evented = evented;
});
}
/**
* Add scroll bar and set remove timer
*/
_addScrollBar() {
const canvas = this.getCanvas();
canvas.add(this._horizontalScroll);
canvas.add(this._verticalScroll);
if (this.scrollBarTid) {
clearTimeout(this.scrollBarTid);
}
this.scrollBarTid = setTimeout(() => {
canvas.remove(this._horizontalScroll);
canvas.remove(this._verticalScroll);
}, 3000);
}
/**
* Check zoom level is default zoom level (1.0)
* @param {number} zoomLevel - zoom level
* @returns {boolean} - whether zoom level is 1.0
*/
_isDefaultZoomLevel(zoomLevel) {
return zoomLevel === DEFAULT_ZOOM_LEVEL;
}
/**
* Fire 'zoomChanged' event
* @param {fabric.Canvas} canvas - fabric canvas
* @param {number} zoomLevel - 'zoomChanged' event params
*/
_fireZoomChanged(canvas, zoomLevel) {
canvas.fire(ZOOM_CHANGED, { viewport: canvas.calcViewportBoundaries(), zoomLevel });
}
/**
* Get zoom mode
*/
get mode() {
return this.zoomMode;
}
}
export default Zoom;