Skip to content

Commit f82a679

Browse files
committed
enhance bbox naming
1 parent 6a50d49 commit f82a679

32 files changed

+354
-345
lines changed

src/renderer/app/db.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[renderer.theme.db :refer [Theme]]
1414
[renderer.timeline.db :refer [Timeline]]
1515
[renderer.tool.db :refer [Handle Tool State Cursor]]
16-
[renderer.utils.bounds :refer [Bounds]]
16+
[renderer.utils.bounds :refer [BBox]]
1717
[renderer.utils.i18n :as i18n]
1818
[renderer.utils.math :refer [Vec2]]
1919
[renderer.window.db :refer [Window]]))
@@ -74,7 +74,7 @@
7474
[:fx {:default []} vector?]
7575
[:pivot-point {:optional true} Vec2]
7676
[:clicked-element {:optional true} [:or Element Handle]]
77-
[:copied-bounds {:optional true} Bounds]
77+
[:copied-bbox {:optional true} BBox]
7878
[:copied-elements {:optional true} [:* Element]]
7979
[:kdtree {:optional true} [:maybe map?]]
8080
[:viewbox-kdtree {:optional true} [:maybe map?]]

src/renderer/document/handlers.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
(-> db :dom-rect)
8383
(-> db :window :focused)
8484
(not (get-in db (path db :focused))))
85-
(-> (frame.h/focus-bounds :original)
85+
(-> (frame.h/focus-bbox :original)
8686
(assoc-in (path db :focused) true)
8787
(snap.h/update-viewport-tree))))
8888

src/renderer/element/db.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[malli.core :as m]
44
[malli.transform :as mt]
55
[renderer.element.hierarchy :as hierarchy]
6-
[renderer.utils.bounds :refer [Bounds]]))
6+
[renderer.utils.bounds :refer [BBox]]))
77

88
(defn tag?
99
[k]
@@ -33,7 +33,7 @@
3333
[:locked {:optional true} boolean?]
3434
[:selected {:optional true} boolean?]
3535
[:children {:default [] :optional true} [:vector uuid?]]
36-
[:bounds {:optional true} Bounds]
36+
[:bbox {:optional true} BBox]
3737
[:content {:optional true} string?]
3838
[:attrs {:optional true} Attrs]])
3939

src/renderer/element/effects.cljs

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
(.close print-window))))
1616

1717
(defn data-url->canvas-context!
18-
[data-url [width height] f]
18+
[data-url [w h] f]
1919
(let [canvas (js/document.createElement "canvas")
2020
context (.getContext canvas "2d")
2121
image (js/Image.)]
2222
(set! (.-onload image)
23-
#(do (set! (.-width canvas) width)
24-
(set! (.-height canvas) height)
25-
(.drawImage context image 0 0 width height)
23+
#(do (set! (.-width canvas) w)
24+
(set! (.-height canvas) h)
25+
(.drawImage context image 0 0 w h)
2626
(f context)))
2727
(set! (.-src image) data-url)))
2828

@@ -31,19 +31,19 @@
3131
(fn [images]
3232
(doseq [image images]
3333
(let [data-url (-> image :attrs :href)
34-
[x y] (:bounds image)
34+
[x y] (:bbox image)
3535
;; TODO: Handle preserveAspectRatio.
36-
width (length/unit->px (-> image :attrs :width))
37-
height (length/unit->px (-> image :attrs :height))]
36+
w (length/unit->px (-> image :attrs :width))
37+
h (length/unit->px (-> image :attrs :height))]
3838
(data-url->canvas-context!
3939
data-url
40-
[width height]
40+
[w h]
4141
(fn [context]
4242
(rf/dispatch
4343
[::worker.e/create
4444
{:action "trace"
4545
:data {:label (:label image)
46-
:image (.getImageData context 0 0 width height)
46+
:image (.getImageData context 0 0 w h)
4747
:position [x y]}
4848
:on-success [::e/traced]}])))))))
4949

