From 8b57dcf015b89bac50f8fcb01d7ce48ae567adde Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 5 Jan 2024 12:38:04 +0100 Subject: [PATCH] :zap: Optimize dasharray generation --- frontend/src/app/main/ui/shapes/attrs.cljs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/frontend/src/app/main/ui/shapes/attrs.cljs b/frontend/src/app/main/ui/shapes/attrs.cljs index 1db0a15bc..707d40719 100644 --- a/frontend/src/app/main/ui/shapes/attrs.cljs +++ b/frontend/src/app/main/ui/shapes/attrs.cljs @@ -17,16 +17,16 @@ [app.util.object :as obj] [cuerdas.core :as str])) -(defn- stroke-type->dasharray - [width style] - (let [values (case style - :mixed [5 5 1 5] - ;; We want 0 so they are circles - :dotted [(- width) 5] - :dashed [10 10] - nil)] - - (->> values (map #(+ % width)) (str/join ",")))) +(defn- calculate-dasharray + [style width] + (let [w+5 (+ 5 width) + w+1 (+ 1 width) + w+10 (+ 10 width)] + (case style + :mixed (str/concat "" w+5 "," w+5 "," w+1 "," w+5) + :dotted (str/concat "" (- (* width 2)) "," w+5) + :dashed (str/concat "" w+10 "," w+10) + ""))) (defn get-border-props [shape] @@ -111,7 +111,7 @@ (obj/set! attrs "strokeOpacity" opacity))) (when (not= style :svg) - (obj/set! attrs "strokeDasharray" (stroke-type->dasharray width style))) + (obj/set! attrs "strokeDasharray" (calculate-dasharray style width))) ;; For simple line caps we use svg stroke-line-cap attribute. This ;; only works if all caps are the same and we are not using the tricks