From 7e4a709144b1b1ebdc594180fb00c52a9bf75556 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Fri, 10 Jul 2026 10:53:15 -0300 Subject: [PATCH] fixing background --- js/matrix.js | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/js/matrix.js b/js/matrix.js index 7c0de6f..8d8c727 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -168,16 +168,39 @@ class MatrixBackground { dot.vx += (dot.baseVx + wanderX - dot.vx) * 0.02; dot.vy += (dot.baseVy + wanderY - dot.vy) * 0.02; - const maxX = this.canvas.width - dot.size; - const maxY = this.canvas.height - dot.size; - if (dot.x <= 0 || dot.x >= maxX) { - dot.vx = -dot.vx; - dot.x = Math.max(0, Math.min(maxX, dot.x)); + // Reflect at the canvas edge. Keep the base velocity in sync with + // the bounce so the drift easing does not push the dot back into + // the same wall on the next frames. + const radius = dot.size / 2; + const minX = radius; + const minY = radius; + const maxX = Math.max(minX, this.canvas.width - radius); + const maxY = Math.max(minY, this.canvas.height - radius); + + if (dot.x <= minX && dot.vx < 0) { + dot.x = minX + (minX - dot.x); + dot.vx = Math.abs(dot.vx); + dot.baseVx = Math.abs(dot.baseVx); + } else if (dot.x >= maxX && dot.vx > 0) { + dot.x = maxX - (dot.x - maxX); + dot.vx = -Math.abs(dot.vx); + dot.baseVx = -Math.abs(dot.baseVx); } - if (dot.y <= 0 || dot.y >= maxY) { - dot.vy = -dot.vy; - dot.y = Math.max(0, Math.min(maxY, dot.y)); + + if (dot.y <= minY && dot.vy < 0) { + dot.y = minY + (minY - dot.y); + dot.vy = Math.abs(dot.vy); + dot.baseVy = Math.abs(dot.baseVy); + } else if (dot.y >= maxY && dot.vy > 0) { + dot.y = maxY - (dot.y - maxY); + dot.vy = -Math.abs(dot.vy); + dot.baseVy = -Math.abs(dot.baseVy); } + + // A large pointer impulse can overshoot both edges by more than + // one frame, so clamp after reflecting the position. + dot.x = Math.max(minX, Math.min(maxX, dot.x)); + dot.y = Math.max(minY, Math.min(maxY, dot.y)); }); }