Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created November 28, 2012 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfredericks/4158758 to your computer and use it in GitHub Desktop.
Save gfredericks/4158758 to your computer and use it in GitHub Desktop.
turb
(ns diagrams.core
(:require [hiccup.core :as h]))
(let [prelude "<?xml version=\"1.0\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN \" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n"]
(defn svg-str
[contents]
(str prelude (h/html [:svg {:height 400 :width 400 :xmlns "http://www.w3.org/2000/svg"} contents]) "\n")))
(defn circle
[x y r & {:as attrs}]
[:circle (merge {:cx x :cy y :r r} attrs)])
(defn rect
[x y w h]
[:rect {:x x :y y :width w :height h}])
(defn line
[x1 y1 x2 y2 & {:as attrs}]
(let [[x1 y1 x2 y2] (map double [x1 y1 x2 y2])]
[:path (merge {:d (format "M%f,%fL%f,%f" x1 y1 x2 y2)} attrs)]))
(defn turbulate
[contents]
(let [id (str (gensym))]
[:g
[:filter {:id id}
[:feTurbulence {:type "fractalNoise" :baseFrequency "0.1" :result "noise"}]
[:feDisplacementMap {:scale "10" :xChannelSelector "R" :yChannelSelector "G" :in "SourceGraphic" :in2 "noise"}]]
[:g {:filter (format "url(#%s)" id)}
contents]]))
(defn run
[]
(spit "sample.svg"
(svg-str
(turbulate
[:g {:style "stroke:black;"}
[:g {:transform "scale(198,198) translate(1.01,1.01)"}
(circle 0 0 1 :fill "red" :stroke-width 0.03)
(for [i (range 30)]
[:g {:transform (format "rotate(%f)" (double (rand 360)#_(/ (* 360 i) 30)))}
[:path {:stroke-width 0 :fill "black" :d "M0,0L1,0.03L1,-0.03L0,0"}]])]]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment