wasm-canvas
canvas.c File Reference
#include "canvas.h"
Include dependency graph for canvas.c:

Functions

HTMLCanvasElementcreateCanvas (char *id)
 
void freeCanvas (HTMLCanvasElement *canvas)
 

Detailed Description

Facilitates interaction with HTML5 Canvas elements in a similar manner to JavaScript via the DOM, but from C to be compiled with Emscripten.

Author
Alex Tyner

Function Documentation

◆ createCanvas()

HTMLCanvasElement* createCanvas ( char *  name)

Creates a struct containing state and OO-like behavior of an HTML canvas structured similarly to how it would be exposed in JavaScript. This struct should be instantiated using the createCanvas() function, and, when you're done using it, should be freed using the freeCanvas() function.

After freeing the canvas struct, the DOM element will still be present and active in HTML. It is possible to acquire an existing HTML canvas or reacquire a previously freed canvas by calling createCanvas() with its matching element id.

A typical use of this function might look like the following:

HTMLCanvas *canvas = createCanvas("myCanvas");
canvas->setHeight(canvas, 1080);
canvas->setWidth(canvas, 1920);
CanvasRenderingContext2D *ctx = canvas->getContext(canvas, "2d"); // only 2d is supported currently
ctx->fillRect(ctx, 50, 75, 100, 200);
freeCanvas(canvas);
// I've decided I want that canvas again
HTMLCanvas *sameOldCanvas = createCanvas("myCanvas");
int width = sameOldCanvas->getWidth(sameOldCanvas);
freeCanvas(sameOldCanvas);

◆ freeCanvas()

void freeCanvas ( HTMLCanvasElement canvas)

Frees the dynamically allocated HTMLCanvasElement and any dynamically allocated state as necessary. The DOM canvas element will still exist in HTML after freeing the struct.