@@ -58,16 +58,16 @@
5858
img (js/document.createElement "img")]
5959
(set! (.-onload img)
6060
(fn []
61-
(let [width (.-width img)
62-
height (.-height img)]
61+
(let [w (.-width img)
62+
h (.-height img)]
6363
(.remove img)
6464
(rf/dispatch [::e/add {:type :element
6565
:tag :image
6666
:label (.-name file)
67-
:attrs {:x (- x (/ width 2))
68-
:y (- y (/ height 2))
69-
:width width
70-
:height height
67+
:attrs {:x (- x (/ w 2))
68+
:y (- y (/ h 2))
69+
:width w
70+
:height h
7171
:href data-url}}]))))
7272
(set! (.-src img) data-url)))
7373
(.readAsDataURL reader file))))

src/renderer/element/events.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
(rf/reg-event-db
204204
::scale
205205
(fn [db [_ ratio]]
206-
(let [pivot-point (-> db h/bounds bounds/center)]
206+
(let [pivot-point (-> db h/bbox bounds/center)]
207207
(-> (h/scale db ratio pivot-point false)
208208
(history.h/finalize "Scale selection")))))
209209

src/renderer/element/handlers.cljs

+54-50
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[renderer.notification.handlers :as notification.h]
1515
[renderer.notification.views :as notification.v]
1616
[renderer.utils.attribute :as attr]
17-
[renderer.utils.bounds :as bounds :refer [Bounds]]
17+
[renderer.utils.bounds :as bounds :refer [BBox]]
1818
[renderer.utils.element :as element]
1919
[renderer.utils.extra :refer [partial-right]]
2020
[renderer.utils.hiccup :as hiccup]
@@ -102,39 +102,42 @@
102102
parent-el
103103
(recur (parent db (:id parent-el)))))))
104104

105-
(m/=> adjusted-bounds [:-> App uuid? [:maybe Bounds]])
106-
(defn adjusted-bounds
105+
(m/=> adjusted-bbox [:-> App uuid? [:maybe BBox]])
106+
(defn adjusted-bbox
107107
[db id]
108108
(loop [container (parent-container db id)
109-
bounds (hierarchy/bounds (entity db id))]
110-
(if-not (and container bounds)
111-
bounds
112-
(let [[offset-x offset-y _ _] (hierarchy/bounds container)
113-
[x1 y1 x2 y2] bounds]
109+
bbox (hierarchy/bbox (entity db id))]
110+
(if-not (and container bbox)
111+
bbox
112+
(let [[offset-x offset-y _ _] (hierarchy/bbox container)
113+
[min-x min-y max-x max-y] bbox]
114114
(recur
115115
(parent-container db (:id container))
116-
[(+ x1 offset-x) (+ y1 offset-y) (+ x2 offset-x) (+ y2 offset-y)])))))
116+
[(+ min-x offset-x)
117+
(+ min-y offset-y)
118+
(+ max-x offset-x)
119+
(+ max-y offset-y)])))))
117120

118-
(m/=> refresh-bounds [:-> App uuid? App])
119-
(defn refresh-bounds
121+
(m/=> refresh-bbox [:-> App uuid? App])
122+
(defn refresh-bbox
120123
[db id]
121124
(let [el (entity db id)
122-
bounds (if (= (:tag el) :g)
123-
(let [b (map #(adjusted-bounds db %) (children-ids db id))]
124-
(when (seq b) (apply bounds/union b)))
125-
(adjusted-bounds db id))]
126-
(if (or (not bounds) (element/root? el))
125+
bbox (if (= (:tag el) :g)
126+
(let [b (map #(adjusted-bbox db %) (children-ids db id))]
127+
(when (seq b) (apply bounds/union b)))
128+
(adjusted-bbox db id))]
129+
(if (or (not bbox) (element/root? el))
127130
db
128-
(-> (reduce refresh-bounds db (children-ids db id))
129-
(update-in (path db id) assoc :bounds bounds)))))
131+
(-> (reduce refresh-bbox db (children-ids db id))
132+
(update-in (path db id) assoc :bbox bbox)))))
130133

131134
(m/=> update-el [:-> App uuid? ifn? [:* any?] App])
132135
(defn update-el
133136
[db id f & more]
134137
(if (locked? db id)
135138
db
136139
(-> (apply update-in db (path db id) f more)
137-
(refresh-bounds id))))
140+
(refresh-bbox id))))
138141

