Skip to content

Commit 51f46ab

Browse files
author
Dylan McGannon
committed
Limit the zoom level in and out.
Limit the camera to within the image.
1 parent c5a8fb7 commit 51f46ab

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

js/gl.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class GL {
2626
this.camera_position = new Vector(0, 0, 0, 1);
2727
this.zoom_target = new Vector(0, 0, 0, 1);
2828
this.zoom_speed = 0;
29+
this.zoom_level = 1;
2930

3031
// Add resize listener
3132
window.addEventListener('resize', () => {
@@ -163,6 +164,10 @@ export class GL {
163164
* @param {float} z
164165
*/
165166
set_camera_position(x, y, z) {
167+
// Stay within the bounds of the quad.
168+
if (x < -2 || x > 1 || y < -1 || y > 1) {
169+
return;
170+
}
166171
this.camera_position = new Vector(x, y, z);
167172
this.view_matrix = Matrix.identity().translate(-x, -y, -z);
168173
}
@@ -241,6 +246,11 @@ export class GL {
241246
new Vector(0, 0, 0, 1)
242247
);
243248

249+
//this.camera_position = new Vector(0, 0, 0, 1);
250+
this.zoom_target = new Vector(0, 0, 0, 1);
251+
this.zoom_speed = 0;
252+
this.zoom_level = 1;
253+
244254
return true;
245255
}
246256

@@ -287,11 +297,17 @@ export class GL {
287297
);
288298

289299
// Scale the projection matrix for the zoom effect.
290-
this.projection_matrix = this.projection_matrix.scale(
291-
1 + (frame_delta * -this.zoom_speed / 1000),
292-
1 + (frame_delta * -this.zoom_speed / 1000),
293-
1
294-
);
300+
if ((this.zoom_level < 30000 && this.zoom_speed < 0)
301+
|| (this.zoom_level > 1 && this.zoom_speed > 0)) {
302+
const scale_factor = 1 + (frame_delta * -this.zoom_speed / 1000);
303+
this.projection_matrix = this.projection_matrix.scale(
304+
scale_factor,
305+
scale_factor,
306+
1
307+
);
308+
309+
this.zoom_level *= scale_factor;
310+
}
295311

296312
// Reduce the zoom speed over time.
297313
this.zoom_speed *= 1 - (frame_delta / 1000);
@@ -325,6 +341,6 @@ export class GL {
325341
}
326342

327343
// Schedule another tick
328-
window.setTimeout(() => this.event_loop(), 1000/60);
344+
window.setTimeout(() => this.event_loop(), (1000 / 60) - (Date.now() - frame_time));
329345
}
330346
}

0 commit comments

Comments
 (0)