From 56d60ad9ec1361b207e2624a23e45605149bbac5 Mon Sep 17 00:00:00 2001
From: TheJoeCoder <joe@radialbog9.uk>
Date: Mon, 5 Aug 2024 13:24:02 +0100
Subject: [PATCH] Move mouse velocity calc to delta-timed main loop

---
 static/js/background_anim.js | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/static/js/background_anim.js b/static/js/background_anim.js
index 6057c0f..ff2ef4f 100644
--- a/static/js/background_anim.js
+++ b/static/js/background_anim.js
@@ -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", () => {
-- 
GitLab