JSCL Flower


A little program to draw a flower written with Common Lisp in the browser.

(defun to-radix (var radix)
  ((jscl::oget var "toString") radix))

(defun random-hex ()
  (to-radix (random 15) 16))

(defun get-random-colour ()
  (concatenate 'string
    "#"
    (loop for _ below 6 collect (random-hex))))

(defvar step-scale 0.99)
(defconstant +PI+ #j:Math:PI)

(defun render-leaflet (rotation)
 (#j:ctx:scale step-scale step-scale)
 (#j:ctx:rotate rotation)
 (#j:ctx:beginPath)
 (#j:ctx:arc 0 50 50 (/ (- +PI+) 2) 0)
 (setf #j:ctx:fillStyle "#000")
 (#j:ctx:fill)
 (#j:ctx:beginPath)
 (#j:ctx:arc 50 0 50 (- +PI+) (/ +PI+ 2) t)
 (setf #j:ctx:fillStyle (get-random-colour))
 (#j:ctx:fill))

(defun reset-matrix ()
  (#j:ctx:setTransform 1 0 0 1 0 0)
  (#j:ctx:translate
    (/ #j:window:innerWidth 2)
    (/ #j:window:innerWidth 2))
  (#j:ctx:scale 5 5))

(defun flower-main ()
  (setf #j:canvas (#j:document:getElementById "jscl-flower-canvas")
        #j:ctx (#j:canvas:getContext "2d")
        #j:ctx:canvas:width #j:window:innerWidth
        #j:ctx:canvas:height #j:window:innerWidth)
  (reset-matrix)
  (let (interval-id)
    (setf interval-id
      (#j:setInterval
        (let ((i 0))
          (lambda ()
            (format t "Rendering ~a~%" i)
            (when (>= i 300)
              (#j:clearInterval interval-id))
            (render-leaflet (/ (incf i) 50))))
        50))))

Powered by JSCL