mirror of
https://github.com/penpot/penpot.git
synced 2025-05-23 17:36:10 +02:00
⚡ Improve move shapes performance
This commit is contained in:
parent
c62905b9a8
commit
ac27d35ff5
4 changed files with 106 additions and 78 deletions
|
@ -17,28 +17,6 @@
|
||||||
[app.common.geom.shapes.intersect :as gin]
|
[app.common.geom.shapes.intersect :as gin]
|
||||||
[app.common.spec :as us]))
|
[app.common.spec :as us]))
|
||||||
|
|
||||||
;; --- Relative Movement
|
|
||||||
|
|
||||||
(defn move
|
|
||||||
"Move the shape relativelly to its current
|
|
||||||
position applying the provided delta."
|
|
||||||
[shape {dx :x dy :y}]
|
|
||||||
(let [dx (d/check-num dx)
|
|
||||||
dy (d/check-num dy)]
|
|
||||||
(-> shape
|
|
||||||
(assoc-in [:modifiers :displacement] (gmt/translate-matrix (gpt/point dx dy)))
|
|
||||||
(gtr/transform-shape))))
|
|
||||||
|
|
||||||
;; --- Absolute Movement
|
|
||||||
|
|
||||||
(declare absolute-move-rect)
|
|
||||||
|
|
||||||
(defn absolute-move
|
|
||||||
"Move the shape to the exactly specified position."
|
|
||||||
[shape {:keys [x y]}]
|
|
||||||
(let [dx (- (d/check-num x) (-> shape :selrect :x))
|
|
||||||
dy (- (d/check-num y) (-> shape :selrect :y))]
|
|
||||||
(move shape (gpt/point dx dy))))
|
|
||||||
|
|
||||||
;; --- Resize (Dimensions)
|
;; --- Resize (Dimensions)
|
||||||
(defn resize-modifiers
|
(defn resize-modifiers
|
||||||
|
@ -120,38 +98,8 @@
|
||||||
(gpr/join-selrects)))
|
(gpr/join-selrects)))
|
||||||
|
|
||||||
(defn translate-to-frame
|
(defn translate-to-frame
|
||||||
[{:keys [type x y] :as shape} {:keys [x y]}]
|
[shape {:keys [x y]}]
|
||||||
(let [move-point
|
(gtr/move shape (gpt/negate (gpt/point x y))) )
|
||||||
(fn [point]
|
|
||||||
(-> point
|
|
||||||
(update :x - x)
|
|
||||||
(update :y - y)))
|
|
||||||
|
|
||||||
move-segment
|
|
||||||
(fn [segment]
|
|
||||||
(-> segment
|
|
||||||
(d/update-in-when [:params :x] - x)
|
|
||||||
(d/update-in-when [:params :y] - y)
|
|
||||||
(d/update-in-when [:params :c1x] - x)
|
|
||||||
(d/update-in-when [:params :c1y] - y)
|
|
||||||
(d/update-in-when [:params :c2x] - x)
|
|
||||||
(d/update-in-when [:params :c2y] - y)))]
|
|
||||||
|
|
||||||
(-> shape
|
|
||||||
(d/update-when :x - x)
|
|
||||||
(d/update-when :y - y)
|
|
||||||
(update-in [:selrect :x] - x)
|
|
||||||
(update-in [:selrect :y] - y)
|
|
||||||
(update-in [:selrect :x1] - x)
|
|
||||||
(update-in [:selrect :y1] - y)
|
|
||||||
(update-in [:selrect :x2] - x)
|
|
||||||
(update-in [:selrect :y2] - y)
|
|
||||||
|
|
||||||
(d/update-when :points #(mapv move-point %))
|
|
||||||
|
|
||||||
(cond-> (= :path type)
|
|
||||||
(d/update-when :content #(mapv move-segment %))))))
|
|
||||||
|
|
||||||
|
|
||||||
;; --- Helpers
|
;; --- Helpers
|
||||||
|
|
||||||
|
@ -244,6 +192,8 @@
|
||||||
(d/export gtr/update-group-selrect)
|
(d/export gtr/update-group-selrect)
|
||||||
(d/export gtr/transform-points)
|
(d/export gtr/transform-points)
|
||||||
(d/export gtr/calculate-adjust-matrix)
|
(d/export gtr/calculate-adjust-matrix)
|
||||||
|
(d/export gtr/move)
|
||||||
|
(d/export gtr/absolute-move)
|
||||||
|
|
||||||
;; PATHS
|
;; PATHS
|
||||||
(d/export gsp/content->points)
|
(d/export gsp/content->points)
|
||||||
|
|
|
@ -139,6 +139,23 @@
|
||||||
(update :width #(if (mth/almost-zero? %) 1 %))
|
(update :width #(if (mth/almost-zero? %) 1 %))
|
||||||
(update :height #(if (mth/almost-zero? %) 1 %)))))
|
(update :height #(if (mth/almost-zero? %) 1 %)))))
|
||||||
|
|
||||||
|
(defn move-content [content move-vec]
|
||||||
|
(let [set-tr (fn [params px py]
|
||||||
|
(let [tr-point (-> (gpt/point (get params px) (get params py))
|
||||||
|
(gpt/add move-vec))]
|
||||||
|
(assoc params
|
||||||
|
px (:x tr-point)
|
||||||
|
py (:y tr-point))))
|
||||||
|
|
||||||
|
transform-params
|
||||||
|
(fn [{:keys [x c1x c2x] :as params}]
|
||||||
|
(cond-> params
|
||||||
|
(not (nil? x)) (set-tr :x :y)
|
||||||
|
(not (nil? c1x)) (set-tr :c1x :c1y)
|
||||||
|
(not (nil? c2x)) (set-tr :c2x :c2y)))]
|
||||||
|
|
||||||
|
(mapv #(update % :params transform-params) content)))
|
||||||
|
|
||||||
(defn transform-content [content transform]
|
(defn transform-content [content transform]
|
||||||
(let [set-tr (fn [params px py]
|
(let [set-tr (fn [params px py]
|
||||||
(let [tr-point (-> (gpt/point (get params px) (get params py))
|
(let [tr-point (-> (gpt/point (get params px) (get params py))
|
||||||
|
|
|
@ -14,6 +14,49 @@
|
||||||
[app.common.math :as mth]
|
[app.common.math :as mth]
|
||||||
[app.common.data :as d]))
|
[app.common.data :as d]))
|
||||||
|
|
||||||
|
;; --- Relative Movement
|
||||||
|
|
||||||
|
(defn move-selrect [selrect {dx :x dy :y}]
|
||||||
|
(-> selrect
|
||||||
|
(d/update-when :x + dx)
|
||||||
|
(d/update-when :y + dy)
|
||||||
|
(d/update-when :x1 + dx)
|
||||||
|
(d/update-when :y1 + dy)
|
||||||
|
(d/update-when :x2 + dx)
|
||||||
|
(d/update-when :y2 + dy)))
|
||||||
|
|
||||||
|
(defn move-points [points move-vec]
|
||||||
|
(->> points
|
||||||
|
(mapv #(gpt/add % move-vec))))
|
||||||
|
|
||||||
|
(defn move
|
||||||
|
"Move the shape relativelly to its current
|
||||||
|
position applying the provided delta."
|
||||||
|
[shape {dx :x dy :y}]
|
||||||
|
(let [dx (d/check-num dx)
|
||||||
|
dy (d/check-num dy)
|
||||||
|
move-vec (gpt/point dx dy)]
|
||||||
|
|
||||||
|
(-> shape
|
||||||
|
(update :selrect move-selrect move-vec)
|
||||||
|
(update :points move-points move-vec)
|
||||||
|
(d/update-when :x + dx)
|
||||||
|
(d/update-when :y + dy)
|
||||||
|
(cond-> (= :path (:type shape))
|
||||||
|
(update :content gpa/move-content move-vec)))))
|
||||||
|
|
||||||
|
;; --- Absolute Movement
|
||||||
|
|
||||||
|
(declare absolute-move-rect)
|
||||||
|
|
||||||
|
(defn absolute-move
|
||||||
|
"Move the shape to the exactly specified position."
|
||||||
|
[shape {:keys [x y]}]
|
||||||
|
(let [dx (- (d/check-num x) (-> shape :selrect :x))
|
||||||
|
dy (- (d/check-num y) (-> shape :selrect :y))]
|
||||||
|
(move shape (gpt/point dx dy))))
|
||||||
|
|
||||||
|
|
||||||
(defn- modif-rotation [shape]
|
(defn- modif-rotation [shape]
|
||||||
(let [cur-rotation (d/check-num (:rotation shape))
|
(let [cur-rotation (d/check-num (:rotation shape))
|
||||||
delta-angle (d/check-num (get-in shape [:modifiers :rotation]))]
|
delta-angle (d/check-num (get-in shape [:modifiers :rotation]))]
|
||||||
|
@ -272,12 +315,27 @@
|
||||||
(and rx (< rx 0)) (update :flip-x not)
|
(and rx (< rx 0)) (update :flip-x not)
|
||||||
(and ry (< ry 0)) (update :flip-y not))))
|
(and ry (< ry 0)) (update :flip-y not))))
|
||||||
|
|
||||||
(defn transform-shape [shape]
|
(defn apply-displacement [shape]
|
||||||
(let [center (gco/center-shape shape)]
|
(let [modifiers (:modifiers shape)]
|
||||||
(if (and (:modifiers shape) center)
|
(if (contains? modifiers :displacement)
|
||||||
(let [transform (modifiers->transform center (:modifiers shape))]
|
(let [mov-vec (-> (gpt/point 0 0)
|
||||||
|
(gpt/transform (:displacement modifiers)))
|
||||||
|
shape (move shape mov-vec)
|
||||||
|
modifiers (dissoc modifiers :displacement)]
|
||||||
(-> shape
|
(-> shape
|
||||||
(set-flip (:modifiers shape))
|
(assoc :modifiers modifiers)
|
||||||
|
(cond-> (empty? modifiers)
|
||||||
|
(dissoc :modifiers))))
|
||||||
|
shape)))
|
||||||
|
|
||||||
|
(defn transform-shape [shape]
|
||||||
|
(let [shape (apply-displacement shape)
|
||||||
|
center (gco/center-shape shape)
|
||||||
|
modifiers (:modifiers shape)]
|
||||||
|
(if (and modifiers center)
|
||||||
|
(let [transform (modifiers->transform center modifiers)]
|
||||||
|
(-> shape
|
||||||
|
(set-flip modifiers)
|
||||||
(apply-transform transform)
|
(apply-transform transform)
|
||||||
(dissoc :modifiers)))
|
(dissoc :modifiers)))
|
||||||
shape)))
|
shape)))
|
||||||
|
|
|
@ -285,27 +285,30 @@
|
||||||
:objects objects
|
:objects objects
|
||||||
:key id}])))]]))
|
:key id}])))]]))
|
||||||
|
|
||||||
|
(defn- strip-obj-data [obj]
|
||||||
|
(select-keys obj [:id
|
||||||
|
:name
|
||||||
|
:blocked
|
||||||
|
:hidden
|
||||||
|
:shapes
|
||||||
|
:type
|
||||||
|
:content
|
||||||
|
:parent-id
|
||||||
|
:component-id
|
||||||
|
:component-file
|
||||||
|
:shape-ref
|
||||||
|
:touched
|
||||||
|
:metadata
|
||||||
|
:masked-group?]))
|
||||||
|
|
||||||
(defn- strip-objects
|
(defn- strip-objects
|
||||||
[objects]
|
[objects]
|
||||||
(let [strip-data #(select-keys % [:id
|
(persistent!
|
||||||
:name
|
(->> objects
|
||||||
:blocked
|
(reduce-kv
|
||||||
:hidden
|
(fn [res id obj]
|
||||||
:shapes
|
(assoc! res id (strip-obj-data obj)))
|
||||||
:type
|
(transient {})))))
|
||||||
:content
|
|
||||||
:parent-id
|
|
||||||
:component-id
|
|
||||||
:component-file
|
|
||||||
:shape-ref
|
|
||||||
:touched
|
|
||||||
:metadata
|
|
||||||
:masked-group?])]
|
|
||||||
(persistent!
|
|
||||||
(reduce-kv (fn [res id obj]
|
|
||||||
(assoc! res id (strip-data obj)))
|
|
||||||
(transient {})
|
|
||||||
objects))))
|
|
||||||
|
|
||||||
(mf/defc layers-tree-wrapper
|
(mf/defc layers-tree-wrapper
|
||||||
{::mf/wrap-props false
|
{::mf/wrap-props false
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue