Skip to content

Instantly share code, notes, and snippets.

@ctford
Created March 31, 2015 20:24
Show Gist options
  • Save ctford/360ca6dffe5b471956dc to your computer and use it in GitHub Desktop.
Save ctford/360ca6dffe5b471956dc to your computer and use it in GitHub Desktop.
Independent boids
(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