Created
March 31, 2015 20:24
-
-
Save ctford/360ca6dffe5b471956dc to your computer and use it in GitHub Desktop.
Independent boids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns universe.core | |
(:require [om.core :as om :include-macros true] | |
[om.dom :as dom :include-macros true])) | |
(def app-state (atom {:text "Hello Boids" | |
:birds (map vector | |
(repeatedly 20 #(rand-int 1000)) | |
(repeatedly 20 #(rand-int 1000)))})) | |
(defn make-bird [[x y]] | |
(dom/circle #js {:cx x :cy y :r 12 :fill (str "rgb(" x ",150," y ")")})) | |
(defn flock [birds] | |
(->> birds | |
(map #(update-in % [0] (fn [x] (mod (+ x (rand-int 3)) 1000)))) | |
(map #(update-in % [1] (fn [y] (mod (+ y (rand-int 5)) 1000)))))) | |
(defn make-map [circles] | |
(apply dom/svg #js {:viewBox "0 0 1000 1000"} circles)) | |
(defn main [] | |
(om/root | |
(fn [app owner] | |
(reify | |
om/IRender | |
(render [_] | |
(dom/div nil | |
(dom/h1 nil (:text app)) | |
(make-map | |
(->> app | |
:birds | |
(map make-bird))))) | |
om/IWillMount | |
(will-mount [_] | |
(js/setInterval (fn [] (om/transact! app :birds flock) 1000))))) | |
app-state | |
{:target (. js/document (getElementById "app"))})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment