-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcanvas_camera.html
152 lines (129 loc) · 4.57 KB
/
canvas_camera.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
<!-- vim: sw=2 ts=2 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Canvas Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>"use strict";
const canvas = document.getElementById("glcanvas");
const ctx = canvas.getContext('2d');
(window.onresize = () => {
canvas.width = window.innerWidth*window.devicePixelRatio,
canvas.height = window.innerHeight*window.devicePixelRatio
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
})();
let input = {
cam: { zoom: 1, x: 0, y: 0 },
has_horizontal_scroll: false,
world_transform: null,
mouse_down: false,
cam_down_x: 0,
cam_down_y: 0,
mouse_down_x: 0,
mouse_down_y: 0,
mouse_x: 0,
mouse_y: 0,
};
{
canvas.onmousedown = e => {
input.mouse_down = true;
input.mouse_down_x = e.offsetX*window.devicePixelRatio;
input.mouse_down_y = e.offsetY*window.devicePixelRatio;
input.cam_down_x = input.cam.x;
input.cam_down_y = input.cam.y;
}
canvas.mouseleave = window.onmouseup = e => {
input.mouse_down = false;
}
canvas.onmousemove = e => {
input.mouse_x = e.offsetX*window.devicePixelRatio;
input.mouse_y = e.offsetY*window.devicePixelRatio;
if (input.mouse_down) {
input.cam.x = input.cam_down_x + (input.mouse_x - input.mouse_down_x);
input.cam.y = input.cam_down_y + (input.mouse_y - input.mouse_down_y);
}
};
let wheelTimeout;
canvas.onwheel = e => {
e.preventDefault();
let delta_x = e.deltaX;
let delta_y = e.deltaY;
if (Math.abs(delta_x) > 0) {
input.has_horizontal_scroll = true;
}
clearTimeout(wheelTimeout);
wheelTimeout = setTimeout(() => input.has_horizontal_scroll = false, 200);
/* on trackpads, people expect vertical scrolling to pan instead of zoom.
* that's fine, except the only way I know of to tell if they're using a trackpad is ...
* did they ever do a horizontal zoom? */
if (input.has_horizontal_scroll && !e.ctrlKey && !e.shiftKey) {
input.cam.x -= delta_x;
input.cam.y -= delta_y;
return;
}
if (e.ctrlKey) {
/* also, if you ARE using a trackpad, ctrlKey is set to true for "pinch to zoom events,"
* and those are much weaker relative to typical scroll */
delta_y *= 10;
}
if (!input.world_transform) return;
const mouse = input.world_transform.transformPoint(new DOMPoint(input.mouse_x, input.mouse_y));
let next_zoom = input.cam.zoom * (1 - delta_y * 0.001);
next_zoom = Math.max(0.15, next_zoom);
/* offset camera.x such that it stays centered around the mouse,
* accounting for the new zoom */
{
const x_t = mouse.x / canvas.width;
const nowSizeX = canvas.width * input.cam.zoom;
const nextSizeX = canvas.width * next_zoom;
const deltaX = nextSizeX - nowSizeX;
input.cam.x -= deltaX * x_t;
}
/* ditto on the Y axis; keep the mouse as the focal point */
{
const y_t = mouse.y / canvas.height;
const nowSizeY = canvas.height * input.cam.zoom;
const nextSizeY = canvas.height * next_zoom;
const deltaY = nextSizeY - nowSizeY;
input.cam.y -= deltaY * y_t;
}
input.cam.zoom = next_zoom;
}
}
requestAnimationFrame(function render(now) {
requestAnimationFrame(render);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
const TILE_SCALE = 100;
ctx.save();
{
ctx.translate(input.cam.x, input.cam.y);
ctx.scale(input.cam.zoom, input.cam.zoom);
input.world_transform = ctx.getTransform().inverse();
{
const min = input.world_transform.transformPoint(new DOMPoint(0, 0));
const max = input.world_transform.transformPoint(new DOMPoint(canvas.width, canvas.height));
min.x = Math.floor(min.x/TILE_SCALE)*TILE_SCALE, min.y = Math.floor(min.y/TILE_SCALE)*TILE_SCALE;
max.x = Math.ceil(max.x/TILE_SCALE)*TILE_SCALE, max.y = Math.ceil(max.y/TILE_SCALE)*TILE_SCALE;
ctx.fillStyle = "red";
for (let y = Math.floor(min.y); y < Math.ceil(max.y); y += TILE_SCALE) {
ctx.fillRect(min.x, y - 1, max.x - min.x, 2);
}
for (let x = Math.floor(min.x); x < Math.ceil(max.x); x += TILE_SCALE) {
ctx.fillRect(x - 1, min.y, 2, max.x - min.x);
}
}
}
ctx.restore();
})
function lerp(v0, v1, t) { return (1 - t) * v0 + t * v1; }
function inv_lerp(min, max, p) { return (p - min) / (max - min); }
</script>
</body>
</html>