Wednesday, May 19, 2021

Playing with Racket: random lines GIF

I'm enjoying playing with Racket. Takes me back to some good old college days writing Scheme, but I don't think creating a GIF was so easy in any of the Scheme systems we were using then. (Which ecosystem -- language + standard libraries -- provides the nicest environment for making animated GIFs?)
#lang racket

(require mrlib/gif)
(require racket/draw)

(define (random-lines-bitmap width height line-count)
  (let* ([target (make-bitmap width height)]
         [dc (new bitmap-dc% [bitmap target])])
    (begin
      (send dc draw-rectangle
       0 0   ; Top-left at (0, 10), 10 pixels down from top-left
       (- width 1) (- height 1))
      (for ([i line-count])
        (send dc draw-line
              (random width) (random height)
              (random width) (random height)))
      target)))

(define (write-random-lines-gif width height line-count frame-count filename)
  (write-animated-gif
   (for/list ([i frame-count])
     (random-lines-bitmap width height line-count))
   10
   filename
   #:loop? true))

(write-random-lines-gif 300 200 50 10 "random-lines.gif")

No comments: