0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 23:49:45 -05:00

Remove old matrix/svg transformation code.

This commit is contained in:
Andrey Antukh 2016-02-06 18:02:46 +02:00
parent 6d89c573a6
commit 8d82c59453
4 changed files with 38 additions and 155 deletions

View file

@ -296,12 +296,27 @@
scale-y (/ height orig-height)
center-x (- width (/ width 2))
center-y (- height (/ height 2))]
(as-> (gmt/matrix) $
(gmt/translate $ x1 y1)
(gmt/translate $ center-x center-y)
(gmt/rotate $ rotation)
(gmt/translate $ (- center-x) (- center-y))
(gmt/scale $ scale-x scale-y))))
(-> (gmt/matrix)
(gmt/translate x1 y1)
(gmt/translate center-x center-y)
(gmt/rotate rotation)
(gmt/translate (- center-x) (- center-y))
(gmt/scale scale-x scale-y))))
(declare outer-rect)
(defmethod -transformation :builtin/group
[{:keys [dx dy rotation items] :or {rotation 0} :as shape}]
(let [shapes-by-id (get @st/state :shapes-by-id)
shapes (map #(get shapes-by-id %) items)
{:keys [x y width height]} (outer-rect shapes)
center-x (+ x (/ width 2))
center-y (+ y (/ height 2))]
(-> (gmt/matrix)
(gmt/translate (or dx 0) (or dy 0))
(gmt/translate center-x center-y)
(gmt/rotate rotation)
(gmt/translate (- center-x) (- center-y)))))
(defmethod -transformation :default
[shape _]

View file

@ -6,8 +6,6 @@
[uxbox.state :as st]
[uxbox.shapes :as sh]
[uxbox.ui.icons :as i]
[uxbox.util.svg :as svg]
[uxbox.util.matrix :as mtx]
[uxbox.util.math :as mth]
[uxbox.util.data :refer (remove-nil-vals)]))
@ -82,50 +80,29 @@
(defmethod sh/-render :builtin/rect
[{:keys [id x1 y1 x2 y2] :as shape}]
(let [key (str id)
props (select-keys shape [:x :y :width :height])
props {:x x1 :y y1 :id key :key key}
size (sh/-size shape)
attrs (-> (extract-style-attrs shape)
(merge {:id key :key key})
(merge props))]
(merge props size))]
(html
[:rect attrs])))
;; FIXME: the impl should be more clear.
(defmethod sh/-render :builtin/group
[{:keys [items id dx dy rotation] :as shape} factory]
(letfn [(rotation-matrix []
(let [shapes-by-id (get @st/state :shapes-by-id)
shapes (map #(get shapes-by-id %) items)
{:keys [x y width height]} (sh/outer-rect shapes)
center-x (+ x (/ width 2))
center-y (+ y (/ height 2))]
(mtx/multiply (svg/translate-matrix center-x center-y)
(svg/rotation-matrix rotation)
(svg/translate-matrix (- center-x)
(- center-y)))))
(translate-matrix []
(svg/translate-matrix (or dx 0) (or dy 0)))
(transform []
(let [result (mtx/multiply (rotation-matrix)
(translate-matrix))
result (flatten @result)]
(->> (map #(nth result %) [0 3 1 4 2 5])
(str/join ",")
(str/format "matrix(%s)"))))]
(let [key (str "group-" id)
tfm (transform)
attrs (merge {:id key :key key :transform tfm}
(make-debug-attrs shape))
shapes-by-id (get @st/state :shapes-by-id)]
(html
[:g attrs
(for [item (->> items
(map #(get shapes-by-id %))
(remove :hidden)
(reverse))]
(-> (factory item)
(rum/with-key (str (:id item)))))]))))
(let [key (str "group-" id)
rfm (sh/-transformation shape)
attrs (merge {:id key :key key :transform (str rfm)}
(extract-style-attrs shape)
(make-debug-attrs shape))
shapes-by-id (get @st/state :shapes-by-id)]
(html
[:g attrs
(for [item (->> items
(map #(get shapes-by-id %))
(remove :hidden)
(reverse))]
(-> (factory item)
(rum/with-key (str (:id item)))))])))
(defmethod sh/-render-svg :builtin/icon
[{:keys [data id view-box] :as shape}]

View file

@ -1,38 +0,0 @@
(ns uxbox.util.matrix
"A lightweight abstraction over Matrix library
of the Google Closure Library."
(:import goog.math.Matrix
goog.math.Size))
(extend-type Matrix
cljs.core/ICounted
(-count [v]
(let [^Size size (.getSize v)]
(* (.-width size)
(.-height size))))
cljs.core/IDeref
(-deref [v]
(js->clj (.toArray v)))
IPrintWithWriter
(-pr-writer [v writer _]
(->> (str "#goog.math.Matrix " (js->clj (.toArray v)))
(cljs.core/-write writer))))
(defn matrix
"Create a matrix instance from coll.
The size is determined by the number
of elements of the collection."
[coll]
{:pre [(coll? coll)
(coll? (first coll))]}
(Matrix. (clj->js coll)))
(defn multiply
([n] n)
([n m]
(.multiply n m))
([n m & more]
(reduce multiply (.multiply n m) more)))

View file

@ -1,71 +0,0 @@
(ns uxbox.util.svg
"A svg related utils."
(:require [cuerdas.core :as str]
[uxbox.util.matrix :as mtx]
[uxbox.util.math :as mth]
[uxbox.util.data :refer (without-keys)]))
(defn translate-matrix
([x]
(translate-matrix x x))
([x y]
(mtx/matrix [[1 0 x]
[0 1 y]
[0 0 1]])))
(defn scale-matrix
([w]
(scale-matrix w w))
([w h]
(mtx/matrix [[w 0 0]
[0 h 0]
[0 0 1]])))
(defn rotation-matrix
[^number degrees]
(let [v1 (mth/cos (mth/radians degrees))
v2 (mth/sin (mth/radians degrees))
v3 (mth/neg v2)]
(mtx/matrix [[v1 v3 0]
[v2 v1 0]
[0 0 1]])))
(defn apply-translate
[{:keys [x y]}]
(translate-matrix x y))
(defn apply-scale
[{:keys [width height view-box]}]
(if (and width height)
(let [orig-width (nth view-box 2)
orig-height (nth view-box 3)
scale-x (/ width orig-width)
scale-y (/ height orig-height)]
(scale-matrix scale-x scale-y))
(scale-matrix 1 1)))
(defn apply-rotation
[{:keys [rotation x y width height view-box] :or {rotation 0}}]
(let [width (nth view-box 2)
height (nth view-box 3)
x (- width (/ width 2))
y (- height (/ height 2))]
(mtx/multiply (translate-matrix x y)
(rotation-matrix rotation)
(translate-matrix (- x)
(- y)))))
(def interpret-attrs (juxt apply-translate
apply-scale
apply-rotation))
(defn calculate-transform
[shape]
(let [result (->> (interpret-attrs shape)
(apply mtx/multiply)
(deref)
(take 6)
(flatten))]
(->> (map #(nth result %) [0 3 1 4 2 5])
(str/join ",")
(str/format "matrix(%s)"))))