diff --git a/src_clj/sketchbook/handler.clj b/src_clj/sketchbook/handler.clj index 6892afe..e877eaa 100644 --- a/src_clj/sketchbook/handler.clj +++ b/src_clj/sketchbook/handler.clj @@ -1,7 +1,9 @@ (ns sketchbook.handler (:require [compojure.core :refer :all] [compojure.route :as route] + [ring.util.response :refer [response content-type]] [ring.middleware.defaults :refer [wrap-defaults site-defaults]] + [ring.middleware.json :refer [wrap-json-response]] [ring.server.standalone :refer [serve]] [clojure.core.async :refer [ app-routes (wrap-defaults site-defaults) + (wrap-json-response) (wrap-index))) (defn -main [& args] diff --git a/src_clj/sketchbook/sketches.clj b/src_clj/sketchbook/sketches.clj index 0ddd345..e8482fc 100644 --- a/src_clj/sketchbook/sketches.clj +++ b/src_clj/sketchbook/sketches.clj @@ -8,25 +8,45 @@ (def sketches {:color {:setup sketchbook.sketches.color/setup - :draw sketchbook.sketches.color/draw}}) + :draw sketchbook.sketches.color/draw + :title "Color Play"} + :foo {:setup sketchbook.sketches.color/setup + :draw sketchbook.sketches.color/draw + :title "Foo"}}) -(defn sketch-bytes [& {:keys [size setup draw seed]}] +(defn get-sketches [] + (map (fn [sketch] + (let [path (name (first sketch))] + {:path path + :title (:title (second sketch))})) + sketches)) + +(defn sketch-bytes [& {:keys [name size setup draw seed]}] (let [channel (chan) - nonce (str (System/currentTimeMillis)) - filename (str "sketch-" nonce ".png") + nonce (str (System/nanoTime)) + filename (str "sketch-files/" + name "-" + (first size) "-" + (second size) "-" + seed + ".png") out (ByteArrayOutputStream.)] - (q/sketch :size size - :setup (fn [] - (r/set-seed seed) - (setup)) - :draw (fn [] - (draw) - (q/save filename) - (with-open [in (io/input-stream filename)] - (io/copy in out)) - (io/delete-file filename) - (go (>! channel (.toByteArray out)) - (q/exit)))) + (if (.exists (io/file filename)) + (do + (with-open [in (io/input-stream filename)] + (io/copy in out)) + (go (>! channel (.toByteArray out)))) + (q/sketch :size [(/ (first size) 2) (/ (second size) 2)] ;; For some reason images were coming out twice as large as they should be + :setup (fn [] + (r/set-seed seed) + (setup)) + :draw (fn [] + (draw) + (q/save filename) + (with-open [in (io/input-stream filename)] + (io/copy in out)) + (go (>! channel (.toByteArray out)) + (q/exit))))) channel)) (defn render-sketch @@ -37,7 +57,8 @@ [name seed size] (let [sketch (get sketches name)] (when-not (nil? sketch) - (sketch-bytes :size size + (sketch-bytes :name name + :size size :seed seed :setup (:setup sketch) :draw (:draw sketch)))))