-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs3.js
More file actions
44 lines (40 loc) · 1.18 KB
/
js3.js
File metadata and controls
44 lines (40 loc) · 1.18 KB
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
let oldPos = new Point(0, 0, 0);
let newPos = new Point(0, 0, 0);
let isdraging = false;
let axisVector = new Point(0, 0, 0);
const theta = Math.PI / 180;
const cosTheta = Math.cos(theta);
const sinTheta = Math.sin(theta);
canvas.addEventListener("mousedown", mousedown);
canvas.addEventListener("mousemove", mousemove);
canvas.addEventListener("mouseup", mouseup);
function mousedown(e) {
isdraging = true;
x = e.layerX - width / 2;
y = e.layerY - height / 2;
z = Math.sqrt((width * width) / 4 - x * x - y * y);
oldPos.setPos(x, y, z);
}
function mousemove(e) {
if (isdraging) {
x = e.layerX - width / 2;
y = e.layerY - height / 2;
z = Math.sqrt((width * width) / 4 - x * x - y * y);
newPos.setPos(x, y, z);
//rotation part here
let cp = oldPos.crossProduct(newPos);
if (cp.x != 0) {
axisVector = cp.nomalize();
rotationQuaternion = new Quaternion(
cosTheta,
axisVector.multiply(sinTheta)
);
let temp = rotationQuaternion.calculateRotationMatrix();
matrix = multiplyMatrix(temp, matrix);
oldPos.setPos(x, y, z);
}
}
}
function mouseup(e) {
isdraging = false;
}