Untitled

📰Getting Started | **Updates |** 📝 Guides | 🔢 API | ❓FAQ

Guides Overview

The drawing API is used to draw primitives. It can be used for debugging or as part of an experience.

var drawing = require('drawing');

Registration and Filtering

First, register a function with the drawing module with an identifier. You can apply a filter to limit which functions are rendered. The filter method will include every drawing method registered that starts with the specified string. Registered functions that have not been filtered out will be called every frame until unregistered.

drawing.register('foo',function(ctx) {
  //
});

drawing.register('foo.bar',function(ctx) {
  //
});

// render everything
drawing.filter('');

// render all functions that start with "foo"
drawing.filter('foo');

// render all functions that start with "foo.bar"
drawing.filter('foo.bar');

Context

Each registered function will be called with a drawing context. This context object has many useful properties for debugging or creative coding. Each function on the context returns the context so that calls may be chained together.

drawing.register('ex',function(ctx) {
  ctx.stroke(0, 0, 1)
     .line(vec3(0, 0, 0), vec3(1, 0, 0));
});

Matrices

Before drawing, it's important to understand the matrix stack. This stack is used to transform primitives that are drawn. The current matrix transformation with be applied to any subsequent drawing calls. Building a matrix sounds hard but it's been made quite simple by the translation, rotation, and scale methods. These methods immediately transform the current matrix.

Each time a registered callback is called, the stack will be cleared and the current matrix will be reset.

ctx
    .rotate(Math.PI, 0, 0)
    .translate(0, 0, 1)
    .translate(0, 1, 0)
    .scale(1, 2, 2);

3D Primitives

Draw a wire box.

var width = 10,
    height = 7,
    depth = 5,
    size = 20;

ctx.box(width, height, depth);
ctx.box(size);

Draw a unit sphere with configurable smoothness. Use matrices to scale. The smoothness parameter passed into sphere uses a subdivision method to construct the sphere geometry. In general, a value higher than 2 or 3 will never be necessary as the number of vertices doubles each time.

// default quality
ctx.sphere();

// higher quality
ctx.sphere(1);
ctx.sphere(2);
ctx.sphere(3);

Module Methods

register(category, callback)

Registers a callback for drawing.

unregister(callback)

Removes a callback for drawing.

Context Class Methods

stroke(color)

stroke(r, g, b)

stroke(r, g, b, a)

Sets the color that all primitives will be drawn with. Currently, the only supported property is line color.

line(from, to)

line(xFrom, yFrom, zFrom, xTo, yTo, zTo)

Draws a line.

lines(points)

Creates line segments form pairs of start and end points.

linestrip(points)

Connects a series of points. Instead of each line segment needing two points, each point draws a line to the next.

triangles(vertices, indices)

Draws a mesh of triangles. When drawing custom 3D shapes, it's best to use the triangles API rather than iterating and drawing triangles in JavaScript.

resetMatrix()

Make the existing matrix an identity matrix.

pushMatrix()

Push a new identity matrix onto the stack.

popMatrix()

Get rid of the current matrix and pop the previous one off the stack.

translate(to)

translate(x, y, z)

Applies translation to the matrix.

rotateX(x)

rotateY(y)

rotateZ(z)

rotate(x, y, x)

Applies rotation to the matrix.

scale(scalar)

scale(scale)

Apply scale to the matrix.

box(size)

box(width, height, depth)

Draws a box.

sphere()

sphere(smoothness)

Draws a unit sphere with configurable smoothness. Use matrices to scale.

octohedron()

Draws an octohedron.

dodecahedron()

Draws a dodecahedron.

Next: Elements

Sidebar Table of Contents


Untitled