www.spaceplanner.app

Web client to the spaceplanner API
git clone git://jacobedwards.org/www.spaceplanner.app
Log | Files | Refs

commit 5faed480b12decee70d45270832d8b8a866ad122
parent 3e35b99b927939f442da656895ad2c576946f92e
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Tue, 17 Sep 2024 20:18:24 -0700

Add point alignment snapping

Now when moving a point it's x and y axis are snapped independently
to all other points if their axis is close enough.  Currently if
points are behind walls they are still snapped to, which I consider
a bug but I'm not going to worry about it now.

This also adds the ability to disable all snapping by pressing
shift.

Diffstat:
Mfiles/floorplans/floorplan/main.js | 40+++++++++++++++++++++++++++++++++++++++-
1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/files/floorplans/floorplan/main.js b/files/floorplans/floorplan/main.js @@ -499,9 +499,46 @@ function precisePointHandler(event, editor, state) { } } const doMove = function() { + const ad = function(a, b) { + return Math.abs(a - b) + } + const updsnaps = function(snaps, k, from, test) { + let thres = 650 + let d = ad(from[k], test[k]) + if (d <= thres) { + if (!snaps[k] || d < snaps[k].d) { + snaps[k] = { d, v: test[k] } + } + } + } + // This is racy state.moveTimeout = null - updatePoint(snap(editor.units.snapTo(state.move, editor.unit), state.origin, 8)) + if (state.nosnap) { + updatePoint(state.move) + return + } + + let snapped = snap(editor.units.snapTo(state.move, editor.unit), state.origin, 8) + + let points = editor.backend.cache.points + let snaps = {} + console.log(points) + let exclude = lib.getID(state.to) + for (let p in points) { + if (p != exclude) { + updsnaps(snaps, "x", snapped, points[p]) + updsnaps(snaps, "y", snapped, points[p]) + } + } + if (snaps.x != null) { + snapped.x = snaps.x.v + } + if (snaps.y != null) { + snapped.y = snaps.y.v + } + console.log(snaps.x, snaps.y) + updatePoint(snapped) } const revert = function() { /* @@ -589,6 +626,7 @@ function precisePointHandler(event, editor, state) { if (event.type === "mousemove") { // This is still far too expensive, it runs up my fans in seconds. state.move = cursor + state.nosnap = event.shiftKey if (state.moveTimeout == null) { state.moveTimeout = setTimeout(doMove, 35) }