jeremy-website/cljs/jeremy_website/sketches/tree.cljs

116 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

Rebuild site with Obelix site builder Squashed commit of the following: commit b61428ef9441b6c6b68192483f20576576484c46 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 10:30:13 2020 -0500 Remove draft post commit b4e01335329091ea0618805515414498487e4569 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 10:29:17 2020 -0500 Make RSS feed commit f9c2956db1f44fea2c9f45d18aad189a950bad17 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 10:28:50 2020 -0500 Update to Obelix 1.1.0 (no longer escapes HTML automatically) commit 1d0b09fb904bee4d19af9eb96c8ec7945b4e4257 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:57:44 2020 -0500 Don't draw <hr> on last blog summary commit e97d3399614d04ae2f2364e18de60d8076d66ba4 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:53:00 2020 -0500 Fix mobile layout commit 4931b8153106702f33200f8098314a54151350d6 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:46:07 2020 -0500 Dedupe tree branches correctly commit 54708b956e1bc4722efbef5f01f96985891a702e Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:45:54 2020 -0500 Add randomness to tree sketch commit 81a68a8d1a92630394082f5218574b52aced56d1 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:45:29 2020 -0500 Re-render sketch on window resize commit eca26effffb5dbc1d881031a76d044608bd3fa69 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Thu Dec 10 08:44:46 2020 -0500 Support stochastic l-system grammars commit 6050d59408548c7d014da450d0e6737fe768f19f Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 21:07:00 2020 -0500 Add TODO commit d478bcc3f642dd58e93e8ef0542e19ea9acc6f1a Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 21:05:51 2020 -0500 Don't sort l-system output; downsize to 6 iteration commit f01f9135172e7b60480b64ede936e380384c0060 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 17:26:15 2020 -0500 [WIP] Leave game plan comment commit e191ea7b86e05098d2fcc2e6ea99c06e6ad493fb Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 17:17:44 2020 -0500 [WIP] Don't draw lines multiple times commit 7cbc9bd839743fa660962f036f34817c6deb05de Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 13:57:49 2020 -0500 Batch size = 50 commit 571ea7728e2605456578be7f70e0c0806434b750 Author: Jeremy Dormitzer <jeremydormitzer@lola.com> Date: Mon Dec 7 13:55:19 2020 -0500 Account for depth when rendering tree branches commit 53469c671c0ecf92edaabd152b58a532dfb5f54f Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Mon Dec 7 08:45:50 2020 -0500 Have the tree draw itself commit 5b023911a346f72cb4fea56aaedebc371b0f7135 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Mon Dec 7 07:11:08 2020 -0500 Calculate l-system once commit 4b9daa8cd2e5790973e93d2ff3909a4791d3332b Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 11:38:15 2020 -0500 Extract l-system->lines into a function commit 7bcef9263aadcb00b0e8e045e23ccfca4ef22a0f Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 11:21:04 2020 -0500 Tweak plant: more iterations, smaller segment length commit 9b37f41af829d64ffd2513573eaf71ab4f6b1fe1 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 11:16:22 2020 -0500 Move all calculations to update-state; use more natural l-system commit 6496fa4b57487a97d037ecb5d357ccd0c1db13a1 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 11:16:10 2020 -0500 Add l-system/rules macro commit 17e1b0f867fe5ea6e188e67b90a5895d9d34cfef Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 11:15:39 2020 -0500 Optimize l-system/step and allow strings as axioms commit f7b35123c3f01e65449a2393645a5b4719c5a9e0 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 10:00:03 2020 -0500 Implement test l-system commit 2088e66fad63ca890c9c4b1c268d868d03d9a7f5 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 08:57:10 2020 -0500 Use HSB color mode in tree sketch commit bf7aac0636c1208a9226df7fce29fce633453cf2 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 08:57:01 2020 -0500 Clean up main.cljs commit 7c5a4cda9932d8ddcd29c5540fb326d444ce9241 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 08:56:35 2020 -0500 Hide empty scroll bar on landing page commit 92559812c1e7a6cc25a0f507f978028a2619fbf8 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 08:55:59 2020 -0500 Initial l-system implementation commit 2e8669c0434a0b26d7571b1cd7098ba12ce4323c Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Dec 6 06:51:31 2020 -0500 Ignore cache files commit 14c605639a7d3862dccb9ecc23f0c9fe23a34144 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Tue Dec 1 08:28:57 2020 -0500 Lay out sketch on main page commit c58155b13bcf285bdcc0d5ceb12ecda442631832 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Mon Nov 30 07:45:11 2020 -0500 Fix makefile target name commit 888ec38de1cfcfe184192a42313232a837cea49c Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Mon Nov 30 07:42:48 2020 -0500 [WIP] Put a quil sketch on the homepage commit 59145b883e77d2b32a483f44d93ebc47ea1ab493 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 29 10:57:18 2020 -0500 Add Makefile with production build command commit febd037d06a4d03efe5bad9c35ce67c169b61347 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 29 10:41:07 2020 -0500 Remove :bundle-freq figwheel option commit 4f15b81202c12270762c2145b224dae62bd544b8 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 29 09:35:22 2020 -0500 Set up Figwheel build + obelix serve config commit fece49200abd1348c7a2d9585c06878f9ce5ac0b Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sat Nov 21 15:10:11 2020 -0500 Add cljs source commit a0b2ed9c36b37bb83b35addcb39a791738704a92 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Thu Nov 19 21:35:23 2020 -0500 Don't make images full-bleed commit cb0b2f6e321616f1d4538bfaa86e266656356522 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Thu Nov 19 19:32:05 2020 -0500 Match index page styling to existing site commit 03f6b943ad2299789583ba35ad08a1686f703a92 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Thu Nov 19 08:26:27 2020 -0500 Format the blog index page commit 782c13fcdf1309806dd065af5383f92d5e1209cc Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Thu Nov 19 07:39:05 2020 -0500 Sort blog posts by publish date on blog index commit dd59865f6ef127604d0508302964fa130b834208 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Wed Nov 18 08:35:52 2020 -0500 Update obelix-plugin-typography version commit 682e69aff9ffb8aecfd3d2bc09b73e00a390d815 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Wed Nov 18 08:35:09 2020 -0500 Style the header commit 4ae624d42215c309f1e6c3b7fbbb0b43b83282b8 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Wed Nov 18 08:34:54 2020 -0500 Add Google Fonts links commit 8d3a3ffc3c32d6f40c37a7692fa0f87dd5a11f55 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 15 16:00:31 2020 -0500 Add typography.js plugin commit fdf743ad58d8a0978778f9577f766cae815e71b5 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 16:26:01 2020 -0500 Sketch out site styling commit 4a6b986d27a4aae364942da1571d2cb86a902963 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 16:25:53 2020 -0500 Add titles commit 3cd7ad4cef10a53ac94a90527667453c433344d5 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 16:25:23 2020 -0500 Make blog headers h2s commit 28106c2d37eefacca98160a09cf8572485e1f3f2 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:08:08 2020 -0500 Fix markdown formatting commit 2549d00cccb9c782b5dcc3b98eaf436e1e2e6d9b Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:07:57 2020 -0500 Make header images full-bleed commit 9256dc3c3e1f6c6db0772533e3b9003ebfaf240b Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:03:07 2020 -0500 Add preliminary blog index commit 3380b8c02a87abfac62edc09af79331b802ccd0d Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:02:57 2020 -0500 Make sure index.md gets read as a page commit cc0aecdf1d039602d69a5b2669826a99228b426f Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:02:40 2020 -0500 Flesh out layout and add separate blog layout commit e978957fe36a6bce93ade00b7e5f5c03e633d09a Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:02:07 2020 -0500 Vendor in highlight.js commit 60d3b7071ba128f026f9c9423c643fc21707cbdb Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sun Nov 8 13:01:22 2020 -0500 Delete package.json and rely on globally-installed Obelix commit 9d0cd7b46e52885012a1c94cca0a7c26811afef4 Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Thu Nov 5 09:43:13 2020 -0500 Port blog posts and index over to Obelix commit 800f4a088bd72f999835280d3381e9307c32776f Author: Jeremy Dormitzer <jeremy.dormitzer@gmail.com> Date: Sat Oct 10 15:06:19 2020 -0400 Delete everything
2020-12-10 15:42:04 +00:00
(ns jeremy-website.sketches.tree
(:require [jeremy-website.l-system :as l-system :include-macros true]
[quil.core :as q]
[quil.middleware :as m]))
(defn degrees->radians [deg]
(* deg (/ js/Math.PI 180)))
(defn l-system-lines
[{:keys [segment-length initial-angle initial-x initial-y l-system]}]
(vec
(:lines
(reduce
(fn [{:keys [x y angle angle-stack position-stack depth lines] :as state}
symbol]
(condp = symbol
"F" (let [new-x (+ x (* segment-length
(js/Math.cos (degrees->radians angle))))
new-y (+ y (* (- segment-length)
(js/Math.sin (degrees->radians angle))))
line {:x1 x :y1 y :x2 new-x :y2 new-y}]
(assoc state
:x new-x
:y new-y
:lines (conj lines (assoc line :depth depth))))
"+" (assoc state :angle (+ angle 25))
"-" (assoc state :angle (- angle 25))
"*" (assoc state :angle (+ angle 20))
"/" (assoc state :angle (- angle 20))
">" (assoc state :angle (+ angle 15))
"<" (assoc state :angle (- angle 15))
"[" (assoc state
:angle-stack (conj angle-stack angle)
:position-stack (conj position-stack [x y])
:depth (inc depth))
"]" (assoc state
:angle (peek angle-stack)
:angle-stack (pop angle-stack)
:x (first (peek position-stack))
:y (second (peek position-stack))
:position-stack (pop position-stack)
:depth (dec depth))
state))
{:angle initial-angle
:angle-stack [initial-angle]
:x initial-x
:y initial-y
:position-stack [[initial-x initial-y]]
:depth 0
:lines []}
(:state l-system)))))
(defn setup []
;; Hue goes from 0-360, saturation/brightness from 0-100
(q/color-mode :hsb 360 100 100)
(q/frame-rate 60)
(q/random-seed (or (-> (js/URLSearchParams. js/window.location.search)
(.get "seed"))
(.now js/Date)))
(let [l-system (-> (l-system/instantiate {:axiom "X"
:rules (l-system/rules
"X(0.4)" -> "F+[[X]-X]-F[-FX]+X"
"X(0.4)" -> "F*[[X]/X]/F[/FX]*X"
"X(0.2)" -> "F>[[X]<X]<F[<FX]>X"
"F" -> "FF")
:random-fn (partial q/random 1)})
((apply comp (repeat 6 l-system/step))))
lines (l-system-lines {:initial-angle (- 90 25)
:segment-length 6
:initial-x (* (q/width) 0.1)
:initial-y (* (q/height) 0.95)
:l-system l-system})]
{:to-draw lines
:drawing []}))
(defn update-state [{:keys [to-draw drawing] :as state}]
(let [batch-size 10]
(if (> (count to-draw) 0)
(assoc state
:to-draw (vec (drop batch-size to-draw))
:drawing (vec (concat drawing (take batch-size to-draw))))
state)))
(defn dedupe-lines [lines]
(:lines
(reduce (fn [{:keys [lines already-seen] :as state} line]
(if-not (contains? already-seen ((juxt :x1 :y1 :x2 :y2) line))
(assoc state
:already-seen (conj already-seen ((juxt :x1 :y1 :x2 :y2) line))
:lines (conj lines line))
state))
{:lines []
:already-seen #{}}
lines)))
(defn draw [{:keys [to-draw drawing]}]
(q/background 0 0 100)
(q/fill 0 0 0)
(let [lines (dedupe-lines drawing)]
(doseq [line lines]
(q/stroke-weight (- 1.75 (* 0.1 (:depth line))))
(q/stroke 240 0 (+ 65 (* 2 (:depth line))))
(apply q/line ((juxt :x1 :y1 :x2 :y2) line))))
(when (= (count to-draw) 0)
(q/no-loop)))
(defn sketch
[{:keys [host size]}]
(q/sketch
:host host
:middleware [m/fun-mode]
:size size
:setup setup
:update update-state
:draw draw))