Skip to content

Commit 385f4be

Browse files
author
Dylan McGannon
committed
Enable click and drag.
1 parent d0e4f59 commit 385f4be

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

js/gl.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,55 @@ export class GL {
2929
this.zoom_level = 1;
3030
this.color_cycle = 0;
3131

32+
this.drag_active = false;
33+
this.drag_point = new Vector(0, 0, 0, 1);
34+
3235
// Add resize listener
3336
window.addEventListener('resize', () => {
3437
this.resize();
3538
});
3639

40+
// Add click listeners
41+
canvas.addEventListener('mousedown', (event) => {
42+
this.drag_active = true;
43+
44+
this.drag_point = this.view_matrix.inverse().multiply_vector(
45+
this.unproject(
46+
event.layerX,
47+
this.canvas.clientHeight - event.layerY,
48+
0
49+
)
50+
);
51+
});
52+
canvas.addEventListener('mouseup', (event) => {
53+
this.drag_active = false;
54+
});
55+
canvas.addEventListener('mousemove', (event) => {
56+
// Only move if the mouse is down.
57+
if (!this.drag_active) {
58+
return;
59+
}
60+
61+
// Stop any zooming going on.
62+
this.zoom_speed = 0;
63+
64+
// Unproject mouse coords into scene coords.
65+
const mouse_position = this.view_matrix.inverse().multiply_vector(
66+
this.unproject(
67+
event.layerX,
68+
this.canvas.clientHeight - event.layerY,
69+
0
70+
)
71+
);
72+
73+
// Set the camera position to: current pos - mouse pos + initial click pos
74+
this.set_camera_position(
75+
this.camera_position.x - mouse_position.x + this.drag_point.x,
76+
this.camera_position.y - mouse_position.y + this.drag_point.y,
77+
this.camera_position.z
78+
);
79+
});
80+
3781
// Add mouse wheel listener
3882
canvas.addEventListener('wheel', (event) => {
3983
this.zoom_target = this.view_matrix.inverse().multiply_vector(

0 commit comments

Comments
 (0)