commit ef27b5f5b9bed91f6abedddd278976e4754cafa2 parent 3faf2411824aa78b4f73027925830b99e668f2d2 Author: Jacob R. Edwards <jacob@jacobedwards.org> Date: Mon, 19 Aug 2024 09:20:49 -0700 Add snapTo method to Units This just snaps the given number to the closest given unit. Diffstat:
| M | files/floorplans/floorplan/editor.js | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/files/floorplans/floorplan/editor.js b/files/floorplans/floorplan/editor.js @@ -148,6 +148,32 @@ class Units { } return t } + + snapTo(x, unit) { + let n = this.get(unit) + let f = function(x) { + x = Math.round(x) + return x - (x % n) + } + + if (typeof x === "number") { + return f(x) + } else if (Array.isArray(x)) { + for (let i in x) { + x[i] = f(x[i]) + } + } else if (typeof x === "object") { + for (let i in x) { + if (typeof x[i] === "number") { + x[i] = f(x[i]) + } + } + } else { + throw new Error("Unable to snap that") + } + + return x + } } export class FloorplanEditor {