diff --git a/common/src/app/common/geom/point.cljc b/common/src/app/common/geom/point.cljc index b93b31a91..f79743f7a 100644 --- a/common/src/app/common/geom/point.cljc +++ b/common/src/app/common/geom/point.cljc @@ -192,7 +192,7 @@ (if (>= y 0) 2 3))) (defn round - "Change the precision of the point coordinates." + "Round the coordinates of the point to a precision" ([point] (round point 0)) @@ -202,6 +202,13 @@ (Point. (mth/precision x decimals) (mth/precision y decimals)))) +(defn half-round + "Round the coordinates to the closest half-point" + [{:keys [x y] :as p}] + (assert (point? p)) + (Point. (mth/half-round x) + (mth/half-round y))) + (defn transform "Transform a point applying a matrix transformation." [{:keys [x y] :as p} {:keys [a b c d e f]}] diff --git a/frontend/src/app/main/data/workspace/path/streams.cljs b/frontend/src/app/main/data/workspace/path/streams.cljs index 8a8f8a59b..5fded7dd3 100644 --- a/frontend/src/app/main/data/workspace/path/streams.cljs +++ b/frontend/src/app/main/data/workspace/path/streams.cljs @@ -18,6 +18,7 @@ [potok.core :as ptk])) (defonce drag-threshold 5) +(def zoom-half-pixel-precision 8) (defn dragging? [start zoom] (fn [current] @@ -26,19 +27,38 @@ (defn finish-edition? [event] (= (ptk/type event) :app.main.data.workspace.common/clear-edition-mode)) +(defn to-pixel-snap [position] + (let [zoom (get-in @st/state [:workspace-local :zoom] 1) + layout (get @st/state :workspace-layout) + snap-pixel? (contains? layout :snap-pixel-grid)] + + (cond + (or (not snap-pixel?) (not (gpt/point? position))) + position + + (>= zoom zoom-half-pixel-precision) + (gpt/half-round position) + + :else + (gpt/round position)))) + (defn drag-stream ([to-stream] (drag-stream to-stream (rx/empty))) ([to-stream not-drag-stream] - (let [start @ms/mouse-position + (let [ zoom (get-in @st/state [:workspace-local :zoom] 1) - mouse-up (->> st/stream (rx/filter #(or (finish-edition? %) - (ms/mouse-up? %)))) + + start (-> @ms/mouse-position to-pixel-snap) + mouse-up (->> st/stream + (rx/filter #(or (finish-edition? %) + (ms/mouse-up? %)))) position-stream (->> ms/mouse-position (rx/take-until mouse-up) + (rx/map to-pixel-snap) (rx/filter (dragging? start zoom)) (rx/take 1))] @@ -53,10 +73,6 @@ (->> position-stream (rx/merge-map (fn [] to-stream))))))) -(defn to-dec [num] - (let [k 50] - (* (mth/floor (/ num k)) k))) - (defn move-points-stream [snap-toggled start-point selected-points points] @@ -73,6 +89,7 @@ (gpt/add position snap)) position))] (->> ms/mouse-position + (rx/map to-pixel-snap) (rx/map check-path-snap)))) (defn get-angle [node handler opposite] @@ -116,6 +133,7 @@ (merge position (gpt/add position snap))))) position))] (->> ms/mouse-position + (rx/map to-pixel-snap) (rx/with-latest merge (->> ms/mouse-position-shift (rx/map #(hash-map :shift? %)))) (rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %)))) (rx/map check-path-snap)))) @@ -136,6 +154,7 @@ (rx/map snap/create-ranges))] (->> ms/mouse-position + (rx/map to-pixel-snap) (rx/with-latest vector ranges-stream) (rx/map (fn [[position ranges]] (if snap-toggled diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index b5431cdca..b38f90773 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -259,9 +259,11 @@ (gsh/points->rect)) target-p (gpt/round (gpt/point raw-bounds)) + target-width (max 1 (mth/round (:width raw-bounds))) + target-height (max 1 (mth/round (:height raw-bounds))) - ratio-width (/ (mth/round (:width raw-bounds)) (:width raw-bounds)) - ratio-height (/ (mth/round (:height raw-bounds)) (:height raw-bounds)) + ratio-width (/ target-width (:width raw-bounds)) + ratio-height (/ target-height (:height raw-bounds)) modifiers (-> modifiers