-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
97 lines (76 loc) · 2.47 KB
/
events.js
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
// rotatePoints(grid.points, 0, 0, .0001, 0, 2);
// translatePoints(grid.points, 100, 100, 0);
mouseInfo = {
gridsquare: null,
}
renderScene(defaultScene);
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
// y: canvas.height - (evt.clientY - rect.top)
y: evt.clientY - rect.top
};
}
function mouseOrbit(evt, downX, downY, startNav) {
mousePos = getMousePos(canvas, evt);
defaultScene.navTransforms.orbitZ = startNav.orbitZ + ((downX - (mousePos.x)));
defaultScene.navTransforms.orbitY = startNav.orbitY + ((downY - (mousePos.y)));
renderScene(defaultScene);
}
function mousePan(evt, downX, downY, startNav) {
mousePos = getMousePos(canvas, evt);
defaultScene.navTransforms.panX = startNav.panX - ((downX - (mousePos.x)));
defaultScene.navTransforms.panY = startNav.panY + ((downY - (mousePos.y)));
renderScene(defaultScene);
}
window.addEventListener('keydown', (event) => {
if (event.key == 'z') {
toggleWireframe();
}
});
canvas.addEventListener('mousedown', function (evt) {
mousePos = getMousePos(canvas, evt);
pressedMouse = true;
canvas.onmouseup = function (something) {
pressedMouse = false;
}
downX = mousePos.x;
downY = mousePos.y;
startNav = JSON.parse(JSON.stringify(defaultScene.navTransforms));
canvas.onmousemove = function (e) {
if (pressedMouse == true) {
if (evt.button == 0) {
mouseOrbit(e, downX, downY, startNav);
}
}
if (pressedMouse == true) {
if (evt.button == 2) {
mousePan(e, downX, downY, startNav);
}
}
}
}, false);
window.addEventListener('contextmenu', function (e) { e.preventDefault(); });
canvas.addEventListener('wheel', checkScrollDirection);
function checkScrollDirection(event) {
if (checkScrollDirectionIsUp(event)) {
defaultScene.navTransforms.zoom += .1;
renderScene(defaultScene);
} else {
if (defaultScene.navTransforms.zoom > .15) {
defaultScene.navTransforms.zoom -= .1;
}
renderScene(defaultScene);
}
}
function checkScrollDirectionIsUp(event) {
if (event.wheelDelta) {
return event.wheelDelta > 0;
}
return event.deltaY < 0;
}
function toggleWireframe() {
defaultScene.display.wireframe = Math.abs(defaultScene.display.wireframe - 1);
renderScene(defaultScene);
}