Tau

FPS:

Click on the boxes below to start the automatons. (256x256 rule 30 1D CA)

The main thread automatons will quickly bog down the main thread.

Automatons on the tauons will do all work off the main thread.

(note: to try this example, you must enable SharedArrayBuffers via chrome://flags)

main thread:

on tauon:

Example code for automatons running off main thread.

(ns tau.alpha.example.core
  (:require [tau.alpha.core :refer [on-screen? tauon tau db get-id]]
            [tau.alpha.example.automata :refer [get-frame make-initial-conditions]]
            [tau.alpha.example.utils :refer [raf on-click actx]]))

(def local-store (atom {}))

(defn paint-automaton [ctx width height iref]
  (let [idata (.createImageData ctx width height)]
    (.set (.-data idata) @iref)
    (.putImageData ctx idata 0 0 0 0 width height)))

(defn get-automaton [tau]
  (get @local-store (get-id tau)))

(defn process-results [tau]
  (raf
   #(let [{:keys [ctx width height on?]} (get-automaton tau)]
      (paint-automaton ctx width height tau)
      (swap! on? (constantly @on?)))))

(defn swap-on! [tauon tau work-fn]
  (on tauon [tau work-fn]
    (swap! tau work-fn)
    (on "screen" [tau]
      (process-results tau))))

(defn run-loop-once [on? tau]
  (when @on?
    (let [{:keys [tauon]} (get-automaton tau)]
      (swap-on! tauon tau get-frame))))

(defn setup-tauon [{:keys [width] :as automaton}]
  (let [tauon (tauon)
        tau (tau (make-initial-conditions width) :ab true :size 1000000)]
    (swap! local-store assoc (str (get-id tau))
           (merge automaton {:on? (atom false) :tauon tauon :tau tau}))
    (get-automaton tau)))

(defn start-automaton [on? tau]
  (swap! on? not)
  (run-loop-once on? tau))

(defn hookup-automaton [astr]
  (let [{:keys [canvas] :as automaton} (actx astr)
        {:keys [tau on?]} (setup-tauon automaton)]
    (on-click canvas #(start-automaton on? tau))
    (add-watch on? tau #(run-loop-once on? tau))))

(when (on-screen?)
  (hookup-automaton "a2")
  (hookup-automaton "b2")
  (hookup-automaton "c2")
  (hookup-automaton "d2")
  (hookup-automaton "e2")
  (hookup-automaton "f2"))