From 1a2a90f829e42e6c33021e65ff8930f5aa8e7878 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Wed, 1 Feb 2023 12:15:43 +0100 Subject: [PATCH] :bug: Fix problems with strange file --- common/src/app/common/geom/shapes.cljc | 10 ++--- .../app/common/geom/shapes/constraints.cljc | 6 +-- common/src/app/common/math.cljc | 2 +- .../shapes/frame/thumbnail_render.cljs | 42 ++++++++++--------- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/common/src/app/common/geom/shapes.cljc b/common/src/app/common/geom/shapes.cljc index be95e3416..5158ed32e 100644 --- a/common/src/app/common/geom/shapes.cljc +++ b/common/src/app/common/geom/shapes.cljc @@ -43,13 +43,13 @@ (defn bounding-box "Returns a rect that wraps the shape after all transformations applied." [shape] - ; TODO: perhaps we need to store this calculation in a shape attribute + ;; TODO: perhaps we need to store this calculation in a shape attribute (gpr/points->rect (:points shape))) (defn left-bound "Returns the lowest x coord of the shape BEFORE applying transformations." - ; TODO: perhaps some day we want after transformations, but for the - ; moment it's enough as is now. + ;; TODO: perhaps some day we want after transformations, but for the + ;; moment it's enough as is now. [shape] (or (:x shape) (:x (:selrect shape)))) ; Paths don't have :x attribute @@ -106,8 +106,8 @@ ([attr val1 val2 precision] (let [close-val? (fn [num1 num2] - (when (and (number? num1) (number? num2)) - (< (mth/abs (- num1 num2)) precision)))] + (when (and (number? num1) (number? num2)) + (< (mth/abs (- num1 num2)) precision)))] (cond (and (number? val1) (number? val2)) (close-val? val1 val2) diff --git a/common/src/app/common/geom/shapes/constraints.cljc b/common/src/app/common/geom/shapes/constraints.cljc index 6cdf6c5d9..6c4f24cc3 100644 --- a/common/src/app/common/geom/shapes/constraints.cljc +++ b/common/src/app/common/geom/shapes/constraints.cljc @@ -210,7 +210,7 @@ ;; after-vec will contain the side length of the grown side ;; we scale the shape by the diference and translate it by the start ;; displacement (so its left+top position is constant) - scale (/ (gpt/length after-vec) (gpt/length before-vec)) + scale (/ (gpt/length after-vec) (max 0.01 (gpt/length before-vec))) resize-origin (gpo/origin child-points-after) @@ -268,11 +268,11 @@ scale-x (if (= :scale constraints-h) 1 - (/ (gpo/width-points child-bb-before) (gpo/width-points child-bb-after))) + (/ (gpo/width-points child-bb-before) (max 0.01 (gpo/width-points child-bb-after)))) scale-y (if (= :scale constraints-v) 1 - (/ (gpo/height-points child-bb-before) (gpo/height-points child-bb-after))) + (/ (gpo/height-points child-bb-before) (max 0.01 (gpo/height-points child-bb-after)))) resize-vector (gpt/point scale-x scale-y) resize-origin (gpo/origin transformed-child-bounds) diff --git a/common/src/app/common/math.cljc b/common/src/app/common/math.cljc index 7760a974c..d3f724ee9 100644 --- a/common/src/app/common/math.cljc +++ b/common/src/app/common/math.cljc @@ -34,7 +34,7 @@ (defn abs [v] #?(:cljs (js/Math.abs v) - :clj (Math/abs v))) + :clj (Math/abs (double v)))) (defn sin "Returns the sine of a number" diff --git a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs index 0900b8b75..c8a1a2c3d 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs @@ -27,23 +27,21 @@ (defn- draw-thumbnail-canvas! [canvas-node img-node] - (ts/raf - (fn [] - (try - (when (and (some? canvas-node) (some? img-node)) - (let [canvas-context (.getContext canvas-node "2d") - canvas-width (.-width canvas-node) - canvas-height (.-height canvas-node)] - (.clearRect canvas-context 0 0 canvas-width canvas-height) - (.drawImage canvas-context img-node 0 0 canvas-width canvas-height) + (try + (when (and (some? canvas-node) (some? img-node)) + (let [canvas-context (.getContext canvas-node "2d") + canvas-width (.-width canvas-node) + canvas-height (.-height canvas-node)] + (.clearRect canvas-context 0 0 canvas-width canvas-height) + (.drawImage canvas-context img-node 0 0 canvas-width canvas-height) - ;; Set a true on the next animation frame, we make sure the drawImage is completed - (ts/raf - #(dom/set-data! canvas-node "ready" "true")) - true)) - (catch :default err - (.error js/console err) - false))))) + ;; Set a true on the next animation frame, we make sure the drawImage is completed + (ts/raf + #(dom/set-data! canvas-node "ready" "true")) + true)) + (catch :default err + (.error js/console err) + false))) (defn- remove-image-loading "Remove the changes related to change a url for its embed value. This is necessary @@ -78,8 +76,12 @@ (gsh/selection-rect (concat [shape] all-children)) (-> shape :points gsh/points->selrect)) - fixed-width (mth/clamp width 250 2000) - fixed-height (/ (* height fixed-width) width) + [fixed-width fixed-height] + (if (> width height) + [(mth/clamp width 250 2000) + (/ (* height (mth/clamp width 250 2000)) width)] + [(/ (* width (mth/clamp height 250 2000)) height) + (mth/clamp height 250 2000)]) image-url (mf/use-state nil) observer-ref (mf/use-var nil) @@ -289,6 +291,6 @@ :height height} [:img {:ref frame-image-ref :src @image-url - :width width - :height height + :width fixed-width + :height fixed-height :on-load on-image-load}]])])]))