wasm-canvas
canvas.h File Reference

HTMLCanvasElement and CanvasRenderingContext2D C-DOM-JS-interaction. More...

#include <emscripten.h>
#include <string.h>
#include <stdlib.h>
Include dependency graph for canvas.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CanvasRenderingContext2D
 
struct  HTMLCanvasElement
 

Typedefs

typedef struct HTMLCanvasElement HTMLCanvasElement
 
typedef struct CanvasRenderingContext2D CanvasRenderingContext2D
 

Functions

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

Detailed Description

HTMLCanvasElement and CanvasRenderingContext2D C-DOM-JS-interaction.

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.