Clojure makes it easy to generate and parse XML. SVG is a form of XML. And therefore clojure is good for doing vector graphics drawings.
|
This year's Turner Prize Winner |
(defn make-rect [i j squaresize]
{:tag :rect
:attrs {:x (str (* squaresize i))
:y (str (* squaresize j))
:width (str squaresize)
:height (str squaresize)
:style "fill:white;stroke:black;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"}})
(defn make-svg [gridsize squaresize]
{:tag :svg
:attrs {:width "100%"
:height "100%"
:version "1.1"
:xmlns "http://www.w3.org/2000/svg"}
:content (for [i (range gridsize) j (range gridsize)] (make-rect i j squaresize))})
(require 'clojure.contrib.lazy-xml)
(spit "squares.svg" (with-out-str (clojure.contrib.lazy-xml/emit (make-svg 10 80))))
Hi thanks for the great post, I'll be building on this for a svg charting lib.
ReplyDeleteAnyway, i just noticed this morning that there's an optional arg for emit called "indent" that takes a num for the spaces to indent. As an example, try this for more nicely formatted xml:
(spit "squares.svg" (with-out-str (clojure.contrib.lazy-xml/emit (make-svg 10 80) :indent 0)))