From ac27d35ff53b6f7023d6999c802d0fc3e5b97f58 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Wed, 28 Apr 2021 17:26:31 +0200 Subject: [PATCH] :zap: Improve move shapes performance --- common/app/common/geom/shapes.cljc | 58 ++-------------- common/app/common/geom/shapes/path.cljc | 17 +++++ common/app/common/geom/shapes/transforms.cljc | 68 +++++++++++++++++-- .../app/main/ui/workspace/sidebar/layers.cljs | 41 +++++------ 4 files changed, 106 insertions(+), 78 deletions(-) diff --git a/common/app/common/geom/shapes.cljc b/common/app/common/geom/shapes.cljc index 28e110b73..a93c20ad3 100644 --- a/common/app/common/geom/shapes.cljc +++ b/common/app/common/geom/shapes.cljc @@ -17,28 +17,6 @@ [app.common.geom.shapes.intersect :as gin] [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) (defn resize-modifiers @@ -120,38 +98,8 @@ (gpr/join-selrects))) (defn translate-to-frame - [{:keys [type x y] :as shape} {:keys [x y]}] - (let [move-point - (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 %)))))) - + [shape {:keys [x y]}] + (gtr/move shape (gpt/negate (gpt/point x y))) ) ;; --- Helpers @@ -244,6 +192,8 @@ (d/export gtr/update-group-selrect) (d/export gtr/transform-points) (d/export gtr/calculate-adjust-matrix) +(d/export gtr/move) +(d/export gtr/absolute-move) ;; PATHS (d/export gsp/content->points) diff --git a/common/app/common/geom/shapes/path.cljc b/common/app/common/geom/shapes/path.cljc index 33c041cee..0339cd652 100644 --- a/common/app/common/geom/shapes/path.cljc +++ b/common/app/common/geom/shapes/path.cljc @@ -139,6 +139,23 @@ (update :width #(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] (let [set-tr (fn [params px py] (let [tr-point (-> (gpt/point (get params px) (get params py)) diff --git a/common/app/common/geom/shapes/transforms.cljc b/common/app/common/geom/shapes/transforms.cljc index 78f78dc88..cf9845741 100644 --- a/common/app/common/geom/shapes/transforms.cljc +++ b/common/app/common/geom/shapes/transforms.cljc @@ -14,6 +14,49 @@ [app.common.math :as mth] [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] (let [cur-rotation (d/check-num (:rotation shape)) delta-angle (d/check-num (get-in shape [:modifiers :rotation]))] @@ -272,12 +315,27 @@ (and rx (< rx 0)) (update :flip-x not) (and ry (< ry 0)) (update :flip-y not)))) -(defn transform-shape [shape] - (let [center (gco/center-shape shape)] - (if (and (:modifiers shape) center) - (let [transform (modifiers->transform center (:modifiers shape))] +(defn apply-displacement [shape] + (let [modifiers (:modifiers shape)] + (if (contains? modifiers :displacement) + (let [mov-vec (-> (gpt/point 0 0) + (gpt/transform (:displacement modifiers))) + shape (move shape mov-vec) + modifiers (dissoc modifiers :displacement)] (-> 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) (dissoc :modifiers))) shape))) diff --git a/frontend/src/app/main/ui/workspace/sidebar/layers.cljs b/frontend/src/app/main/ui/workspace/sidebar/layers.cljs index 388279d48..a4744b6a4 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/layers.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/layers.cljs @@ -285,27 +285,30 @@ :objects objects :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 [objects] - (let [strip-data #(select-keys % [:id - :name - :blocked - :hidden - :shapes - :type - :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)))) + (persistent! + (->> objects + (reduce-kv + (fn [res id obj] + (assoc! res id (strip-obj-data obj))) + (transient {}))))) (mf/defc layers-tree-wrapper {::mf/wrap-props false