1
Fork 0
mirror of https://github.com/RGBCube/rgbcube.github.io synced 2025-05-14 05:54:58 +00:00
This commit is contained in:
RGBCube 2023-12-19 15:55:10 +03:00
parent 36fe3c4a4b
commit ad4f02c875
No known key found for this signature in database

53
cube.js
View file

@ -97,16 +97,11 @@ const orientation = {
previous: null,
};
document.addEventListener("mouseup", () => {
mouse.down = false;
});
document.addEventListener("mousedown", (event) => {
// Disables link dragging that occurs when spinning.
event.preventDefault();
mouse.down = true;
});
document.addEventListener("mousemove", (event) => {
if (!mouse.down) return;
@ -127,4 +122,50 @@ const orientation = {
orientation.set(Quat.mul(rotation, orientation.get()));
});
let angularMomentum = new Vec3(0, 0, 0);
document.addEventListener("mousedown", (event) => {
// Disables link dragging that occurs when spinning.
event.preventDefault();
mouse.down = true;
angularMomentum = new Vec3(0, 0, 0);
});
let lastUpdate = 0;
const updateFrame = (timestamp) => {
if (lastUpdate == 0) lastUpdate = timestamp;
const delta = (timestamp - lastUpdate) / 1000;
lastUpdate = timestamp;
const axis = angularMomentum;
const omega = angularMomentum.length();
const decay = Math.exp(-delta * friction);
const effectiveDelta = friction > 0 ? (1 - decay) / friction : delta;
let theta = effectiveDelta * omega;
angularMomentum.x *= decay;
angularMomentum.y *= decay;
angularMomentum.z *= decay;
if (friction > 0 && angularMomentum.length() < 0.00001) {
theta += angularMomentum.length() / friction;
angularMomentum.x = 0;
angularMomentum.y = 0;
angularMomentum.z = 0;
}
if (!mouse.down) orientation.set(Quat.mul(Quat.fromAngleAxis(theta, axis), orientation.get()));
requestAnimationFrame(updateFrame);
};
updateFrame(0);
})();