From fc7302f8fa575ebba7debbacdc1ecb7d226292e2 Mon Sep 17 00:00:00 2001 From: Victor Seiji Hariki Date: Sat, 17 Dec 2022 01:20:51 -0300 Subject: [PATCH] fix snapping for negative i values Signed-off-by: Victor Seiji Hariki --- js/lib/util.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/lib/util.js b/js/lib/util.js index d6ac39f..23a88e6 100644 --- a/js/lib/util.js +++ b/js/lib/util.js @@ -244,7 +244,12 @@ function makeWriteOnce(obj, name = "write-once object", exceptions = []) { * @returns an offset, in which [i + offset = (a location snapped to the grid)] */ function snap(i, offset = 0, gridSize = 64) { - const modulus = (i - offset) % gridSize; + let diff = i - offset; + if (diff < 0) { + diff += gridSize * Math.ceil(Math.abs(diff / gridSize)); + } + + const modulus = diff % gridSize; var snapOffset = modulus; if (modulus > gridSize / 2) snapOffset = modulus - gridSize;