Changes to snap to pixel

This commit is contained in:
alonso.torres 2023-01-17 23:20:54 +01:00
parent 0d96b5b798
commit c18d3c66a8
12 changed files with 75 additions and 57 deletions

View file

@ -274,12 +274,12 @@
(Point. (mth/precision (dm/get-prop pt :x) decimals)
(mth/precision (dm/get-prop pt :y) decimals))))
(defn half-round
(defn round-step
"Round the coordinates to the closest half-point"
[pt]
[pt step]
(assert (point? pt) "expected point instance")
(Point. (mth/half-round (dm/get-prop pt :x))
(mth/half-round (dm/get-prop pt :y))))
(Point. (mth/round (dm/get-prop pt :x) step)
(mth/round (dm/get-prop pt :y) step)))
(defn transform
"Transform a point applying a matrix transformation."

View file

@ -382,15 +382,24 @@
result))
(defn set-objects-modifiers
([modif-tree objects ignore-constraints snap-pixel?]
(set-objects-modifiers nil modif-tree objects ignore-constraints snap-pixel?))
([modif-tree objects]
(set-objects-modifiers modif-tree objects nil))
([old-modif-tree modif-tree objects ignore-constraints snap-pixel?]
([modif-tree objects params]
(set-objects-modifiers nil modif-tree objects params))
([old-modif-tree modif-tree objects
{:keys [ignore-constraints snap-pixel? snap-precision]
:or {ignore-constraints false snap-pixel? false snap-precision 1}}]
(let [objects (-> objects
(cond-> (some? old-modif-tree)
(apply-structure-modifiers old-modif-tree))
(apply-structure-modifiers modif-tree))
modif-tree
(cond-> modif-tree
snap-pixel? (gpp/adjust-pixel-precision objects snap-precision))
bounds (d/lazy-map (keys objects) #(dm/get-in objects [% :points]))
bounds (cond-> bounds
(some? old-modif-tree)
@ -417,11 +426,7 @@
modif-tree
(if old-modif-tree
(merge-modif-tree old-modif-tree modif-tree)
modif-tree)
modif-tree
(cond-> modif-tree
snap-pixel? (gpp/adjust-pixel-precision objects))]
modif-tree)]
;;#?(:cljs
;; (.log js/console ">result" (modif->js modif-tree objects)))

View file

@ -18,7 +18,7 @@
[app.common.types.modifiers :as ctm]))
(defn size-pixel-precision
[modifiers shape points]
[modifiers shape points precision]
(let [origin (gpo/origin points)
curr-width (gpo/width-points points)
curr-height (gpo/height-points points)
@ -29,8 +29,8 @@
vertical-line? (and path? (<= curr-width 0.01))
horizontal-line? (and path? (<= curr-height 0.01))
target-width (if vertical-line? curr-width (max 1 (mth/round curr-width)))
target-height (if horizontal-line? curr-height (max 1 (mth/round curr-height)))
target-width (if vertical-line? curr-width (max 1 (mth/round curr-width precision)))
target-height (if horizontal-line? curr-height (max 1 (mth/round curr-height precision)))
ratio-width (/ target-width curr-width)
ratio-height (/ target-height curr-height)
@ -39,23 +39,23 @@
(ctm/resize scalev origin transform transform-inverse {:precise? true}))))
(defn position-pixel-precision
[modifiers _ points]
[modifiers _ points precision]
(let [bounds (gpr/bounds->rect points)
corner (gpt/point bounds)
target-corner (gpt/round corner)
target-corner (gpt/round-step corner precision)
deltav (gpt/to-vec corner target-corner)]
(ctm/move modifiers deltav)))
(defn set-pixel-precision
"Adjust modifiers so they adjust to the pixel grid"
[modifiers shape]
[modifiers shape precision]
(let [points (-> shape :points (gco/transform-points (ctm/modifiers->transform modifiers)))
has-resize? (not (ctm/only-move? modifiers))
[modifiers points]
(let [modifiers
(cond-> modifiers
has-resize? (size-pixel-precision shape points))
has-resize? (size-pixel-precision shape points precision))
points
(if has-resize?
@ -63,16 +63,16 @@
(gco/transform-points (ctm/modifiers->transform modifiers)) )
points)]
[modifiers points])]
(position-pixel-precision modifiers shape points)))
(position-pixel-precision modifiers shape points precision)))
(defn adjust-pixel-precision
[modif-tree objects]
[modif-tree objects precision]
(let [update-modifiers
(fn [modif-tree shape]
(let [modifiers (dm/get-in modif-tree [(:id shape) :modifiers])]
(cond-> modif-tree
(ctm/has-geometry? modifiers)
(update-in [(:id shape) :modifiers] set-pixel-precision shape))))]
(update-in [(:id shape) :modifiers] set-pixel-precision shape precision))))]
(->> (keys modif-tree)
(map (d/getf objects))

View file

@ -104,15 +104,16 @@
(defn round
"Returns the value of a number rounded to
the nearest integer."
[v]
#?(:cljs (js/Math.round v)
:clj (Math/round (float v))))
the nearest integer.
If given step rounds to the next closest step, for example:
(round 13.4 0.5) => 13.5
(round 13.4 0.3) => 13.3"
([v step]
(* (round (/ v step)) step))
(defn half-round
"Returns a value rounded to the next point or half point"
[v]
(/ (round (* v 2)) 2))
([v]
#?(:cljs (js/Math.round v)
:clj (Math/round (float v)))))
(defn ceil
"Returns the smallest integer greater than

View file

@ -203,7 +203,7 @@
(t/deftest halft-round-point
(let [p1 (gpt/point 1.34567 3.34567)
rs (gpt/half-round p1)]
rs (gpt/round-step p1 0.5)]
(t/is (gpt/point? rs))
(t/is (mth/close? 1.5 (:x rs)))
(t/is (mth/close? 3.5 (:y rs)))))