-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinfinite-canvas.ts
259 lines (213 loc) · 6.75 KB
/
infinite-canvas.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
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
export class InfiniteCanvas {
canvas: HTMLCanvasElement | null = null;
context: CanvasRenderingContext2D | null = null;
cellSize: number;
#scale = 1;
#offsetX = 0;
#offsetY = 0;
#touchMode: "single" | "double" = "single";
#prevTouch: [Touch | null, Touch | null] = [null, null];
constructor(cellSize = 40) {
this.cellSize = cellSize;
const canvas = document.getElementById("canvas");
if (canvas && canvas instanceof HTMLCanvasElement) {
this.canvas = canvas;
this.#setupEvents(canvas);
const context = canvas.getContext("2d");
if (context) {
this.context = context;
this.#draw();
} else {
console.error(`<canvas> element is missing context 2d`);
}
} else {
console.error(`<canvas> element with id="canvas" not found`);
}
}
toVirtualX(xReal: number): number {
return (xReal + this.#offsetX) * this.#scale;
}
toVirtualY(yReal: number): number {
return (yReal + this.#offsetY) * this.#scale;
}
toRealX(xVirtual: number): number {
return xVirtual / this.#scale - this.#offsetX;
}
toRealY(yVirtual: number): number {
return yVirtual / this.#scale - this.#offsetY;
}
virtualHeight(): number {
return (this.canvas?.clientHeight ?? 0) / this.#scale;
}
virtualWidth(): number {
return (this.canvas?.clientWidth ?? 0) / this.#scale;
}
zoom(amount: number): void {
this.#scale *= amount;
this.#draw();
}
offsetLeft(amount: number): void {
this.#offsetX -= amount;
this.#draw();
}
offsetRight(amount: number): void {
this.#offsetX += amount;
this.#draw();
}
offsetUp(amount: number): void {
this.#offsetY -= amount;
this.#draw();
}
offsetDown(amount: number): void {
this.#offsetY += amount;
this.#draw();
}
#draw(): void {
if (this.canvas && this.context) {
this.canvas.width = document.body.clientWidth;
this.canvas.height = document.body.clientHeight;
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.#drawGrid();
}
}
#setupEvents(canvas: HTMLCanvasElement): void {
canvas.addEventListener("touchstart", (event) =>
this.#onTouchStart(event.touches)
);
canvas.addEventListener("touchmove", (event) =>
this.#onTouchMove(event.touches)
);
window.addEventListener("resize", () => this.#draw());
}
#onTouchStart(touches: TouchList): void {
if (touches.length == 1) {
this.#touchMode = "single";
} else if (touches.length >= 2) {
this.#touchMode = "double";
}
// Store the last touches
this.#prevTouch[0] = touches[0];
this.#prevTouch[1] = touches[1];
this.#onTouchMove(touches);
}
#onTouchMove(touches: TouchList): void {
// Get first touch coordinates
const touch0X = touches[0].pageX;
const touch0Y = touches[0].pageY;
const prevTouch0X = this.#prevTouch[0]!.pageX;
const prevTouch0Y = this.#prevTouch[0]!.pageY;
if (this.#touchMode === "single") {
// Single touch (setup click event)
} else if (this.#touchMode === "double") {
// get second touch coordinates
const touch1X = touches[1].pageX;
const touch1Y = touches[1].pageY;
const prevTouch1X = this.#prevTouch[1]!.pageX;
const prevTouch1Y = this.#prevTouch[1]!.pageY;
const scaleAmount = this.#zoom(
[touch0X, touch0Y],
[prevTouch0X, prevTouch0Y],
[touch1X, touch1Y],
[prevTouch1X, prevTouch1Y]
);
this.#pan(
scaleAmount,
[touch0X, touch0Y],
[prevTouch0X, prevTouch0Y],
[touch1X, touch1Y],
[prevTouch1X, prevTouch1Y]
);
this.#draw();
}
this.#prevTouch[0] = touches[0];
this.#prevTouch[1] = touches[1];
}
#pan(
scaleAmount: number,
[touch0X, touch0Y]: [number, number],
[prevTouch0X, prevTouch0Y]: [number, number],
[touch1X, touch1Y]: [number, number],
[prevTouch1X, prevTouch1Y]: [number, number]
): void {
// get midpoints
const midX = (touch0X + touch1X) / 2;
const midY = (touch0Y + touch1Y) / 2;
const prevMidX = (prevTouch0X + prevTouch1X) / 2;
const prevMidY = (prevTouch0Y + prevTouch1Y) / 2;
// Calculate how many pixels the midpoints have moved in the x and y direction
const panX = midX - prevMidX;
const panY = midY - prevMidY;
// Scale this movement based on the zoom level
this.#offsetX += panX / this.#scale;
this.#offsetY += panY / this.#scale;
// Get the relative position of the middle of the zoom.
// 0, 0 would be top left.
// 0, 1 would be top right etc.
const zoomRatioX = midX / (this.canvas?.clientWidth ?? 1);
const zoomRatioY = midY / (this.canvas?.clientHeight ?? 1);
// calculate the amounts zoomed from each edge of the screen
const unitsZoomedX = this.virtualWidth() * scaleAmount;
const unitsZoomedY = this.virtualHeight() * scaleAmount;
const unitsAddLeft = unitsZoomedX * zoomRatioX;
const unitsAddTop = unitsZoomedY * zoomRatioY;
this.#offsetX += unitsAddLeft;
this.#offsetY += unitsAddTop;
}
#zoom(
[touch0X, touch0Y]: [number, number],
[prevTouch0X, prevTouch0Y]: [number, number],
[touch1X, touch1Y]: [number, number],
[prevTouch1X, prevTouch1Y]: [number, number]
): number {
const hypot = Math.sqrt(
Math.pow(touch0X - touch1X, 2) + Math.pow(touch0Y - touch1Y, 2)
);
const prevHypot = Math.sqrt(
Math.pow(prevTouch0X - prevTouch1X, 2) +
Math.pow(prevTouch0Y - prevTouch1Y, 2)
);
const zoomAmount = hypot / prevHypot;
this.zoom(zoomAmount);
const scaleAmount = 1 - zoomAmount;
return scaleAmount;
}
#drawGrid(): void {
if (this.canvas && this.context) {
this.context.strokeStyle = "rgb(229,231,235)";
this.context.lineWidth = 1;
this.context.font = "10px serif";
this.context.beginPath();
const width = this.canvas.clientWidth;
const height = this.canvas.clientHeight;
for (
let x = (this.#offsetX % this.cellSize) * this.#scale;
x <= width;
x += this.cellSize * this.#scale
) {
const source = x;
this.context.moveTo(source, 0);
this.context.lineTo(source, height);
this.context.fillText(
`${this.toVirtualX(source).toFixed(0)}`,
source,
10
);
}
for (
let y = (this.#offsetY % this.cellSize) * this.#scale;
y <= height;
y += this.cellSize * this.#scale
) {
const destination = y;
this.context.moveTo(0, destination);
this.context.lineTo(width, destination);
this.context.fillText(
`${this.toVirtualY(destination).toFixed(0)}`,
0,
destination
);
}
this.context.stroke();
}
}
}