-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.html
303 lines (258 loc) · 9.98 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite Canvas</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
/* Prevent document pinch-zoom & touch-hold-to-highlight */
touch-action: none;
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-user-select: none;
/* Safari */
-khtml-user-select: none;
/* Konqueror HTML */
-moz-user-select: none;
/* Old versions of Firefox */
-ms-user-select: none;
/* Internet Explorer/Edge */
user-select: none;
/* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
}
</style>
</head>
<body>
<canvas id="canvas">Your browser does not support HTML5 canvas</canvas>
<script>
// get our canvas element
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
// disable right clicking
document.oncontextmenu = function () {
return false;
}
// list of all strokes drawn
const drawings = [];
// coordinates of our cursor
let cursorX;
let cursorY;
let prevCursorX;
let prevCursorY;
// distance from origin
let offsetX = 0;
let offsetY = 0;
// zoom amount
let scale = 1;
// convert coordinates
function toScreenX(xTrue) {
return (xTrue + offsetX) * scale;
}
function toScreenY(yTrue) {
return (yTrue + offsetY) * scale;
}
function toTrueX(xScreen) {
return (xScreen / scale) - offsetX;
}
function toTrueY(yScreen) {
return (yScreen / scale) - offsetY;
}
function trueHeight() {
return canvas.clientHeight / scale;
}
function trueWidth() {
return canvas.clientWidth / scale;
}
function redrawCanvas() {
// set the canvas to the size of the window
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
context.fillStyle = '#fff';
context.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < drawings.length; i++) {
const line = drawings[i];
drawLine(toScreenX(line.x0), toScreenY(line.y0), toScreenX(line.x1), toScreenY(line.y1));
}
}
redrawCanvas();
// if the window changes size, redraw the canvas
window.addEventListener("resize", (event) => {
redrawCanvas();
});
// Mouse Event Handlers
canvas.addEventListener('mousedown', onMouseDown);
canvas.addEventListener('mouseup', onMouseUp, false);
canvas.addEventListener('mouseout', onMouseUp, false);
canvas.addEventListener('mousemove', onMouseMove, false);
canvas.addEventListener('wheel', onMouseWheel, false);
// Touch Event Handlers
canvas.addEventListener('touchstart', onTouchStart);
canvas.addEventListener('touchend', onTouchEnd);
canvas.addEventListener('touchcancel', onTouchEnd);
canvas.addEventListener('touchmove', onTouchMove);
// mouse functions
let leftMouseDown = false;
let rightMouseDown = false;
function onMouseDown(event) {
// detect left clicks
if (event.button == 0) {
leftMouseDown = true;
rightMouseDown = false;
}
// detect right clicks
if (event.button == 2) {
rightMouseDown = true;
leftMouseDown = false;
}
// update the cursor coordinates
cursorX = event.pageX;
cursorY = event.pageY;
prevCursorX = event.pageX;
prevCursorY = event.pageY;
}
function onMouseMove(event) {
// get mouse position
cursorX = event.pageX;
cursorY = event.pageY;
const scaledX = toTrueX(cursorX);
const scaledY = toTrueY(cursorY);
const prevScaledX = toTrueX(prevCursorX);
const prevScaledY = toTrueY(prevCursorY);
if (leftMouseDown) {
// add the line to our drawing history
drawings.push({
x0: prevScaledX,
y0: prevScaledY,
x1: scaledX,
y1: scaledY
})
// draw a line
drawLine(prevCursorX, prevCursorY, cursorX, cursorY);
}
if (rightMouseDown) {
// move the screen
offsetX += (cursorX - prevCursorX) / scale;
offsetY += (cursorY - prevCursorY) / scale;
redrawCanvas();
}
prevCursorX = cursorX;
prevCursorY = cursorY;
}
function onMouseUp() {
leftMouseDown = false;
rightMouseDown = false;
}
function onMouseWheel(event) {
const deltaY = event.deltaY;
const scaleAmount = -deltaY / 500;
scale = scale * (1 + scaleAmount);
// zoom the page based on where the cursor is
var distX = event.pageX / canvas.clientWidth;
var distY = event.pageY / canvas.clientHeight;
// calculate how much we need to zoom
const unitsZoomedX = trueWidth() * scaleAmount;
const unitsZoomedY = trueHeight() * scaleAmount;
const unitsAddLeft = unitsZoomedX * distX;
const unitsAddTop = unitsZoomedY * distY;
offsetX -= unitsAddLeft;
offsetY -= unitsAddTop;
redrawCanvas();
}
function drawLine(x0, y0, x1, y1) {
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.strokeStyle = '#000';
context.lineWidth = 2;
context.stroke();
}
// touch functions
const prevTouches = [null, null]; // up to 2 touches
let singleTouch = false;
let doubleTouch = false;
function onTouchStart(event) {
if (event.touches.length == 1) {
singleTouch = true;
doubleTouch = false;
}
if (event.touches.length >= 2) {
singleTouch = false;
doubleTouch = true;
}
// store the last touches
prevTouches[0] = event.touches[0];
prevTouches[1] = event.touches[1];
}
function onTouchMove(event) {
// get first touch coordinates
const touch0X = event.touches[0].pageX;
const touch0Y = event.touches[0].pageY;
const prevTouch0X = prevTouches[0].pageX;
const prevTouch0Y = prevTouches[0].pageY;
const scaledX = toTrueX(touch0X);
const scaledY = toTrueY(touch0Y);
const prevScaledX = toTrueX(prevTouch0X);
const prevScaledY = toTrueY(prevTouch0Y);
if (singleTouch) {
// add to history
drawings.push({
x0: prevScaledX,
y0: prevScaledY,
x1: scaledX,
y1: scaledY
})
drawLine(prevTouch0X, prevTouch0Y, touch0X, touch0Y);
}
if (doubleTouch) {
// get second touch coordinates
const touch1X = event.touches[1].pageX;
const touch1Y = event.touches[1].pageY;
const prevTouch1X = prevTouches[1].pageX;
const prevTouch1Y = prevTouches[1].pageY;
// get midpoints
const midX = (touch0X + touch1X) / 2;
const midY = (touch0Y + touch1Y) / 2;
const prevMidX = (prevTouch0X + prevTouch1X) / 2;
const prevMidY = (prevTouch0Y + prevTouch1Y) / 2;
// calculate the distances between the touches
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));
// calculate the screen scale change
var zoomAmount = hypot / prevHypot;
scale = scale * zoomAmount;
const scaleAmount = 1 - zoomAmount;
// 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
offsetX += (panX / scale);
offsetY += (panY / scale);
// Get the relative position of the middle of the zoom.
// 0, 0 would be top left.
// 0, 1 would be top right etc.
var zoomRatioX = midX / canvas.clientWidth;
var zoomRatioY = midY / canvas.clientHeight;
// calculate the amounts zoomed from each edge of the screen
const unitsZoomedX = trueWidth() * scaleAmount;
const unitsZoomedY = trueHeight() * scaleAmount;
const unitsAddLeft = unitsZoomedX * zoomRatioX;
const unitsAddTop = unitsZoomedY * zoomRatioY;
offsetX += unitsAddLeft;
offsetY += unitsAddTop;
redrawCanvas();
}
prevTouches[0] = event.touches[0];
prevTouches[1] = event.touches[1];
}
function onTouchEnd(event) {
singleTouch = false;
doubleTouch = false;
}
</script>
</body>
</html>