Skip to content

zhaozhemin/pict-lang

Repository files navigation

A Picture Language in JavaScript

This is a JavaScript implementation of Peter Henderson’s 1982 paper “Functional Geometry”. The most important element in this small language is called a painter. A painter is a one-argument function, when called with a frame, “draws an image that is shifted and scaled to fit within a designated parallelogram-shaped frame”. Since it’s just a function, we can utilize closure and higher order function to combine simple painters to make complex painters. For more information, check out the aforementioned paper and section 2.2.4 of SICP.

Docs

There’s one difference between the implementation and the paper: beside a frame, a painter also needs a canvas context to decide in which canvas it should be drawn. A painter is actually a curried function; it should be called this way: painter(frame)(ctx).

The implementation provides the following functions:

Vectors

  • makeVect
  • addVect
  • subVect
  • scaleVect

Frames

  • makeFrame
  • makeRelativeFrame
  • frameCoordMap
  • unitSquare

Segments

  • makeSegment

Drawing Functions

  • drawLine
  • line

Primitive Painters

  • segmentsToPainter
  • vectsToPainter
  • svgPathToPainter
  • colorToPainter
  • imageToPainter

Built-in Painters

  • blank
  • fish
  • heart
  • black
  • grey

Higher Order Painters

  • transformPainter
  • over
  • beside
  • above
  • flipVert
  • flipHoriz
  • identity
  • rot
  • rot45
  • rot180
  • rot270
  • quartet

Testing

Start a web server in the root directory (for example, python -m http.server), and open test.html. For imageToPainter testing to work, you need to supply your own picture.

Credits

  1. It’s indebted to SICP (some code was directly translated from the book to JavaScript) and Racket’s SICP package (especially the matrix transformation part).
  2. The built-in fish painter’s bézier curve is copied from this repo which in turn is copied from this repo.