-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
212 additions
and
65 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(ns noc.chapter-0-1 | ||
(:require [quil.core :as q])) | ||
|
||
(def sketch | ||
{:size [640 240] | ||
:setup (fn [] | ||
(q/background 255) | ||
{:walker {:x (quot (q/width) 2) | ||
:y (quot (q/height) 2)}}) | ||
:tick (fn [{:keys [walker] :as state}] | ||
(let [choice (Math/floor (q/random 4)) | ||
{:keys [x y]} walker] | ||
(assoc state :walker | ||
(condp = choice | ||
0 {:x (inc x) :y y} | ||
1 {:x (dec x) :y y} | ||
2 {:x x :y (inc y)} | ||
3 {:x x :y (dec y)})))) | ||
:draw (fn [{:keys [walker]}] | ||
(q/stroke 0) | ||
(q/point (:x walker) (:y walker)))}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns noc.chapter-0-2 | ||
(:require [quil.core :as q])) | ||
|
||
(def sketch | ||
{:size [640 240] | ||
:setup (fn [] | ||
{:counts (into [] (repeat 20 0))}) | ||
|
||
:tick (fn [{:keys [counts] :as state}] | ||
(let [idx (Math/floor (q/random (count counts)))] | ||
(update-in state [:counts idx] inc))) | ||
|
||
:draw (fn [{:keys [counts]}] | ||
(q/background 255) | ||
(q/stroke 0) | ||
(q/fill 127) | ||
(let [w (quot (q/width) (count counts))] | ||
(doseq [[idx v] (map-indexed vector counts)] | ||
(q/rect (* idx w) | ||
(- (q/height) v) | ||
(dec w) | ||
v))))}) |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,62 @@ | ||
(ns noc.render | ||
(:require | ||
[noc.custom])) | ||
[noc.sketch])) | ||
|
||
(def icon-reset [:svg | ||
{:stroke "currentColor", | ||
:aria-hidden "true", | ||
:fill "none", | ||
:width "1em", | ||
:xmlns "http://www.w3.org/2000/svg", | ||
:stroke-width "2", | ||
:class "h-[15px] w-[15px]", | ||
:viewBox "0 0 24 24", | ||
:height "1em"} | ||
[:path | ||
{:stroke-linecap "round", | ||
:stroke-linejoin "round", | ||
:d | ||
"M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"}]]) | ||
|
||
(def icon-pause | ||
[:svg | ||
{:stroke "currentColor", | ||
:aria-hidden "true", | ||
:fill "none", | ||
:width "1em", | ||
:xmlns "http://www.w3.org/2000/svg", | ||
:stroke-width "2", | ||
:class "h-4 w-4", | ||
:viewBox "0 0 24 24", | ||
:height "1em"} | ||
[:path | ||
{:stroke-linecap "round", | ||
:stroke-linejoin "round", | ||
:d "M10 9v6m4-6v6m7-3a9 9 0 11-18 0 9 9 0 0118 0z"}]]) | ||
|
||
(defn render-quil-sketch [sketch] | ||
(if (noc.custom/has-sketch? sketch) | ||
[:div {:ref (fn [el] | ||
(prn sketch) | ||
(when el | ||
(noc.custom/load-sketch sketch el)))}] | ||
[:div (str "No sketch named " sketch "found")])) | ||
(let [sketch-opts (noc.sketch/load-sketch sketch) | ||
applet* (atom nil)] | ||
[:div {:class "clear-both my-4 overflow-hidden rounded border bg-gray-100"} | ||
[:div {:class "relative overflow-hidden rounded-t bg-white"} | ||
(if sketch-opts | ||
[:div {:class "w-full border-none transition-opacity opacity-100" | ||
:ref (fn [el] | ||
(prn sketch) | ||
(when el | ||
(reset! applet* (noc.sketch/show-sketch sketch-opts el))))}] | ||
[:div (str "No sketch named " sketch " found")])] | ||
[:div {:class "flex items-center justify-between border-t"} | ||
[:div | ||
{:class "flex"} | ||
[:button {:class "flex items-center border-r px-2.5 py-1.5 text-sm hover:bg-gray-200" | ||
:on-click (fn [_] | ||
(noc.sketch/reset applet*))} | ||
icon-reset | ||
[:span {:class "ml-1.5"} "Reset"]] | ||
[:button | ||
{:class "flex items-center border-r px-2.5 py-1.5 text-sm hover:bg-gray-200" | ||
:on-click (fn [_] | ||
(noc.sketch/toggle-pause applet*))} | ||
icon-pause | ||
[:span {:class "ml-1"} "Pause"]]]]])) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns noc.sketch | ||
(:require [quil.core :as q] | ||
[quil.sketch :as ap :include-macros true] | ||
[noc.chapter-0-1 :as c0.1] | ||
[noc.chapter-0-2 :as c0.2] | ||
[noc.dots :as dots])) | ||
|
||
(def sketches {:dots dots/sketch | ||
:walker c0.1/sketch | ||
:rand-dist c0.2/sketch}) | ||
|
||
(defn load-sketch [s] | ||
(get sketches s)) | ||
|
||
(def default-state {:paused? false | ||
:default-frame-rate 60}) | ||
|
||
(defn setup-state [init-state] | ||
(merge default-state init-state)) | ||
|
||
(defn reset [sketch*] | ||
(let [{:keys [applet opts]} @sketch* | ||
{:keys [setup]} opts] | ||
(ap/with-sketch applet | ||
(let [state* (q/state :state) | ||
new-state (setup-state (setup))] | ||
(reset! state* new-state))))) | ||
|
||
(defn toggle-pause [sketch*] | ||
(let [{:keys [applet]} @sketch*] | ||
(ap/with-sketch applet | ||
(let [state* (q/state :state) | ||
state @state*] | ||
(if (= 0 (.frameRate applet)) | ||
(do | ||
(q/frame-rate (:default-frame-rate state)) | ||
(swap! state* assoc :paused? false)) | ||
(do | ||
(q/frame-rate 0) | ||
(swap! state* assoc :paused? true))))))) | ||
|
||
(defn setup-wrapper [setup] | ||
(let [state (setup-state (setup))] | ||
(q/set-state! :state (atom state) :init-state state))) | ||
|
||
(defn draw-wrapper [tick draw] | ||
(let [state* (q/state :state)] | ||
(reset! state* (tick @state*)) | ||
(draw @state*))) | ||
|
||
(defn show-sketch [{:keys [setup tick draw] :as opts} el] | ||
{:applet (apply q/sketch (apply concat | ||
(-> opts | ||
(assoc :host el) | ||
(assoc :setup (partial setup-wrapper setup)) | ||
(assoc :draw (partial draw-wrapper tick draw))))) | ||
:opts opts | ||
:el el}) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
;; # Chapter 0 | ||
|
||
(ns chapter-0 | ||
{:nextjournal.clerk/visibility {:code :hide}} | ||
(:require [nextjournal.clerk :as clerk] | ||
[noc.quil-clerk :refer [show-sketch]])) | ||
|
||
;; ## [Example 0.1: A Traditional Random Walk](https://natureofcode.com/random/#example-01-a-traditional-random-walk) | ||
|
||
^{::clerk/no-cache true ::clerk/viewer clerk/code} | ||
(slurp "dev/noc/chapter_0_1.cljs") | ||
|
||
(show-sketch :walker) | ||
|
||
;; ## [Example 0.2: A Random-Number Distribution](https://natureofcode.com/random/#example-02-a-random-number-distribution) | ||
|
||
^{::clerk/no-cache true ::clerk/viewer clerk/code} | ||
(slurp "dev/noc/chapter_0_2.cljs") | ||
|
||
(show-sketch :rand-dist) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(ns noc.quil-clerk | ||
(:require [nextjournal.clerk :as clerk])) | ||
|
||
^{:nextjournal.clerk/visibility {:result :hide}} | ||
(defn make-sketch-viewer [] | ||
{:transform-fn clerk/mark-presented | ||
:render-fn 'noc.render/render-quil-sketch}) | ||
|
||
^{:nextjournal.clerk/visibility {:result :hide}} | ||
(defn show-sketch [sketch-name] | ||
(clerk/with-viewer (make-sketch-viewer) sketch-name)) |