-
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
6 changed files
with
164 additions
and
24 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
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,19 @@ | ||
(ns noc.chapter-0-4 | ||
(:require | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [{:keys [width height]}] | ||
{}) | ||
|
||
(defn setup! [] | ||
(q/background 255)) | ||
|
||
(defn tick [s] s) | ||
|
||
(defn draw! [_] | ||
(let [x (+ 320 (* 60 (q/random-gaussian)))] | ||
(q/no-stroke) | ||
(q/fill 0 10) | ||
(q/ellipse x 120 16 16))) |
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,27 @@ | ||
(ns noc.chapter-0-4e | ||
(:require | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [_] | ||
{:ui {:stdev {:type :slider | ||
:min 5 :max 120 | ||
:value 10}}}) | ||
|
||
(defn setup! [] | ||
(q/color-mode :hsl 360) | ||
(q/background 360)) | ||
|
||
(defn tick [s] | ||
s) | ||
|
||
(defn rand-gaus [mean stdev] (+ mean (* stdev (q/random-gaussian)))) | ||
|
||
(defn draw! [{:keys [width height ui]}] | ||
(let [stdev (get-in ui [:stdev :value]) | ||
x (rand-gaus (quot width 2) stdev) | ||
y (rand-gaus (quot height 2) stdev)] | ||
(q/no-stroke) | ||
(q/fill (rand-gaus 0 stdev) 200 200 200) | ||
(q/ellipse x y 16 16))) |
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,56 @@ | ||
(ns noc.ui | ||
(:require | ||
[quil.sketch :as ap :include-macros true])) | ||
|
||
(defn slider | ||
"Creates a slider <input></input> element. | ||
ref: https://p5js.org/reference/#/p5/createSlider" | ||
([{:keys [value step position size min max] | ||
:or {min 0 max 100 value 0 step 0} :as opts}] | ||
(let [slider (.createSlider (ap/current-applet) min max value step)] | ||
(when position | ||
(.position slider (first position) (second position))) | ||
(when size | ||
(.size slider size)) | ||
slider))) | ||
|
||
(defn slider-value [s] | ||
(.value s)) | ||
|
||
(defn prepare-element [state id {:keys [type] :as def}] | ||
(condp = type | ||
:slider (assoc-in state [:_ui id] (slider def)) | ||
state)) | ||
|
||
(defn prepare-ui [{:keys [ui] :as state}] | ||
(if ui | ||
(reduce (fn [state [id ui-def]] | ||
(prepare-element state id ui-def)) state ui) | ||
state)) | ||
|
||
(defn update-element [state id] | ||
(let [handle (get-in state [:_ui id]) | ||
def (get-in state [:ui id])] | ||
(if def | ||
(condp = (:type def) | ||
:slider (assoc-in state [:ui id :value] (slider-value handle)) | ||
state) | ||
(do | ||
(.remove handle) | ||
(update-in state [:_ui] #(dissoc % id)))))) | ||
|
||
(defn update-ui | ||
"Populates the state map with the current values of any ui elements." | ||
[state] | ||
(if (:_ui state) | ||
(reduce (fn [state [id _]] | ||
(update-element state id)) state (:_ui state)) | ||
state)) | ||
|
||
(defn remove-all! | ||
"Removes all UI elements from the DOM, returns nil. " | ||
[state] | ||
(when (:_ui state) | ||
(doseq [[_ handle] (:_ui state)] | ||
(when handle | ||
(.remove handle))))) |