wchntGraphics

wchntGraphics is the portable drawing surface available to Target code. It is what your %step (or %init) uses to draw to the window or canvas, and it exposes the same API on both the OpenFL and browser-canvas hosts.

It is deliberately small: just enough to draw rectangles, circles, outlines, and text, so that Schema / Construction / Methods stay platform-independent and only the Target draws.

The v1 API

Method What it does
clear() Fill the whole surface with the background colour.
beginFill(color) Set the fill colour used by the following shapes.
endFill() Finish the current fill.
lineStyle(thickness, color) Switch to outlining: the next drawRect is stroked, not filled.
drawRect(x, y, width, height) Draw a rectangle (filled, or outlined if lineStyle was set).
drawCircle(x, y, radius) Draw a filled circle centred at (x, y).
fillText(text, x, y) Draw text at (x, y).

Colours are 24-bit integers written in hex: 0xRRGGBB. For example 0xf2f2f2 is a light grey and 0x00ff00 is green.

An optional alpha is accepted by beginFill and lineStyle on the OpenFL host (beginFill(color, alpha)); the browser-canvas harness currently ignores it.

Typical usage

Clear, then fill the background and draw a ball:

wchntGraphics.clear();
wchntGraphics.beginFill(0x2a2a2a);
wchntGraphics.drawRect(0, 0, 800, 600);
wchntGraphics.endFill();
wchntGraphics.beginFill(0xf2f2f2);
wchntGraphics.drawCircle(b.x, b.y, b.rad);
wchntGraphics.endFill();

Outline a rectangle instead of filling it:

wchntGraphics.lineStyle(2, 0x00ff00);
wchntGraphics.drawRect(r.x, r.y, r.width, r.height);

Paths (browser canvas only)

The browser harness additionally provides moveTo(x, y) and lineTo(x, y) so you can draw arbitrary polygons. Follow them with endFill() to fill the path:

wchntGraphics.beginFill(0x00ff00);
wchntGraphics.moveTo(0, 0).lineTo(50, 0).lineTo(25, 40).lineTo(0, 0);
wchntGraphics.endFill();

moveTo / lineTo are not part of the OpenFL wchntGraphics wrapper yet. On OpenFL, use a @Graphics/g parameter in ## Target Methods and pass the raw graphics object instead — see below.

Passing wchntGraphics to Target Methods

You can hand wchntGraphics into a @Graphics/g method and call it there. The calls chain in source; the compiler unrolls the Void chain for you:

Shape::draw : Void = { @Graphics/g |
  g.beginFill(15316448).drawCircle(x, y, radius).endFill()
}

Then, from Target:

for (var s of assemblage.shapes) {
    s.draw(wchntGraphics);
}

The canonical examples show all of this in action: bounce (fill + circle), pollution (outline + text), and shapes (paths via @Graphics/g).