139142
(m/=> siblings-selected? [:-> App [:maybe boolean?]])
140143
(defn siblings-selected?
@@ -241,7 +244,7 @@
241244
(defn update-prop
242245
[db id k & more]
243246
(-> (apply update-in db (path db id k) more)
244-
(refresh-bounds id)))
247+
(refresh-bbox id)))
245248

246249
(m/=> assoc-prop [:function
247250
[:-> App keyword? any? App]
@@ -253,7 +256,7 @@
253256
(-> (if (str/blank? v)
254257
(update-in db (path db id) dissoc k)
255258
(assoc-in db (path db id k) v))
256-
(refresh-bounds id))))
259+
(refresh-bbox id))))
257260

258261
(m/=> dissoc-attr [:function
259262
[:-> App keyword? App]
@@ -276,7 +279,7 @@
276279
(cond-> db
277280
(not (locked? db id))
278281
(-> (assoc-in (path db id :attrs k) v)
279-
(refresh-bounds id)))))
282+
(refresh-bbox id)))))
280283

281284
(m/=> set-attr [:function
282285
[:-> App keyword? any? App]
@@ -437,10 +440,10 @@
437440
(:active-document db)
438441
(assoc-in [:documents (:active-document db) :ignored-ids] #{})))
439442

440-
(m/=> bounds [:-> App [:maybe Bounds]])
441-
(defn bounds
443+
(m/=> bbox [:-> App [:maybe BBox]])
444+
(defn bbox
442445
[db]
443-
(element/united-bounds (selected db)))
446+
(element/united-bbox (selected db)))
444447

445448
(m/=> copy [:-> App App])
446449
(defn copy
@@ -449,7 +452,7 @@
449452
(cond-> db
450453
(seq els)
451454
(assoc :copied-elements els
452-
:copied-bounds (bounds db)))))
455+
:copied-bbox (bbox db)))))
453456

454457
(m/=> delete [:function
455458
[:-> App App]
@@ -509,7 +512,7 @@
509512
(let [svgs (reverse (root-svgs db))
510513
pointer-pos (:adjusted-pointer-pos db)]
511514
(or
512-
(some #(when (bounds/contained-point? (:bounds %) pointer-pos) %) svgs)
515+
(some #(when (bounds/contained-point? (:bbox %) pointer-pos) %) svgs)
513516
(root db))))
514517

515518
(m/=> translate [:function
@@ -531,7 +534,7 @@
531534
(reduce (partial-right place position) db (top-ancestor-ids db)))
532535
([db id position]
533536
(let [el (entity db id)
534-
center (bounds/center (hierarchy/bounds el))
537+
center (bounds/center (hierarchy/bbox el))
535538
offset (mat/sub position center)]
536539
(update-el db id hierarchy/translate offset))))
537540

@@ -544,7 +547,7 @@
544547
(reduce
545548
(fn [db id]
546549
(let [adjusted-pivot-point (->> (entity db id)
547-
:bounds
550+
:bbox
548551
(take 2)
549552
(mat/sub pivot-point))]
550553
(update-el db id hierarchy/scale ratio adjusted-pivot-point)))
@@ -558,19 +561,20 @@
558561
([db direction]
559562
(reduce (partial-right align direction) db (selected-ids db)))
560563
([db id direction]
561-
(let [el-bounds (:bounds (entity db id))
562-
center (bounds/center el-bounds)
563-
parent-bounds (:bounds (parent db id))
564-
parent-center (bounds/center parent-bounds)
564+
(let [el-bbox (:bbox (entity db id))
565+
center (bounds/center el-bbox)
566+
parent-bbox (:bbox (parent db id))
567+
parent-center (bounds/center parent-bbox)
565568
[cx cy] (mat/sub parent-center center)
566-
[x1 y1 x2 y2] (mat/sub parent-bounds el-bounds)]
569+
delta-bbox (mat/sub parent-bbox el-bbox)
570+
[min-x-delta min-y-delta max-x-delta max-y-delta] delta-bbox]
567571
(translate db id (case direction
568-
:top [0 y1]
572+
:top [0 min-y-delta]
569573
:center-vertical [0 cy]
570-
:bottom [0 y2]
571-
:left [x1 0]
574+
:bottom [0 max-y-delta]
575+
:left [min-x-delta 0]
572576
:center-horizontal [cx 0]
573-
:right [x2 0])))))
577+
:right [max-x-delta 0])))))
574578

