0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-13 02:28:18 -05:00

Pixel/half-pixel on path drawing

This commit is contained in:
alonso.torres 2022-03-11 12:39:08 +01:00
parent e5206e65e7
commit 099d1259b2
3 changed files with 38 additions and 10 deletions

View file

@ -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]}]

View file

@ -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

View file

@ -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