10 PRINT

canvasanimation

_10 PRINT_ is a book about a one-line Commodore 64 BASIC program, published in November 2012. To paraphrase from http://10print.org/ _"... a single line of code—the extremely concise BASIC program for the Commodore 64 inscribed in the title—and uses it as a lens through which to consider the phenomenon of creative computing and the way computer programs exist in culture."_ This gist replicates that functionality (more verbosely) when you move the mouse - essentially treating the stream of mouse-move events as the basis of a random-number generator with which to generate the maze.

13 Sept 2014 by rm-hull
rm-hull/4bf4ce47c4f615e9cfe6
10-print.cljs
;  10 PRINT CHR$(205.5+RND(1)); : GOTO 10

(ns big-bang.examples.ten-print
  (:require
    [jayq.core :refer [show]]
    [inkspot.color-chart :as cc]
    [big-bang.core :refer [big-bang]]
    [big-bang.events.browser :refer [client-coords]]
    [enchilada :refer [ctx canvas canvas-size value-of]]
    [monet.canvas :refer [begin-path move-to line-to
                         stroke-width stroke-style stroke-cap stroke
                          fill-rect fill-style]]))

(def dimensions
  (let [[width height] (canvas-size)]
    {:x 0 :y 0 :w width :h height}))

(def initial-state
  (let [size (value-of :size 20)]
    { :t 0
      :size size
      :mouse-coords [0 0]
      :gradient (cc/ui-gradient 
                  (value-of :gradient :candy)
                  (/ (:w dimensions) size))}))

; TODO: move into big-bang.events.browser
(defn touch-coords [event]
  (when-let [touch-object (.-changedTouches event)]
    (client-coords (aget touch-object 0))))

(defn handle-movement [event world-state]
  (->
    world-state
    (update-in [:t] inc)
    (assoc :mouse-coords (or (touch-coords event) 
                             (client-coords event)))))

(defn determine-coords [mouse-coords x y size]
  (if (zero? (mod (apply + mouse-coords) 2))
    [x y (+ x size) (+ y size)]
    [x (+ y size) (+ x size) y]))
    

(defn render [{:keys [t mouse-coords gradient size mouse-coords] :as world-state}]
  (let [x (* size (mod t (/ (:w dimensions) size)))
        y (* size (mod (quot t (/ (:w dimensions) size)) (/ (:h dimensions) size)))
        color (nth gradient (/ x size))
        [x' y' x'' y''] (determine-coords mouse-coords x y size)]
    (->
      ctx
      (fill-style :white)
      (fill-rect {:x x :y y :w size :h size})
      (stroke-width 4)
      ;(stroke-cap :square)
      (stroke-style color)
      (begin-path)
      (move-to x' y')
      (line-to x'' y'')
      (stroke))))

(show canvas)

(big-bang
  :initial-state initial-state
  :on-mousemove handle-movement
  :on-touchmove handle-movement
  :to-draw render)