Skip to content

Instantly share code, notes, and snippets.

@nvbn nvbn/core.cljs
Created Aug 11, 2015

Embed
What would you like to do?
Shadow Script examples
(ns ^:figwheel-always rerenderer.examples.core
(:require [rerenderer.core :as r :include-macros true]
[rerenderer.browser :refer [interprete]]))
(enable-console-print!)
(def scene (.. js/window -location -hash))
(defn render!
[canvas-id]
(let [vars (interprete @r/script)
canvas (vars canvas-id)
parent-canvas (.getElementById js/document "canvas")
parent-ctx (.getContext parent-canvas "2d")]
(.drawImage parent-ctx canvas 0 0))
(reset! r/script []))
(when (= scene "#1")
(let [canvas (r/new Canvas)
context (r/.. canvas (getContext "2d"))]
(r/set! (r/.. canvas -width) 200)
(r/set! (r/.. canvas -height) 200)
(r/set! (r/.. context -fillStyle) "red")
(r/.. context (fillRect 0 0 100 100))
(render! canvas)))
(defn draw-box
[color w h]
(let [canvas (r/new Canvas)
context (r/.. canvas (getContext "2d"))]
(r/set! (r/.. canvas -width) w)
(r/set! (r/.. canvas -height) h)
(r/set! (r/.. context -fillStyle) color)
(r/.. context (fillRect 0 0 w h))
canvas))
(when (= scene "#2")
(def state {:color "yellow"})
(let [red-box (draw-box "red" 50 50)
another-box (draw-box (:color state) 800 600)
another-box-ctx (r/.. another-box (getContext "2d"))]
(r/.. another-box-ctx (drawImage red-box 50 50))
(render! another-box)))
(when (= scene "#3")
(def state {:color "green"})
(let [red-box (draw-box "red" 50 50)
another-box (draw-box (:color state) 800 600)
another-box-ctx (r/.. another-box (getContext "2d"))]
(r/.. another-box-ctx (drawImage red-box 50 50))
(render! another-box)))
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=800, user-scalable=no" />
</head>
<body>
<canvas width="800" height="600" id="canvas"></canvas>
<script src="optimized.js" type="text/javascript"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.