Clojurescript中的SVG
在Clojurescript中向dom添加SVG元素的最佳方法是什么?Twitterbuzz示例使用Google Closure lib的图形对象,但如果我不关心IE兼容性,有没有办法像html那样添加元素Clojurescript中的SVG,svg,clojurescript,Svg,Clojurescript,在Clojurescript中向dom添加SVG元素的最佳方法是什么?Twitterbuzz示例使用Google Closure lib的图形对象,但如果我不关心IE兼容性,有没有办法像html那样添加元素(dom/build[:svg[:g[:text“类似于此?”]]) 我尝试了Closure的图形库,但没有使用很多方便的SVG特性。我尝试了Apogee,但是它序列化了SVG代码,我更喜欢直接的DOM操作。DOM/build构建没有名称空间的DOM元素。您需要增强dom/build,或者为S
(dom/build[:svg[:g[:text“类似于此?”]])
我尝试了Closure的图形库,但没有使用很多方便的SVG特性。我尝试了Apogee,但是它序列化了SVG代码,我更喜欢直接的DOM操作。DOM/build构建没有名称空间的DOM元素。您需要增强dom/build,或者为SVG编写一个。我使用以下代码创建SVG元素
(defn element-ns
"Create a namespaced dom element"
[namespace tag & args]
(let [[tag attrs children] (normalize-args tag args)
parent (. js/document createElementNS namespace (name tag))
[parent children] (if (string? (first children))
[(set-text (element-ns namespace tag attrs) (first children))
(rest children)]
[parent children])]
(doseq [[k v] attrs]
(. parent setAttributeNS nil (name k) v))
(apply append parent children)))
(defn build-svg
"Build up a svg element from nested vectors."
[x]
(if (vector? x)
(let [[parent children] (if (keyword? (first x))
[(apply element-ns "http://www.w3.org/2000/svg" (take-while element-arg? x))
(drop-while element-arg? x)]
[(first x) (rest x)])
children (map build-svg children)]
(apply append parent children))
x))
您可能对my library感兴趣,它是D3 JavaScript库的ClojureScript包装器。
D3是操纵SVG DOM的极好工具。如何使用SVG?这就是我计划用于几个实时项目的内容。如果要将svg作为外部资源加载,请尝试将其作为文本加载,并将其附加到利用cljs-d3的div中:
(defn load-svg-as-text []
(fn []
(let [on-complete (fn [error externalSVGText]
(-> d3
(.select "#viewport")
(.html externalSVGText))
)]
(-> d3
(.text "images/smartcity.svg" on-complete)))))
我从这篇文章的尼斯答案中得到了这个想法,然后将它移植到ClojureScript,因为有一点,Enlive是一个HTML模板库,它适合在服务器上生成HTML,而我在运行时在客户机上生成SVG。另外,我认为它不支持SVGAhh所需的XML名称空间,我明白了,很抱歉,请看一下ClojureScript land的“Enfocus”。我将从客户端和服务器生成svg,
元素arg?
规范化args
,设置文本
,以及追加
在哪里定义?