;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 

;; SIMPLE ANIMATION EXAMPLE

;;

;; Stochastically moves a square

;; around a canvas while

;; constantly changing it's colour.

;;

;; This example uses the gfx:make-square definition

;; which saves the trouble of creationg a path

;; as was done in the square.scm example. There are

;; shape definitions for rectangle, circle and

;; oval also.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; create a new canvas

(define *anim-canvas* (gfx:make-canvas))


; value used to stop infinite recursion

(define *go* #t)


; randomly walk a path around a canvas

; begin careful to stay within the

; bounds of the canvas

(define (move-square time path)

   (let ((pathX (car (gfx:get-path-bounds path)))

         (pathY (cadr (gfx:get-path-bounds path)))

         (x (random -10 11))

         (y (random -10 11)))

      (cond ((> (+ pathX x) (car (gfx:get-canvas-size *anim-canvas*))) 

             (set! x -10))

            ((> (+ pathY y) (cdr (gfx:get-canvas-size *anim-canvas*))) 

             (set! y -10))

            ((< (- pathX x) 0) 

             (set! x 10))

            ((< (- pathY y) 0) 

             (set! y 10)))

      (gfx:move-path time path x y)))


; 1) clear the canvase 

; 2) draw the square with a random colour

; 3) randomly move the square

(define (drawer time path)

   (gfx:clear-canvas time *anim-canvas* '(0.0 0.0 0.0 1.0))

   (move-square time path)   

   (gfx:draw-path (+ time 4) 

              *anim-canvas* 

              path

              ()

              (list (random) (random) (random) 1.0)) ; r g b alphad

   (if *go* 

       (callback (+ time 3000) 'drawer (+ time 4000) path)))


; start function

(define (start time)

   (set! *go* #t)

   (drawer time (gfx:make-square 250 200 100)))


; stop function

(define stop

   (lambda ()

      (set! *go* #f)))


; STOP

(stop)


; START

(start (now))