March Docs

Canvas

Canvas — 2D drawing bindings for March compiled with --target js.

Wraps the browser's CanvasRenderingContext2D. Requires --target js; calling these functions from a native build panics.

Usage: match Dom.find("game-canvas") do None -> () Some(node) -> match Canvas.get_context(node) do None -> () Some(ctx) -> Canvas.set_fill_style(ctx, "#e74c3c") Canvas.fill_rect(ctx, 10.0, 10.0, 80.0, 40.0) end end

Functions

fnarcarc(ctx, x, y, radius, start_angle, end_angle) do canvas_arc(ctx, x, y, radius, start_angle, end_angle) end#

Add a circular arc centered at (x, y) with the given radius, from start_angle to end_angle (radians, clockwise). Use start_angle = 0.0, end_angle = 6.283185307179586 (2*pi) for a full circle.

fnbegin_pathbegin_path(ctx) do canvas_begin_path(ctx) end#

Start a new path, discarding any path built so far.

fnbezier_curve_tobezier_curve_to(ctx, cp1x, cp1y, cp2x, cp2y, x, y) do canvas_bezier_curve_to(ctx, cp1x, cp1y, cp2x, cp2y, x, y) end#

Add a cubic Bezier curve from the path's current point to (x, y), using (cp1x, cp1y) and (cp2x, cp2y) as control points.

fnclear_rectclear_rect(ctx, x, y, w, h) do canvas_clear_rect(ctx, x, y, w, h) end#

Clear the rectangle at (x, y) of size (w, h) to fully transparent.

fnclose_pathclose_path(ctx) do canvas_close_path(ctx) end#

Close the current path by drawing a straight line back to its start point.

fndraw_imagedraw_image(ctx, img, x, y) do canvas_draw_image(ctx, img, x, y) end#

Draw img at (x, y) using its natural size.

fndraw_image_scaleddraw_image_scaled(ctx, img, x, y, w, h) do canvas_draw_image_scaled(ctx, img, x, y, w, h) end#

Draw img at (x, y), scaled to (w, h).

fnfillfill(ctx) do canvas_fill(ctx) end#

Fill the current path with the current fill style.

fnfill_noise_circlefill_noise_circle(ctx, cx, cy, radius, alpha) do canvas_fill_noise_circle(ctx, cx, cy, radius, alpha) end#

Fill a circle centered at (cx, cy) with radius radius using a tileable, world-aligned value-noise texture (a soft cloudy/wispy look), faded to transparent at the edge. alpha caps the overall opacity. The noise tile is generated once and cached, so repeated calls are cheap.

fnfill_rectfill_rect(ctx, x, y, w, h) do canvas_fill_rect(ctx, x, y, w, h) end#

Fill the rectangle at (x, y) of size (w, h) with the current fill style.

fnfill_textfill_text(ctx, text, x, y) do canvas_fill_text(ctx, text, x, y) end#

Draw text filled with the current fill style, baseline at (x, y).

fnget_contextget_context(node) do canvas_get_context(node) end#

Get the 2D drawing context for a <canvas> element. Returns None if node is not a canvas element.

fnline_toline_to(ctx, x, y) do canvas_line_to(ctx, x, y) end#

Add a straight line from the path's current point to (x, y).

fnload_imageload_image(url) do canvas_load_image(url) end#

Load an image from url (asynchronously). Returns Err with a message if the image fails to load.

fnmove_tomove_to(ctx, x, y) do canvas_move_to(ctx, x, y) end#

Move the path's current point to (x, y) without drawing.

fnquadratic_curve_toquadratic_curve_to(ctx, cpx, cpy, x, y) do canvas_quadratic_curve_to(ctx, cpx, cpy, x, y) end#

Add a quadratic Bezier curve from the path's current point to (x, y), using (cpx, cpy) as the control point.

fnrestorerestore(ctx) do canvas_restore(ctx) end#

Pop the most recently saved drawing state, restoring transform and styles.

fnrotaterotate(ctx, angle) do canvas_rotate(ctx, angle) end#

Rotate subsequent drawing by angle radians, clockwise around the current origin.

fnsavesave(ctx) do canvas_save(ctx) end#

Push the current drawing state (transform, styles) onto a stack. Pair with restore.

fnscalescale(ctx, sx, sy) do canvas_scale(ctx, sx, sy) end#

Scale subsequent drawing by (sx, sy).

fnset_fill_styleset_fill_style(ctx, color) do canvas_set_fill_style(ctx, color) end#

Set the fill color/style used by fill_rect, fill, and fill_text (any CSS color string, e.g. \

fnset_fontset_font(ctx, font) do canvas_set_font(ctx, font) end#

Set the font used by fill_text/stroke_text (CSS font shorthand, e.g. \

fnset_global_alphaset_global_alpha(ctx, a) do canvas_set_global_alpha(ctx, a) end#

Set the global alpha (opacity) for subsequent drawing, from 0.0 (transparent) to 1.0 (opaque).

fnset_line_widthset_line_width(ctx, w) do canvas_set_line_width(ctx, w) end#

Set the line width (in canvas units) used by stroke_rect and stroke.

fnset_stroke_styleset_stroke_style(ctx, color) do canvas_set_stroke_style(ctx, color) end#

Set the stroke color/style used by stroke_rect, stroke, and stroke_text.

fnset_text_alignset_text_align(ctx, align) do canvas_set_text_align(ctx, align) end#

Set horizontal text alignment for fill_text/stroke_text: \

fnstrokestroke(ctx) do canvas_stroke(ctx) end#

Stroke the current path's outline with the current stroke style.

fnstroke_rectstroke_rect(ctx, x, y, w, h) do canvas_stroke_rect(ctx, x, y, w, h) end#

Stroke the outline of the rectangle at (x, y) of size (w, h) with the current stroke style.

fnstroke_textstroke_text(ctx, text, x, y) do canvas_stroke_text(ctx, text, x, y) end#

Draw text outlined with the current stroke style, baseline at (x, y).

fntranslatetranslate(ctx, x, y) do canvas_translate(ctx, x, y) end#

Translate the drawing origin by (x, y).