File size: 16,697 Bytes
b5ba7a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * File to add generic rendering functions and shared utilities
 */

const _tool = {
	/**
	 *  Draws a reticle used for image generation
	 *
	 * @param {BoundingBox} bb The bounding box of the reticle (world space)
	 * @param {string} tool Name of the tool to diplay
	 * @param {{w: number, h: number}} resolution Resolution of generation to display
	 * @param {object} style Styles to use for rendering the reticle
	 * @param {string} [style.sizeTextStyle = "#FFF5"] Style of the text for diplaying the bounding box size.
	 * @param {string} [style.genSizeTextStyle = "#FFF5"] Style of the text for diplaying generation size
	 * @param {string} [style.toolTextStyle = "#FFF5"] Style of the text for the tool name
	 * @param {number} [style.reticleWidth = 1] Width of the line of the reticle
	 * @param {string} [style.reticleStyle] Style of the line of the reticle
	 *
	 * @returns A function that erases this reticle drawing
	 */
	_reticle_draw(bb, tool, resolution, style = {}) {
		defaultOpt(style, {
			sizeTextStyle: "#FFF5",
			genSizeTextStyle: "#FFF5",
			toolTextStyle: "#FFF5",
			reticleWidth: 1,
			reticleStyle: global.hasActiveInput ? "#BBF" : "#FFF",
		});

		const bbvp = bb.transform(viewport.c2v);

		uiCtx.save();

		// Draw targeting square reticle thingy cursor
		uiCtx.lineWidth = style.reticleWidth;
		uiCtx.strokeStyle = style.reticleStyle;
		uiCtx.strokeRect(bbvp.x, bbvp.y, bbvp.w, bbvp.h); // Origin is middle of the frame

		uiCtx.font = `bold 20px Open Sans`;

		// Draw Tool Name
		if (bb.h > 40) {
			const xshrink = Math.min(
				1,
				(bbvp.w - 20) / uiCtx.measureText(tool).width
			);

			uiCtx.font = `bold ${20 * xshrink}px Open Sans`;

			uiCtx.textAlign = "left";
			uiCtx.fillStyle = style.toolTextStyle;
			uiCtx.fillText(tool, bbvp.x + 10, bbvp.y + 10 + 20 * xshrink, bb.w);
		}

		// Draw width and height
		{
			// Render Cursor Width
			uiCtx.textAlign = "center";
			uiCtx.fillStyle = style.sizeTextStyle;
			uiCtx.translate(bbvp.x + bbvp.w / 2, bbvp.y + bbvp.h / 2);
			const xshrink = Math.min(
				1,
				(bbvp.w - 30) / uiCtx.measureText(`${bb.w}px`).width
			);
			const yshrink = Math.min(
				1,
				(bbvp.h - 30) / uiCtx.measureText(`${bb.h}px`).width
			);
			uiCtx.font = `bold ${20 * xshrink}px Open Sans`;
			uiCtx.fillText(`${bb.w}px`, 0, bbvp.h / 2 - 10 * xshrink, bb.w);

			// Render Generation Width
			uiCtx.fillStyle = style.genSizeTextStyle;
			uiCtx.font = `bold ${10 * xshrink}px Open Sans`;
			if (bb.w !== resolution.w)
				uiCtx.fillText(`${resolution.w}px`, 0, bbvp.h / 2 - 30 * xshrink, bb.h);

			// Render Cursor Height
			uiCtx.rotate(-Math.PI / 2);
			uiCtx.fillStyle = style.sizeTextStyle;
			uiCtx.font = `bold ${20 * yshrink}px Open Sans`;
			uiCtx.fillText(`${bb.h}px`, 0, bbvp.w / 2 - 10 * yshrink, bb.h);

			// Render Generation Height
			uiCtx.fillStyle = style.genSizeTextStyle;
			uiCtx.font = `bold ${10 * yshrink}px Open Sans`;
			if (bb.h !== resolution.h)
				uiCtx.fillText(`${resolution.h}px`, 0, bbvp.w / 2 - 30 * xshrink, bb.h);

			uiCtx.restore();
		}

		return () => {
			uiCtx.save();

			uiCtx.clearRect(bbvp.x - 64, bbvp.y - 64, bbvp.w + 128, bbvp.h + 128);

			uiCtx.restore();
		};
	},

	/**
	 * Draws a generic crosshair cursor at the specified location
	 *
	 * @param {number} x X world coordinate of the cursor
	 * @param {number} y Y world coordinate of the cursor
	 * @param {object} style Style of the lines of the cursor
	 * @param {string} [style.width = 3] Line width of the lines of the cursor
	 * @param {string} [style.style] Stroke style of the lines of the cursor
	 *
	 * @returns A function that erases this cursor drawing
	 */
	_cursor_draw(x, y, style = {}) {
		defaultOpt(style, {
			width: 3,
			style: global.hasActiveInput ? "#BBF5" : "#FFF5",
		});
		const vpc = viewport.canvasToView(x, y);

		// Draw current cursor location
		uiCtx.lineWidth = style.width;
		uiCtx.strokeStyle = style.style;

		uiCtx.beginPath();
		uiCtx.moveTo(vpc.x, vpc.y + 10);
		uiCtx.lineTo(vpc.x, vpc.y - 10);
		uiCtx.moveTo(vpc.x + 10, vpc.y);
		uiCtx.lineTo(vpc.x - 10, vpc.y);
		uiCtx.stroke();
		return () => {
			uiCtx.clearRect(vpc.x - 15, vpc.y - 15, vpc.x + 30, vpc.y + 30);
		};
	},

	/**
	 * Creates generic handlers for dealing with draggable selection areas
	 *
	 * @param {object} state State of the tool
	 * @param {boolean} state.snapToGrid Whether the cursor should snap to the grid
	 * @param {() => void} [state.redraw] Function to redraw the cursor
	 * @returns
	 */
	_draggable_selection(state) {
		const selection = {
			_inside: false,
			_dirty_bb: true,
			_cached_bb: null,
			_selected: null,

			/**
			 * If the cursor is cursor is currently inside the selection
			 */
			get inside() {
				return this._inside;
			},

			/**
			 * Get intermediate selection object
			 */
			get selected() {
				return this._selected;
			},

			/**
			 * If the selection exists
			 */
			get exists() {
				return !!this._selected;
			},

			/**
			 * Gets the selection bounding box
			 *
			 * @returns {BoundingBox}
			 */
			get bb() {
				if (this._dirty_bb && this._selected) {
					this._cached_bb = BoundingBox.fromStartEnd(
						this._selected.start,
						this._selected.now
					);
					this._dirty_bb = false;
				}
				return this._selected && this._cached_bb;
			},

			/**
			 * When the cursor enters the selection
			 */
			onenter: new Observer(),

			/**
			 * When the cursor leaves the selection
			 */
			onleave: new Observer(),

			// Utility methods
			deselect() {
				if (this.inside) {
					this._inside = false;
					this.onleave.emit({evn: null});
				}
				this._selected = null;
			},

			// Dragging handlers
			/**
			 * Drag start event handler
			 *
			 * @param {Point} evn Drag start event
			 */
			dragstartcb(evn) {
				const x = state.snapToGrid ? evn.ix + snap(evn.ix, 0, 64) : evn.ix;
				const y = state.snapToGrid ? evn.iy + snap(evn.iy, 0, 64) : evn.iy;
				this._selected = {start: {x, y}, now: {x, y}};
				this._dirty_bb = true;
			},
			/**
			 * Drag event handler
			 *
			 * @param {Point} evn Drag event
			 */
			dragcb(evn) {
				const x = state.snapToGrid ? evn.x + snap(evn.x, 0, 64) : evn.x;
				const y = state.snapToGrid ? evn.y + snap(evn.y, 0, 64) : evn.y;

				if (x !== this._selected.now.x || y !== this._selected.now.y) {
					this._selected.now = {x, y};
					this._dirty_bb = true;
				}
			},
			/**
			 * Drag end event handler
			 *
			 * @param {Point} evn Drag end event
			 */
			dragendcb(evn) {
				const x = state.snapToGrid ? evn.x + snap(evn.x, 0, 64) : evn.x;
				const y = state.snapToGrid ? evn.y + snap(evn.y, 0, 64) : evn.y;

				this._selected.now = {x, y};
				this._dirty_bb = true;

				if (
					this._selected.start.x === this._selected.now.x ||
					this._selected.start.y === this._selected.now.y
				) {
					this.deselect();
				}
			},

			/**
			 * Mouse move event handler
			 *
			 * @param {Point} evn Mouse move event
			 */
			smousemovecb(evn) {
				if (!this._selected || !this.bb.contains(evn.x, evn.y)) {
					if (this.inside) {
						this._inside = false;
						this.onleave.emit({evn});
					}
				} else {
					if (!this.inside) {
						this._inside = true;
						this.onenter.emit({evn});
					}
				}
			},
		};

		return selection;
	},

	/**
	 * Processes cursor position
	 *
	 * @param {Point} wpoint World coordinate of the cursor
	 * @param {boolean} snapToGrid Snap to grid
	 */
	_process_cursor(wpoint, snapToGrid) {
		// Get cursor positions
		let x = wpoint.x;
		let y = wpoint.y;
		let sx = x;
		let sy = y;

		if (snapToGrid) {
			sx += snap(x, 0, config.gridSize);
			sy += snap(y, 0, config.gridSize);
		}

		const vpc = viewport.canvasToView(x, y);
		const vpsc = viewport.canvasToView(sx, sy);

		return {
			// World Coordinates
			x,
			y,
			sx,
			sy,

			// Viewport Coordinates
			vpx: vpc.x,
			vpy: vpc.y,
			vpsx: vpsc.x,
			vpsy: vpsc.y,
		};
	},

	/**
	 * Represents a marquee selection with an image
	 */
	MarqueeSelection: class {
		/** @type {HTMLCanvasElement} */
		canvas;

		_dirty = false;
		_position = {x: 0, y: 0};
		/**
		 * @type {Point}
		 */
		get position() {
			return this._position;
		}
		set position(v) {
			this._dirty = true;
			this._position = v;
		}

		_scale = {x: 1, y: 1};
		/**
		 * @type {Point}
		 */
		get scale() {
			return this._scale;
		}
		set scale(v) {
			if (v.x === 0 || v.y === 0) return;
			this._dirty = true;
			this._scale = v;
		}

		_rotation = 0;
		get rotation() {
			return this._rotation;
		}
		set rotation(v) {
			this._dirty = true;
			this._rotation = v;
		}

		/**
		 * @param {HTMLCanvasElement} canvas Selected image canvas
		 * @param {Point} position Initial position of the selection
		 */
		constructor(canvas, position = {x: 0, y: 0}) {
			this.canvas = canvas;
			this.position = position;
		}

		/** @type {DOMMatrix} */
		_rtmatrix = null;
		get rtmatrix() {
			if (!this._rtmatrix || this._dirty) {
				const m = new DOMMatrix();

				m.translateSelf(this.position.x, this.position.y);
				m.rotateSelf((this.rotation * 180) / Math.PI);

				this._rtmatrix = m;
			}

			return this._rtmatrix;
		}

		/** @type {DOMMatrix} */
		_matrix = null;
		get matrix() {
			if (!this._matrix || this._dirty) {
				this._matrix = this.rtmatrix.scaleSelf(this.scale.x, this.scale.y);
			}
			return this._matrix;
		}

		/**
		 * If the main marquee box contains a given point
		 *
		 * @param {number} x X coordinate of the point
		 * @param {number} y Y coordinate of the point
		 */
		contains(x, y) {
			const p = this.matrix.invertSelf().transformPoint({x, y});

			return (
				Math.abs(p.x) < this.canvas.width / 2 &&
				Math.abs(p.y) < this.canvas.height / 2
			);
		}

		hoveringRotateHandle(x, y, scale = 1) {
			const localc = this.rtmatrix.inverse().transformPoint({x, y});
			const localrh = {
				x: 0,
				y:
					(-this.scale.y * this.canvas.height) / 2 -
					config.rotateHandleDistance * scale,
			};

			const dx = Math.abs(localc.x - localrh.x);
			const dy = Math.abs(localc.y - localrh.y);

			return (
				dx * dx + dy * dy <
				(scale * scale * config.handleDetectSize * config.handleDetectSize) / 4
			);
		}

		hoveringHandle(x, y, scale = 1) {
			const localbb = new BoundingBox({
				x: (this.scale.x * -this.canvas.width) / 2,
				y: (this.scale.y * -this.canvas.height) / 2,
				w: this.canvas.width * this.scale.x,
				h: this.canvas.height * this.scale.y,
			});

			const localc = this.rtmatrix.inverse().transformPoint({x, y});
			const ontl =
				Math.max(
					Math.abs(localc.x - localbb.tl.x),
					Math.abs(localc.y - localbb.tl.y)
				) <
				(config.handleDetectSize / 2) * scale;
			const ontr =
				Math.max(
					Math.abs(localc.x - localbb.tr.x),
					Math.abs(localc.y - localbb.tr.y)
				) <
				(config.handleDetectSize / 2) * scale;
			const onbl =
				Math.max(
					Math.abs(localc.x - localbb.bl.x),
					Math.abs(localc.y - localbb.bl.y)
				) <
				(config.handleDetectSize / 2) * scale;
			const onbr =
				Math.max(
					Math.abs(localc.x - localbb.br.x),
					Math.abs(localc.y - localbb.br.y)
				) <
				(config.handleDetectSize / 2) * scale;

			return {onHandle: ontl || ontr || onbl || onbr, ontl, ontr, onbl, onbr};
		}

		hoveringBox(x, y) {
			const localbb = new BoundingBox({
				x: -this.canvas.width / 2,
				y: -this.canvas.height / 2,
				w: this.canvas.width,
				h: this.canvas.height,
			});

			const localc = this.matrix.inverse().transformPoint({x, y});

			return (
				!this.hoveringHandle(x, y).onHandle &&
				localbb.contains(localc.x, localc.y)
			);
		}

		/**
		 * Draws the marquee selector box
		 *
		 * @param {CanvasRenderingContext2D} context A context for rendering the box to
		 * @param {Point} cursor Cursor position
		 * @param {DOMMatrix} transform A transformation matrix to transform the position by
		 */
		drawBox(context, cursor, transform = new DOMMatrix()) {
			const drawscale =
				1 / Math.sqrt(transform.a * transform.a + transform.b * transform.b);
			const m = transform.multiply(this.matrix);

			context.save();

			const localbb = new BoundingBox({
				x: -this.canvas.width / 2,
				y: -this.canvas.height / 2,
				w: this.canvas.width,
				h: this.canvas.height,
			});

			// Line Style
			context.strokeStyle = "#FFF";
			context.lineWidth = 2;

			const tl = m.transformPoint(localbb.tl);
			const tr = m.transformPoint(localbb.tr);
			const bl = m.transformPoint(localbb.bl);
			const br = m.transformPoint(localbb.br);

			const bbc = m.transformPoint({x: 0, y: 0});

			context.beginPath();
			context.arc(bbc.x, bbc.y, 5, 0, Math.PI * 2);
			context.stroke();

			context.setLineDash([4, 2]);

			// Draw main rectangle
			context.beginPath();
			context.moveTo(tl.x, tl.y);
			context.lineTo(tr.x, tr.y);
			context.lineTo(br.x, br.y);
			context.lineTo(bl.x, bl.y);
			context.lineTo(tl.x, tl.y);
			context.stroke();

			// Draw rotation handle
			context.setLineDash([]);

			const hm = new DOMMatrix().rotateSelf((this.rotation * 180) / Math.PI);
			const tm = m.transformPoint({x: 0, y: -this.canvas.height / 2});
			const rho = hm.transformPoint({x: 0, y: -config.rotateHandleDistance});
			const rh = {x: tm.x + rho.x, y: tm.y + rho.y};

			let handleRadius = config.handleDrawSize / 2;
			if (this.hoveringRotateHandle(cursor.x, cursor.y, drawscale))
				handleRadius *= config.handleDrawHoverScale;

			context.beginPath();
			context.moveTo(tm.x, tm.y);
			context.lineTo(rh.x, rh.y);
			context.stroke();

			context.beginPath();
			context.arc(rh.x, rh.y, handleRadius, 0, 2 * Math.PI);
			context.stroke();

			// Draw handles
			const drawHandle = (pt, hover) => {
				let hsz = config.handleDrawSize / 2;
				if (hover) hsz *= config.handleDrawHoverScale;

				const htl = hm.transformPoint({x: -hsz, y: -hsz});
				const htr = hm.transformPoint({x: hsz, y: -hsz});
				const hbr = hm.transformPoint({x: hsz, y: hsz});
				const hbl = hm.transformPoint({x: -hsz, y: hsz});

				context.beginPath();
				context.moveTo(htl.x + pt.x, htl.y + pt.y);
				context.lineTo(htr.x + pt.x, htr.y + pt.y);
				context.lineTo(hbr.x + pt.x, hbr.y + pt.y);
				context.lineTo(hbl.x + pt.x, hbl.y + pt.y);
				context.lineTo(htl.x + pt.x, htl.y + pt.y);
				context.stroke();
			};

			context.strokeStyle = "#FFF";
			context.lineWidth = 2;
			context.setLineDash([]);

			const {ontl, ontr, onbl, onbr} = this.hoveringHandle(
				cursor.x,
				cursor.y,
				drawscale
			);

			drawHandle(tl, ontl);
			drawHandle(tr, ontr);
			drawHandle(bl, onbl);
			drawHandle(br, onbr);

			context.restore();

			return () => {
				const border = config.handleDrawSize * config.handleDrawHoverScale;

				const minx = Math.min(tl.x, tr.x, bl.x, br.x, rh.x) - border;
				const maxx = Math.max(tl.x, tr.x, bl.x, br.x, rh.x) + border;
				const miny = Math.min(tl.y, tr.y, bl.y, br.y, rh.y) - border;
				const maxy = Math.max(tl.y, tr.y, bl.y, br.y, rh.y) + border;

				context.clearRect(minx, miny, maxx - minx, maxy - miny);
			};
		}

		/**
		 * Draws the selected image
		 *
		 * @param {CanvasRenderingContext2D} context A context for rendering the image to
		 * @param {CanvasRenderingContext2D} peekctx A context for rendering the layer peeking to
		 * @param {object} options
		 * @param {DOMMatrix} options.transform A transformation matrix to transform the position by
		 * @param {number} options.opacity Opacity of the peek display
		 */
		drawImage(context, peekctx, options = {}) {
			defaultOpt(options, {
				transform: new DOMMatrix(),
				opacity: 0.4,
			});

			context.save();
			peekctx.save();

			const m = options.transform.multiply(this.matrix);

			// Draw image
			context.setTransform(m);
			context.drawImage(
				this.canvas,
				-this.canvas.width / 2,
				-this.canvas.height / 2,
				this.canvas.width,
				this.canvas.height
			);

			// Draw peek
			peekctx.filter = `opacity(${options.opacity * 100}%)`;
			peekctx.setTransform(m);
			peekctx.drawImage(
				this.canvas,
				-this.canvas.width / 2,
				-this.canvas.height / 2,
				this.canvas.width,
				this.canvas.height
			);

			peekctx.restore();
			context.restore();

			return () => {
				// Here we only save transform for performance
				const pt = context.getTransform();
				const ppt = context.getTransform();

				context.setTransform(m);
				peekctx.setTransform(m);

				context.clearRect(
					-this.canvas.width / 2 - 10,
					-this.canvas.height / 2 - 10,
					this.canvas.width + 20,
					this.canvas.height + 20
				);

				peekctx.clearRect(
					-this.canvas.width / 2 - 10,
					-this.canvas.height / 2 - 10,
					this.canvas.width + 20,
					this.canvas.height + 20
				);

				context.setTransform(pt);
				peekctx.setTransform(ppt);
			};
		}
	},
};