Skip to content

Instantly share code, notes, and snippets.

@t-sin
Created July 27, 2015 16:12
Show Gist options
  • Save t-sin/df89ba56a566e095f69f to your computer and use it in GitHub Desktop.
Save t-sin/df89ba56a566e095f69f to your computer and use it in GitHub Desktop.
Playing with Quil!
(ns quil-test.core
(:require [quil.core :as q]
[quil.middleware :as m]))
(def width 640)
(def height 480)
(def circle-num 10)
(defn rand-point [w h]
[(rand-int w) (rand-int h)])
(defn calc-radius [])
(defn setup []
; Set frame rate to 30 frames per second.
(q/frame-rate 30)
; Set color mode to HSB (HSV) instead of default RGB.
(q/color-mode :rgb)
; setup function returns initial state. It contains
; circle color and position.
{:points (for [n (range circle-num)] (rand-point width height))
:phases (for [n (range circle-num)] (rand-int 60))})
(defn update-state [state]
; Update sketch state by changing circle color and position.
{:points (let [pts (:points state)
phs (:phases state)]
(for [[idx pt] (map-indexed vector pts)]
(if (= (nth phs idx) 49)
(rand-point width height)
pt)))
:phases (map #(mod (inc %) 50) (:phases state))})
(defn draw-state [state]
; Clear the sketch by filling it with light-grey color.
(q/background 50)
; Set circle color.
(q/fill 255 0)
(q/stroke-weight 2)
(q/stroke 150 180 200)
(dotimes [i (count (:points state))]
(let [pt (nth (:points state) i)
ph (nth (:phases state) i)]
(dotimes [n (int (/ ph 10))]
(q/ellipse (nth pt 0) (nth pt 1) (+ 30 (* 40 n)) (+ 30 (* 40 n)))))))
(q/defsketch quil-test
:title "nanka asonde miru!"
:size [width height]
; setup function called only once, during sketch initialization.
:setup setup
; update-state is called on each iteration before draw-state.
:update update-state
:draw draw-state
:features [:keep-on-top]
; This sketch uses functional-mode middleware.
; Check quil wiki for more info about middlewares and particularly
; fun-mode.
:middleware [m/fun-mode])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment