flix

0.81.0

Canvas

eff CanvasSource

Drawing, as an effect.

Sketch and game code calls these operations directly, so drawing code reads the way it would in Processing. What makes that safe is that the effect has two interpretations: Canvas.runWithCollector turns the calls into data with no window, and the runtime's handler paints them onto a real sketch. Rendering is therefore testable without a display.

Operations

def clear(c: Color): Unit \ Canvas§ Source

Fills the whole canvas with c, erasing the previous frame.

def dimensions(): (Int32, Int32) \ Canvas§ Source

Returns the canvas size as (width, height).

def ellipse(x: Float32, y: Float32, w: Float32, h: Float32): Unit \ Canvas§ Source

Draws an ellipse centred on (x, y).

def fill(c: Color): Unit \ Canvas§ Source

Sets the fill colour used by subsequent shapes and text.

def fillAlpha(c: Color, alpha: Int32): Unit \ Canvas§ Source

Sets a translucent fill, with alpha from 0 (invisible) to 255 (opaque).

Exists so an overlay can dim what is behind it instead of hiding it -- the attract screen draws its title over a live game, and a solid band would cut the formation in half.

def rect(x: Float32, y: Float32, w: Float32, h: Float32): Unit \ Canvas§ Source

Draws a rectangle with its top-left corner at (x, y).

def text(s: String, x: Float32, y: Float32): Unit \ Canvas§ Source

Draws s with its baseline starting at (x, y).

def textSize(px: Float32): Unit \ Canvas§ Source

Sets the text size in pixels.

Definitions

def commands(dims: (Int32, Int32), f: Unit -> Unit \ ef): List[DrawCmd] \ ef - Canvas§ Source

Runs f and returns only the draw commands it produced.

def handleWithCollector(dims: (Int32, Int32), f: a -> b \ ef): CmdCollector[a, b, ef]§ Source

Interprets Canvas by recording every drawing operation instead of performing it.

Pure: the accumulator lives in a region that is fully discharged here, so this needs no IO and opens no window. This is what the rendering tests use.

The handle/runWith pair follows the stdlib's companion-module convention -- see Logger.flix, which offers seven interpretations of one effect this way. Adding an eighth interpretation of Canvas means adding a function here, not editing the runtime.

def paint(cmds: List[DrawCmd]): Unit \ Canvas§ Source

Replays a list of draw commands through the Canvas effect.

This is the bridge from a pure render : world -> List[DrawCmd] to whichever handler is currently installed.

def runWithCollector(dims: (Int32, Int32), f: Unit -> a \ ef): (a, List[DrawCmd]) \ ef - Canvas§ Source

Runs f, returning its result together with the draw commands it produced.

def runWithNoOp(dims: (Int32, Int32), f: Unit -> a \ ef): a \ ef - Canvas§ Source

Interprets Canvas by discarding every drawing operation.

Deliberately not installed as a @DefaultHandler: with a default in place, a test that forgot to choose an interpretation would compile and silently assert on a frame that was never drawn. Leaving Canvas without one keeps that a type error.