mirror of
https://github.com/penpot/penpot.git
synced 2025-05-23 23:46:11 +02:00
✨ Pixel/half-pixel on path drawing
This commit is contained in:
parent
e5206e65e7
commit
099d1259b2
3 changed files with 38 additions and 10 deletions
|
@ -192,7 +192,7 @@
|
||||||
(if (>= y 0) 2 3)))
|
(if (>= y 0) 2 3)))
|
||||||
|
|
||||||
(defn round
|
(defn round
|
||||||
"Change the precision of the point coordinates."
|
"Round the coordinates of the point to a precision"
|
||||||
([point]
|
([point]
|
||||||
(round point 0))
|
(round point 0))
|
||||||
|
|
||||||
|
@ -202,6 +202,13 @@
|
||||||
(Point. (mth/precision x decimals)
|
(Point. (mth/precision x decimals)
|
||||||
(mth/precision y 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
|
(defn transform
|
||||||
"Transform a point applying a matrix transformation."
|
"Transform a point applying a matrix transformation."
|
||||||
[{:keys [x y] :as p} {:keys [a b c d e f]}]
|
[{:keys [x y] :as p} {:keys [a b c d e f]}]
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
[potok.core :as ptk]))
|
[potok.core :as ptk]))
|
||||||
|
|
||||||
(defonce drag-threshold 5)
|
(defonce drag-threshold 5)
|
||||||
|
(def zoom-half-pixel-precision 8)
|
||||||
|
|
||||||
(defn dragging? [start zoom]
|
(defn dragging? [start zoom]
|
||||||
(fn [current]
|
(fn [current]
|
||||||
|
@ -26,19 +27,38 @@
|
||||||
(defn finish-edition? [event]
|
(defn finish-edition? [event]
|
||||||
(= (ptk/type event) :app.main.data.workspace.common/clear-edition-mode))
|
(= (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
|
(defn drag-stream
|
||||||
([to-stream]
|
([to-stream]
|
||||||
(drag-stream to-stream (rx/empty)))
|
(drag-stream to-stream (rx/empty)))
|
||||||
|
|
||||||
([to-stream not-drag-stream]
|
([to-stream not-drag-stream]
|
||||||
(let [start @ms/mouse-position
|
(let [
|
||||||
zoom (get-in @st/state [:workspace-local :zoom] 1)
|
zoom (get-in @st/state [:workspace-local :zoom] 1)
|
||||||
mouse-up (->> st/stream (rx/filter #(or (finish-edition? %)
|
|
||||||
|
start (-> @ms/mouse-position to-pixel-snap)
|
||||||
|
mouse-up (->> st/stream
|
||||||
|
(rx/filter #(or (finish-edition? %)
|
||||||
(ms/mouse-up? %))))
|
(ms/mouse-up? %))))
|
||||||
|
|
||||||
position-stream
|
position-stream
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
(rx/take-until mouse-up)
|
(rx/take-until mouse-up)
|
||||||
|
(rx/map to-pixel-snap)
|
||||||
(rx/filter (dragging? start zoom))
|
(rx/filter (dragging? start zoom))
|
||||||
(rx/take 1))]
|
(rx/take 1))]
|
||||||
|
|
||||||
|
@ -53,10 +73,6 @@
|
||||||
(->> position-stream
|
(->> position-stream
|
||||||
(rx/merge-map (fn [] to-stream)))))))
|
(rx/merge-map (fn [] to-stream)))))))
|
||||||
|
|
||||||
(defn to-dec [num]
|
|
||||||
(let [k 50]
|
|
||||||
(* (mth/floor (/ num k)) k)))
|
|
||||||
|
|
||||||
(defn move-points-stream
|
(defn move-points-stream
|
||||||
[snap-toggled start-point selected-points points]
|
[snap-toggled start-point selected-points points]
|
||||||
|
|
||||||
|
@ -73,6 +89,7 @@
|
||||||
(gpt/add position snap))
|
(gpt/add position snap))
|
||||||
position))]
|
position))]
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
|
(rx/map to-pixel-snap)
|
||||||
(rx/map check-path-snap))))
|
(rx/map check-path-snap))))
|
||||||
|
|
||||||
(defn get-angle [node handler opposite]
|
(defn get-angle [node handler opposite]
|
||||||
|
@ -116,6 +133,7 @@
|
||||||
(merge position (gpt/add position snap)))))
|
(merge position (gpt/add position snap)))))
|
||||||
position))]
|
position))]
|
||||||
(->> ms/mouse-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-shift (rx/map #(hash-map :shift? %))))
|
||||||
(rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %))))
|
(rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %))))
|
||||||
(rx/map check-path-snap))))
|
(rx/map check-path-snap))))
|
||||||
|
@ -136,6 +154,7 @@
|
||||||
(rx/map snap/create-ranges))]
|
(rx/map snap/create-ranges))]
|
||||||
|
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
|
(rx/map to-pixel-snap)
|
||||||
(rx/with-latest vector ranges-stream)
|
(rx/with-latest vector ranges-stream)
|
||||||
(rx/map (fn [[position ranges]]
|
(rx/map (fn [[position ranges]]
|
||||||
(if snap-toggled
|
(if snap-toggled
|
||||||
|
|
|
@ -259,9 +259,11 @@
|
||||||
(gsh/points->rect))
|
(gsh/points->rect))
|
||||||
|
|
||||||
target-p (gpt/round (gpt/point raw-bounds))
|
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-width (/ target-width (:width raw-bounds))
|
||||||
ratio-height (/ (mth/round (:height raw-bounds)) (:height raw-bounds))
|
ratio-height (/ target-height (:height raw-bounds))
|
||||||
|
|
||||||
modifiers
|
modifiers
|
||||||
(-> modifiers
|
(-> modifiers
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue