Plasma

canvasanimation

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.

6 Sept 2014 by rm-hull
rm-hull/1be4335002213cce2999
plasma.cljs
(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)