Fork me on GitHub


A micro WebGL2 framework



Commented, full
Minified, full
(8.1kb, 2.6kb zipped)
Minified, lite
(5.8kb, 2.0kb zipped)
No built-in models or smooth shading

or load it in HTML:

<script src=//></script>


// Start the framework

// Set clear color ("#fff" by default)

// Set camera
// Settings: position (x, y, z), angle (rx, ry, rz), fov{x, y, z, rx, ry, rz, fov});

// Set light direction
W.light({x, y, z});

// Set ambient light's force (between 0 and 1)

// Create a group of objects: name, position, rotation{n, x, y, z, rx, ry, rz});

// Draw a built-in 3D object
// Settings: group, name, size, position, rotation, background, texture, smooth...
let settings = {g, n, size, x, y, z, rx, ry, rz, b, t, mix, s, ns, mode};

// Add and draw a custom 3D model (see the OBJ file loader below)
W.add("custom_model", { vertices, uv, indices });

// Move/resize a group or object: name, position, rotation, animation, delay (in ms)
W.move({n, size, x, y, z, rx, ry, rz, a}, delay);

// You can also use M to set a custom transformation matrix
W.move({n, M}, delay);

// Move camera / light: settings, animation, delay{x, y, z, rx, ry, rz, a}, delay);
W.light({x, y, z, a}, delay);

// Delete an element: name, delay
W.delete(n, delay);

Examples / Tutorial

1) Setup
First, create a HTML5 canvas
<canvas id=canvasElement>
and call
By default, the camera is placed at [0,0,0], looking at the -Z axis, with a 30 degrees FoV.
The scene contains an ambient light and a directional light pointing to the bottom: [0, -1, 0].
You can set the camera's position, rotation and FoV using{x, y, z, rx, ry, rz, fov})
You can set the directional light using
W.light({x, y, z})
and the ambient light leved using
You can change the background color using
(where c is a hex RGB/RGBA string).
No need to start a render loop, the canvas is redrawn after each screen refresh.

2) Draw built-in 3D models
The full version of the framework includes 5 built-in shapes:
(Billboards are planes that always face the camera, and can be used to show sprites or particles for example.)
The shapes settings can contain the following values:
{g, n, size, w, h, d, x, y, z, rx, ry, rz, b, t, mix, s, mode}
Groups can be created using{n, x, y, z, rx, ry, rz})
(or{n, M})
) and can be nested.

Edit on Codepen

3) Draw custom 3D models
Besides the built-in shapes, W can also import and display custom 3D models.
You'll need: Add the new model using
W.add("custom_shape", {vertices, uv, indices})
and draw it using
Custom models can be drawn with the same settings as the built-in ones (group, size, position, rotation, background, shading...).
Flat shading is applied by default, smooth shading is only available in the full version of the framework.
OBJ files can be converted into W custom models using the following online tool: OBJ2JS!

Edit on Codepen

4) Transformations and animation
The groups and 3D shapes can be moved and transformed using
W.move({n, size, x, y, z, rx, ry, rz, b, t, a}, delay)
If a
is provided, the transformation will be done after that delay in milliseconds. (0 by default)
If an
parameter is provided, the transformation will be animated. The animation will last for
milliseconds. (0 by default)

The camera can be moved and animated using{x, y, z, rx, ry, rz, a}, delay)
The directional light can be moved and animated with
W.light({x, y, z, a}, delay)

Edit on Codepen

5) Advanced techniques Edit on Codepen

Edit on Codepen

Edit on Codepen

Edit on Codepen

Related projects and demos:

2021-2022 - Made by xem, stas & kchplr