575579
(m/=> ->path [:function
576580
[:-> App App]
@@ -592,13 +596,13 @@
592596
([db id]
593597
(update-el db id element/stroke->path)))
594598

595-
(m/=> overlapping-svg [:-> App Bounds Element])
599+
(m/=> overlapping-svg [:-> App BBox Element])
596600
(defn overlapping-svg
597-
[db el-bounds]
601+
[db el-bbox]
598602
(let [svgs (reverse (root-svgs db))] ; Reverse to select top svgs first.
599603
(or
600-
(some #(when (bounds/contained? el-bounds (:bounds %)) %) svgs)
601-
(some #(when (bounds/intersect? el-bounds (:bounds %)) %) svgs)
604+
(some #(when (bounds/contained? el-bbox (:bbox %)) %) svgs)
605+
(some #(when (bounds/intersect? el-bbox (:bbox %)) %) svgs)
602606
(root db))))
603607

604608
(m/=> assoc-parent-id [:-> App Element Element])
@@ -608,7 +612,7 @@
608612
(not (or (element/root? el) (:parent el)))
609613
(assoc :parent (:id (if (element/svg? el)
610614
(root db)
611-
(overlapping-svg db (hierarchy/bounds el)))))))
615+
(overlapping-svg db (hierarchy/bbox el)))))))
612616

613617
(m/=> create [:-> App map? App])
614618
(defn create
@@ -624,7 +628,7 @@
624628
child-els (-> (entities db (set (:children el)))
625629
(vals)
626630
(concat (:content el)))
627-
[x1 y1] (hierarchy/bounds (entity db (:parent new-el)))
631+
[min-x min-y] (hierarchy/bbox (entity db (:parent new-el)))
628632
add-children (fn [db child-els]
629633
(reduce #(cond-> %1
630634
(db/tag? (:tag %2))
@@ -641,10 +645,10 @@
641645
(update-prop (:parent new-el) :children #(vec (conj % id)))
642646

643647
(not (or (element/svg? new-el) (element/root? new-el) (:parent el)))
644-
(translate [(- x1) (- y1)])
648+
(translate [(- min-x) (- min-y)])
645649

646650
:always
647-
(refresh-bounds id)
651+
(refresh-bbox id)
648652

649653
child-els
650654
(add-children child-els)))))
@@ -703,11 +707,11 @@
703707
(let [parent-el (hovered-svg db)]
704708
(reduce (partial-right paste parent-el) (deselect-all db) (:copied-elements db))))
705709
([db el parent-el]
706-
(let [center (bounds/center (:copied-bounds db))
707-
el-center (bounds/center (:bounds el))
710+
(let [center (bounds/center (:copied-bbox db))
711+
el-center (bounds/center (:bbox el))
708712
offset (mat/sub el-center center)
709-
el (dissoc el :bounds)
710-
[s-x1 s-y1] (:bounds parent-el)
713+
el (dissoc el :bbox)
714+
[s-x1 s-y1] (:bbox parent-el)
711715
pointer-pos (:adjusted-pointer-pos db)]
712716
(reduce
713717
select

src/renderer/element/hierarchy.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(defmulti area :tag)
2121
(defmulti centroid :tag)
2222
(defmulti snapping-points :tag)
23-
(defmulti bounds :tag)
23+
(defmulti bbox :tag)
2424
(defmulti translate (fn [el _offset] (:tag el)))
2525
(defmulti scale (fn [el _ratio _pivot-point] (:tag el)))
2626
(defmulti edit (fn [el _offset _handle] (:tag el)))
@@ -29,7 +29,7 @@
2929
(defmethod render :default [])
3030
(defmethod render-to-string :default [element] [render element])
3131
(defmethod render-edit :default [])
32-
(defmethod bounds :default [])
32+
(defmethod bbox :default [])
3333
(defmethod area :default [])
3434
(defmethod centroid :default [])
3535
(defmethod snapping-points :default [])

0 commit comments

Comments
 (0)