mirror of
https://github.com/RGBCube/rgbcube.github.io
synced 2025-05-28 11:55:12 +00:00
Make code better
This commit is contained in:
parent
b800086988
commit
36fe3c4a4b
1 changed files with 5 additions and 15 deletions
20
cube.js
20
cube.js
|
@ -35,12 +35,6 @@ class Vec3 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Vec2 extends Vec3 {
|
|
||||||
constructor(x, y) {
|
|
||||||
super(x, y, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Quat {
|
class Quat {
|
||||||
constructor(x, y, z, w) {
|
constructor(x, y, z, w) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
|
@ -99,14 +93,10 @@ const orientation = {
|
||||||
(() => {
|
(() => {
|
||||||
const mouse = {
|
const mouse = {
|
||||||
down: false,
|
down: false,
|
||||||
lastMove: window.performance.now(),
|
lastMove: 0,
|
||||||
previous: new Vec2(0, 0),
|
previous: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
document.addEventListener("mouseleave", () => {
|
|
||||||
mouse.down = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
document.addEventListener("mouseup", () => {
|
document.addEventListener("mouseup", () => {
|
||||||
mouse.down = false;
|
mouse.down = false;
|
||||||
});
|
});
|
||||||
|
@ -120,19 +110,19 @@ const orientation = {
|
||||||
document.addEventListener("mousemove", (event) => {
|
document.addEventListener("mousemove", (event) => {
|
||||||
if (!mouse.down) return;
|
if (!mouse.down) return;
|
||||||
|
|
||||||
const newMouse = new Vec2(event.clientX, event.clientY);
|
const newMouse = new Vec3(event.clientX, event.clientY, 0);
|
||||||
|
|
||||||
if (window.performance.now() - mouse.lastMove > 100) {
|
if (window.performance.now() - mouse.lastMove > 100) {
|
||||||
// This is a fresh scroll.
|
// This is a fresh scroll.
|
||||||
mouse.previous = newMouse;
|
mouse.previous = newMouse;
|
||||||
}
|
}
|
||||||
|
|
||||||
const delta = Vec2.sub(newMouse, mouse.previous);
|
const delta = Vec3.sub(newMouse, mouse.previous);
|
||||||
|
|
||||||
mouse.previous = newMouse;
|
mouse.previous = newMouse;
|
||||||
mouse.lastMove = window.performance.now();
|
mouse.lastMove = window.performance.now();
|
||||||
|
|
||||||
const axis = new Vec2(-delta.y,delta.x);
|
const axis = new Vec3(-delta.y, delta.x, 0);
|
||||||
const rotation = Quat.fromAngleAxis(delta.length() * sensitivity, axis);
|
const rotation = Quat.fromAngleAxis(delta.length() * sensitivity, axis);
|
||||||
|
|
||||||
orientation.set(Quat.mul(rotation, orientation.get()));
|
orientation.set(Quat.mul(rotation, orientation.get()));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue