commit c4704e006c9d608d8e9201b6ffe88b09b0dc7a00
parent 136cf10bd716d77408b682a284ce5ccd6effe6ba
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 16 Oct 2024 21:40:30 -0700
Implement method to fit the floor plan to view
Diffstat:
2 files changed, 25 insertions(+), 22 deletions(-)
diff --git a/files/floorplans/floorplan/editor.js b/files/floorplans/floorplan/editor.js
@@ -495,6 +495,30 @@ export class FloorplanEditor {
grid.size(box.width + margin, box.height + margin).move(box.x - margin / 2, box.y - margin / 2)
}
+ fitToView() {
+ const adj = function(ns, t, pos, siz) {
+ t[pos] += (t[siz] - ns) / 2
+ t[siz] = ns
+ }
+ const add = function(d, t, pos, siz) {
+ return adj(t[siz] + d, t, pos, siz)
+ }
+
+ let bbox = this.draw.findOne("#floorplan").bbox()
+ let ft = this.units.get("foot")
+ let min = ft * 20
+ add(ft * 2, bbox, "x", "width")
+ if (bbox.width < min) {
+ adj(min, bbox, "x", "width")
+ }
+ add(ft * 2, bbox, "y", "height")
+ if (bbox.height < min) {
+ adj(min, bbox, "y", "height")
+ }
+ this.draw.viewbox(bbox)
+ this.updateGrid()
+ }
+
// Should be called after each user "action"
finishAction() {
this.backend.history.mark()
diff --git a/files/floorplans/floorplan/main.js b/files/floorplans/floorplan/main.js
@@ -234,29 +234,8 @@ function run(editor) {
if (editor.draw.findExactlyOne("#points").children().length === 0) {
editor.addPoint({ x: 0, y: 0 })
}
-
- const adj = function(ns, t, pos, siz) {
- t[pos] += (t[siz] - ns) / 2
- t[siz] = ns
- }
- const add = function(d, t, pos, siz) {
- return adj(t[siz] + d, t, pos, siz)
- }
-
- let bbox = editor.draw.findOne("#floorplan").bbox()
- let ft = editor.units.get("foot")
- let min = ft * 20
- add(ft * 2, bbox, "x", "width")
- if (bbox.width < min) {
- adj(min, bbox, "x", "width")
- }
- add(ft * 2, bbox, "y", "height")
- if (bbox.height < min) {
- adj(min, bbox, "y", "height")
- }
- editor.draw.viewbox(bbox)
editor.useGrid("imperial")
- editor.updateGrid()
+ editor.fitToView()
})
let preventWhenSel = function(e) {