Skip to content
Snippets Groups Projects
Verified Commit 56d60ad9 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Move mouse velocity calc to delta-timed main loop

parent 1213bfdf
Branches
No related tags found
No related merge requests found
Pipeline #183 passed
......@@ -234,6 +234,16 @@ const draw = () => {
}
};
const mouseVelocity = () => {
// Work out velocity
mouseVel[0] = mousePos[0] - prevMousePos[0];
mouseVel[1] = mousePos[1] - prevMousePos[1];
// Set previous mouse pos
prevMousePos[0] = mousePos[0];
prevMousePos[1] = mousePos[1];
};
const physics = () => {
for (let i = 0; i < dots.length; i++) {
let dot = dots[i]; // We have to do it this way since we want the dots to update
......@@ -321,6 +331,7 @@ const tick = (timeStamp) => {
if(deltaTime >= (1 / FPS) * 1000) {
zero = timeStamp;
draw();
mouseVelocity();
physics();
}
......@@ -332,14 +343,6 @@ window.addEventListener("mousemove", (event) => {
// Scale clientX and clientY in case the screen size has changed
mousePos[0] = (canvas.width / window.innerWidth) * event.clientX;
mousePos[1] = (canvas.height / window.innerHeight) * event.clientY;
// Work out velocity
mouseVel[0] = mousePos[0] - prevMousePos[0];
mouseVel[1] = mousePos[1] - prevMousePos[1];
// Set previous mouse pos
prevMousePos[0] = mousePos[0];
prevMousePos[1] = mousePos[1];
});
window.addEventListener("resize", () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment