March Docs

Arcade.Canvas

Types

typeFillRuleFillRule = RuleNonZero | RuleEvenOdd#

The fill and clip winding rule: Holed is even-odd, every other shape non-zero.

typeColourStopColourStop = { stop_at : Float, stop_css : String }#

A gradient stop with its colour already in CSS.

typeCanvasCallCanvasCall =#

One CanvasRenderingContext2D call. Strings are CSS values passed through. CvArc(x, y, r, sweep) is arc(x, y, r, 0, sweep). CvRadialStyle sets fillStyle to createRadialGradient(x0, y0, r0, x1, y1, r1) with its stops. CvTextCentred sets textAlign center and textBaseline middle.

typePaintingPainting = {#

The frame being built: the calls so far, newest first, and how many clips are open (each one a save the frame must restore).

Functions

fncallscalls(n : Arcade.Numerics.Numerics, dpr : Float, width : Float, height : Float, ops : List(Arcade.Draw.DrawOp)) : List(CanvasCall) needs ()#

The calls that paint ops on a canvas of width by height CSS pixels at dpr device pixels per CSS pixel. A PopClip with no clip open restores nothing, and clips left open are restored at the end, so the context's state stack is balanced after every frame. A full disc sweeps n.tau.

fnframed_byframed_by(dpr : Float, f : Arcade.Draw.Affine) : CanvasCall needs ()#

fn rule(sh : Arcade.Draw.Shape) : FillRule needs () do match sh do Holed(_, _) -> RuleEvenOdd Poly(_) -> RuleNonZero Polys(_) -> RuleNonZero Disc(_, _) -> RuleNonZero Outline(_) -> RuleNonZero end end

-- ---- Paths ---- pfn path(p : Painting, sh : Arcade.Draw.Shape) : Painting needs () do let begun = push(p, CvBeginPath) match sh do Poly(ps) -> ring(begun, ps) Holed(outer, inner) -> ring(ring(begun, outer), inner) Polys(rings) -> List.fold_left(rings, begun, ring) Disc(c, r) -> push(begun, CvArc(c.x, c.y, Arcade.Num.max_float(0.0, r), p.sweep)) Outline(steps) -> List.fold_left(steps, begun, path_step) end end

pfn ring(p : Painting, ps : List(Arcade.Vec2.Vec2)) : Painting needs () do match ps do Nil -> p Cons(first, rest) -> do let started = push(p, CvMoveTo(first.x, first.y)) push(List.fold_left(rest, started, line_to), CvClosePath) end end end

pfn line_to(p : Painting, v : Arcade.Vec2.Vec2) : Painting needs () do push(p, CvLineTo(v.x, v.y)) end

pfn path_step(p : Painting, s : Arcade.Draw.PathStep) : Painting needs () do match s do MoveTo(a) -> push(p, CvMoveTo(a.x, a.y)) LineTo(a) -> push(p, CvLineTo(a.x, a.y)) QuadTo(a, b) -> push(p, CvQuadTo(a.x, a.y, b.x, b.y)) CubicTo(a, b, c) -> push(p, CvCubicTo(a.x, a.y, b.x, b.y, c.x, c.y)) ClosePath -> push(p, CvClosePath) end end

-- ---- Fills, strokes and lettering ---- -- A radial paint's geometry is in its frame: the path is already built in -- pixels, so the frame is set after it, for the gradient alone. pfn fill(p : Painting, sh : Arcade.Draw.Shape, paint : Arcade.Draw.Paint) : Painting needs () do match paint do Flat(c) -> push_all(p, [CvFillStyle(Arcade.Draw.css(c)), CvFill(rule(sh))]) RadialPaint(g) -> do let framed = push(push(p, CvSave), framed_by(p.dpr, g.frame)) push_all(framed, [CvRadialStyle(g.inner.x, g.inner.y, g.inner_radius, g.outer.x, g.outer.y, g.outer_radius, List.map(g.stops, colour_stop)), CvFill(rule(sh)), CvRestore]) end end end

pfn colour_stop(s : Arcade.Draw.GradientStop) : ColourStop needs () do { stop_at: s.offset, stop_css: Arcade.Draw.css(s.colour) } end