Skip to content

Commit 3ac655f

Browse files
committed
introduce scale-offset fn and fix styles
1 parent db251b3 commit 3ac655f

File tree

12 files changed

+61
-46
lines changed

12 files changed

+61
-46
lines changed

src/renderer/element/impl/box.cljs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"This serves as an abstraction for box elements that share the
33
:x :y :width :height attributes (e.g. rect, svg, image)."
44
(:require
5-
[clojure.core.matrix :as matrix]
65
[renderer.element.hierarchy :as element.hierarchy]
76
[renderer.tool.views :as tool.views]
87
[renderer.utils.bounds :as utils.bounds]
@@ -18,7 +17,7 @@
1817
(defmethod element.hierarchy/scale ::element.hierarchy/box
1918
[el ratio pivot-point]
2019
(let [[x y] ratio
21-
offset (matrix/sub pivot-point (matrix/mul pivot-point ratio))]
20+
offset (utils.element/scale-offset ratio pivot-point)]
2221
(-> (utils.element/update-attrs-with el * [[:width x] [:height y]])
2322
(element.hierarchy/translate offset))))
2423

src/renderer/element/impl/container/canvas.cljs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
nearest-neighbor @(rf/subscribe [::snap.subs/nearest-neighbor])
5454
snapped-el-id (-> nearest-neighbor meta :id)
5555
snapped-el (when snapped-el-id @(rf/subscribe [::element.subs/entity snapped-el-id]))
56-
keyboard-handler #(rf/dispatch-sync [::tool.events/keyboard-event (utils.keyboard/event-formatter %)])]
56+
key-handler #(rf/dispatch-sync [::tool.events/keyboard-event (utils.keyboard/event-formatter %)])]
5757
[:svg#canvas {:on-pointer-up pointer-handler
5858
:on-pointer-down pointer-handler
5959
:on-pointer-move pointer-handler
60-
:on-key-up keyboard-handler
61-
:on-key-down keyboard-handler
60+
:on-key-up key-handler
61+
:on-key-down key-handler
6262
:tab-index 0 ; Enable keyboard events
6363
:viewBox viewbox-attr
6464
:on-drop drop-handler!

src/renderer/element/impl/custom/blob.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
(defmethod element.hierarchy/scale :blob
9292
[el ratio pivot-point]
93-
(let [offset (matrix/sub pivot-point (matrix/mul pivot-point ratio))
93+
(let [offset (utils.element/scale-offset ratio pivot-point)
9494
ratio (apply min ratio)]
9595
(-> el
9696
(attr.hierarchy/update-attr :size * ratio)

src/renderer/element/impl/custom/brush.cljs

+3-5
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,14 @@
157157
(defmethod element.hierarchy/scale :brush
158158
[el ratio pivot-point]
159159
(let [bbox-min (take 2 (element.hierarchy/bbox el))
160-
pivot-point (matrix/sub pivot-point (matrix/mul pivot-point ratio))]
160+
offset (utils.element/scale-offset ratio pivot-point)]
161161
(update-in el
162162
[:attrs :points]
163163
#(->> (into [] partition-to-px (utils.attribute/str->seq %))
164164
(reduce (fn [points point]
165165
(let [rel-point (matrix/sub bbox-min (take 2 point))
166-
offset (matrix/add pivot-point
167-
(matrix/sub rel-point
168-
(matrix/mul rel-point
169-
ratio)))]
166+
rel-offset (utils.element/scale-offset ratio rel-point)
167+
offset (matrix/add offset rel-offset)]
170168
(translate offset points point))) [])
171169
(string/join " ")))))
172170

src/renderer/element/impl/shape/circle.cljs

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232

3333
(defmethod element.hierarchy/scale :circle
3434
[el ratio pivot-point]
35-
(let [dimensions (utils.bounds/->dimensions (element.hierarchy/bbox el))
36-
pivot-point (matrix/sub pivot-point (matrix/div dimensions 2))
37-
offset (matrix/sub pivot-point (matrix/mul pivot-point ratio))
35+
(let [dimensions (-> el element.hierarchy/bbox utils.bounds/->dimensions)
36+
pivot-point (->> (matrix/div dimensions 2)
37+
(matrix/sub pivot-point))
38+
offset (utils.element/scale-offset ratio pivot-point)
3839
ratio (apply min ratio)]
3940
(-> el
4041
(attr.hierarchy/update-attr :r * ratio)

src/renderer/element/impl/shape/ellipse.cljs

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
(defmethod element.hierarchy/scale :ellipse
3434
[el ratio pivot-point]
3535
(let [[x y] ratio
36-
dimensions (utils.bounds/->dimensions (element.hierarchy/bbox el))
37-
pivot-point (matrix/sub pivot-point (matrix/div dimensions 2))
38-
offset (matrix/sub pivot-point (matrix/mul pivot-point ratio))]
36+
dimensions (-> el element.hierarchy/bbox utils.bounds/->dimensions)
37+
pivot-point (->> (matrix/div dimensions 2)
38+
(matrix/sub pivot-point))
39+
offset (utils.element/scale-offset ratio pivot-point)]
3940
(-> (utils.element/update-attrs-with el * [[:rx x] [:ry y]])
4041
(element.hierarchy/translate offset))))
4142

src/renderer/element/impl/shape/line.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
dimensions (utils.bounds/->dimensions (element.hierarchy/bbox el))
3535
[x y] (matrix/sub dimensions (matrix/mul dimensions ratio))
3636
pivot-diff (matrix/sub pivot-point dimensions)
37-
offset (matrix/sub pivot-diff (matrix/mul pivot-diff ratio))]
37+
offset (utils.element/scale-offset ratio pivot-diff)]
3838
(-> (utils.element/update-attrs-with el + [[(if (< x1 x2) :x1 :x2) x]
3939
[(if (< y1 y2) :y1 :y2) y]])
4040
(element.hierarchy/translate offset))))

src/renderer/element/impl/shape/path.cljs

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
(defmethod element.hierarchy/scale :path
3434
[el ratio pivot-point]
3535
(let [[scale-x scale-y] ratio
36+
offset (utils.element/scale-offset ratio pivot-point)
3637
[x y] (element.hierarchy/bbox el)
37-
[x y] (matrix/sub (matrix/add [x y]
38-
(matrix/sub pivot-point
39-
(matrix/mul pivot-point ratio)))
40-
(matrix/mul ratio [x y]))]
38+
[x y] (-> (matrix/add [x y] offset)
39+
(matrix/sub (matrix/mul ratio [x y])))]
4140
(update-in el [:attrs :d] #(-> (svgpath %)
4241
(.scale scale-x scale-y)
4342
(.translate x y)

src/renderer/element/impl/shape/polyshape.cljs

+24-19
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,29 @@
3838
(defmethod element.hierarchy/scale ::element.hierarchy/polyshape
3939
[el ratio pivot-point]
4040
(let [bounds-min (take 2 (element.hierarchy/bbox el))
41-
pivot-point (matrix/sub pivot-point (matrix/mul pivot-point ratio))]
41+
offset (utils.element/scale-offset ratio pivot-point)]
4242
(update-in el
4343
[:attrs :points]
4444
#(->> (utils.attribute/str->seq %)
4545
(transduce
4646
partition-to-px
4747
(fn [points point]
4848
(let [rel-point (matrix/sub bounds-min point)
49-
offset (matrix/add pivot-point (matrix/sub rel-point (matrix/mul rel-point ratio)))]
49+
offset (->> ratio
50+
(matrix/mul rel-point)
51+
(matrix/sub rel-point)
52+
(matrix/add offset))]
5053
(translate offset points point))) [])
5154
(string/join " ")
5255
(string/trim)))))
5356

5457
(defmethod element.hierarchy/render-edit ::element.hierarchy/polyshape
5558
[el]
56-
[:g (map-indexed (fn [index [x y]]
57-
(let [[x y] (mapv utils.length/unit->px [x y])
58-
[x y] (matrix/add (utils.element/offset el) [x y])]
59+
[:g (map-indexed (fn [index point]
60+
(let [offset (utils.element/offset el)
61+
[x y] (->> point
62+
(mapv utils.length/unit->px)
63+
(matrix/add offset))]
5964
^{:key index}
6065
[tool.views/square-handle {:id (keyword (str index))
6166
:x x
@@ -87,9 +92,14 @@
8792
max-y (apply max (map #(utils.length/unit->px (second %)) points))]
8893
[min-x min-y max-x max-y]))
8994

90-
(defn calc-polygon-area
91-
[vertices]
92-
(let [count-v (count vertices)]
95+
(defn ->vertices
96+
[el]
97+
(-> el :attrs :points points->px))
98+
99+
(defmethod element.hierarchy/area ::element.hierarchy/polyshape
100+
[el]
101+
(let [vertices (->vertices el)
102+
count-v (count vertices)]
93103
(/ (reduce-kv (fn [area index point]
94104
(let [point-b (if (= index (dec count-v))
95105
(first vertices)
@@ -100,17 +110,12 @@
100110
0
101111
vertices) 2)))
102112

103-
(defmethod element.hierarchy/area ::element.hierarchy/polyshape
104-
[{{:keys [points]} :attrs}]
105-
(let [points-v (points->px points)]
106-
(calc-polygon-area points-v)))
107-
108113
(defmethod element.hierarchy/centroid ::element.hierarchy/polyshape
109-
[{{:keys [points]} :attrs}]
110-
(let [points-v (points->px points)]
111-
(matrix/div (reduce matrix/add [0 0] points-v)
112-
(count points-v))))
114+
[el]
115+
(let [vertices (->vertices el)]
116+
(-> (reduce matrix/add [0 0] vertices)
117+
(matrix/div (count vertices)))))
113118

114119
(defmethod element.hierarchy/snapping-points ::element.hierarchy/polyshape
115-
[{{:keys [points]} :attrs}]
116-
(points->px points))
120+
[el]
121+
(->vertices el))

src/renderer/element/impl/text.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
(defmethod element.hierarchy/scale :text
4444
[el ratio pivot-point]
45-
(let [offset (matrix/sub pivot-point (matrix/mul pivot-point ratio))
45+
(let [offset (utils.element/scale-offset ratio pivot-point)
4646
ratio (apply min ratio)]
4747
(-> el
4848
(attr.hierarchy/update-attr :font-size * ratio)

src/renderer/tool/impl/base/edit.cljs

+6-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
db (history.handlers/reset-state db)
6161
el-id (:element clicked-element)
6262
handle-id (:id clicked-element)
63-
delta (cond-> (matrix/add (tool.handlers/pointer-delta db) (snap.handlers/nearest-delta db))
63+
delta (cond-> (matrix/add (tool.handlers/pointer-delta db)
64+
(snap.handlers/nearest-delta db))
6465
(utils.pointer/ctrl? e)
6566
(utils.pointer/lock-direction))]
6667
(cond-> db
@@ -86,8 +87,10 @@
8687
(defmethod tool.hierarchy/snapping-elements :edit
8788
[db]
8889
(let [non-selected-ids (element.handlers/non-selected-ids db)
89-
non-selected (select-keys (element.handlers/entities db) (vec non-selected-ids))]
90-
(filter :visible (vals non-selected))))
90+
non-selected (select-keys (element.handlers/entities db)
91+
(vec non-selected-ids))]
92+
(->> (vals non-selected)
93+
(filter :visible))))
9194

9295
(defmethod tool.hierarchy/render :edit
9396
[]

src/renderer/utils/element.cljs

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
[el]
5353
(let [el-bbox (:bbox el)
5454
local-bbox (element.hierarchy/bbox el)]
55-
(vec (take 2 (matrix/sub el-bbox local-bbox)))))
55+
(->> (matrix/sub el-bbox local-bbox)
56+
(take 2)
57+
(vec))))
5658

5759
(m/=> snapping-points [:-> Element SnapOptions [:* Vec2]])
5860
(defn snapping-points
@@ -151,3 +153,10 @@
151153
[el f attrs-map]
152154
(reduce (fn [el [k & more]]
153155
(apply attribute.hierarchy/update-attr el k f more)) el attrs-map))
156+
157+
(m/=> scale-offset [:-> Vec2 Vec2 Vec2])
158+
(defn scale-offset
159+
[ratio pivot-point]
160+
(->> ratio
161+
(matrix/mul pivot-point)
162+
(matrix/sub pivot-point)))

0 commit comments

Comments
 (0)