Everything done in the render function is done to a clean, transparent "buffer" canvas. Nothing is written to the screen until the commit() function is called. When that happens, the contents of the temporary buffer canvas is added to the screen canvas and the buffer is cleared. It is then possible to draw new stuff and commit that as well.
So, while everything is first drawn unto a clean canvas, in the end it's composited with the previous frame. If you want to clear the screen, you need to explicitly draw e.g. a black rectangle covering everything.
Unless otherwise noted, all (x,y) coordinates are assumed to be in the space of (0,0) to (1,1) where (0,0) is the top left corner and (1,1) is the bottom right corner.
A whole bunch of functions are imported into the scope of the render function and made available. Here's a brief rundown.
This one has already been mentioned above. It clears the buffer canvas and draws the contents onto the screen. If keepBuffer is true, the buffer is not cleared.
Draws a black rectangle covering everything, amount is a number in the range 0-1 indicating the opacity of the rectangle.
Sets the quality (internal resolution) of the canvas, q is a number in the range 0-1 (non-zero) where 0 is lowest quality and 1 is 1:1 resolution. Unless keepImage is true, both buffer and screen will be cleared when setting the quality.
Clears a rectangular region of the buffer canvas.
Following is a number of drawing functions which all can be used to draw simple shapes. They all have two versions, one for drawing a single shape (rect, circle, etc) and one that allows you to feed it an array of shapes to draw. The shapes are then all drawn with the same style. The style parameters all work the same way for all the functions, if fillStyle is present the shape will be filled with the given color, if strokeStyle (and optionally strokeWidth) is present, the shape's border will be drawn.
Parameters should be self-explanatory.
The rects array contains objects of the form {x: x, y: y, w: w, h: h}.
Again, parameters should be self-explanatory.
The circles array contains objects of the form {x: x, y: y, radius: radius}.
Draws an arc. startAngle and endAngle are in radians.
The arcs array contains objects of the form {x: x, y: y, radius: radius, startAngle: startAngle, endAngle: endAngle}.
Draws a path of arbitrary points. The argument points is an array of 2-element arrays where each 2-element array is a point on the path [x, y]. E.g. [[0.1,0.1], [0.5,0.1], [0.5,0.5]].
The paths array contains points arrays as described above.
Draws the image specified by the src parameter at position x,y with the dimensions w x h. Optionally rotated by angle (in radians).
Zooms the image by the (non-negative) value factor. A value of 1 indicates no zoom, 2 zooms to twice the size, etc.
Stretches the image by a factor of sx along the horizontal axis and sy along the vertical axis. By default the center of the screen (0.5, 0.5) is the origin of the stretching, unless an alternative point is given in the cx, cy parameters.
Translates the image.
Rotates the image. Angle is given in radians.
Simply a combination of rotation and zoom.
This function takes another function, fn, as its first parameter. The function fn is applied to all points of a grid of gridX * gridY points and this altered grid is then used to deform the current buffer image.
It must follow the signature:
function fn(radius, angle, x, y) {
return { ... }
}
The radius parameter is the distance of the point from the center of the screen.
The angle parameter is the angle (in radians) of the point relative to the horizontal line going through the center of the screen.
The x and y are the coordinates of the point, with x=0, y=0 being the top left corner and x=1, y=1 being the bottom right corner.
The function must return an object with zero or more of the following properties:
Converts an RGB color to HSL. Returns an object of the form {h:h, s:s, l:l}.
Converts an HSL color to RGB. Returns an object of the form {r:r, g:g, b:b}.
Converts an HSV color to RGB. Returns an object of the form {r:r, g:g, b:b}.