Make precision closest point computation depend on zoom

This commit is contained in:
Miguel de Benito Delgado 2025-05-21 21:29:40 +02:00
parent a2ac2bc6c6
commit e4f2dfaa11
2 changed files with 8 additions and 9 deletions

View file

@ -182,11 +182,11 @@
;; FIXME: move to helpers?, this function need performance review, it
;; is executed so many times on path edition
(defn- curve-closest-point
[position start end h1 h2]
[position start end h1 h2 precision]
(let [d (memoize (fn [t] (gpt/distance position (helpers/curve-values start end h1 h2 t))))]
(loop [t1 0.0
t2 1.0]
(if (<= (mth/abs (- t1 t2)) path-closest-point-accuracy)
(if (<= (mth/abs (- t1 t2)) precision)
(-> (helpers/curve-values start end h1 h2 t1)
;; store the segment info
(with-meta {:t t1 :from-p start :to-p end}))
@ -214,7 +214,7 @@
(double t2)))))))
(defn- line-closest-point
"Point on line"
"Finds the closest point in the line segment defined by from-p and to-p"
[position from-p to-p]
(let [e1 (gpt/to-vec from-p to-p)
@ -274,9 +274,8 @@
(defn closest-point
"Given a path and a position"
[content position]
"Returns the closest point in the path to the position, at a given precision"
[content position precision]
(let [point+distance
(fn [[cur-segment prev-segment]]
(let [from-p (helpers/segment->point prev-segment)
@ -291,7 +290,7 @@
(line-closest-point position from-p to-p)
:curve-to
(curve-closest-point position from-p to-p h1 h2)
(curve-closest-point position from-p to-p h1 h2 precision)
nil)]
(when point

View file

@ -348,7 +348,7 @@
ms/mouse-position
(mf/deps base-content zoom)
(fn [position]
(when-let [point (path.segment/closest-point base-content position)]
(when-let [point (path.segment/closest-point base-content position (/ 0.01 zoom))]
(reset! hover-point (when (< (gpt/distance position point) (/ 10 zoom)) point)))))
[:g.path-editor {:ref editor-ref}