Create a gist now

Instantly share code, notes, and snippets.

@rm-hull /plasma.cljs
Last active Aug 29, 2015

What would you like to do?
Old-skool plasma effect, flogged to death in the early 90's demo scene - the plasma is basically a function on 2D space created by adding together a few sinusoids. By combining different types of sines and adding a time component the illusion of motion is achieved.
(ns big-bang.examples.plasma
(:require
[big-bang.core :refer [big-bang]]
[enchilada :refer [ctx canvas canvas-size]]
[jayq.core :refer [show]]))
(def initial-state
(let [block-size 15
[width height] (map #(quot % block-size) (canvas-size))]
{:t 1 :block-size block-size :w width :h height}))
(defn update-state [event world-state]
(update-in world-state [:n] inc))
(defn render [{:keys [n block-size w h] :as world-state}]
(let [m (* n 4)]
(doseq [y (range h)
:let [y' (* y block-size)]
x (range w)
:let [x' (* x block-size)
s (Math/sin (* x 10))
; RGB color components
r (Math/floor (* 255 (+ (Math/cos (/ (+ x y n s) 20))
(Math/cos (/ (+ x y m) 10)))))
g (Math/floor (* 255 (+ (Math/sin (/ (+ x y m s) 20))
(Math/cos (/ (+ x x n) 10)))))
b (Math/floor (* 255 (+ (Math/cos (/ (+ x (Math/cos (/ y (+ x m))) (Math/sin (* y 10))) 20))
(Math/cos (/ (+ x x n) 20)))))]]
(set! (.-fillStyle ctx) (str "rgba(" r "," g "," b ",0.3)"))
(. ctx (fillRect x' y' block-size block-size)))
ctx))
(show canvas)
(big-bang
:initial-state initial-state
:on-tick update-state
:to-draw render)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment