Canvas3D:
Filter:
Canvas3D/Classes (extension) | GUI > 3D

Canvas3D
ExtensionExtension

3D vector graphics canvas

Description

Canvas3D provides a simple wire-frame 3D vector graphics canvas. It displays a list of Canvas3DItems, and can be transformed and animated.

Class Methods

.new

Create a new canvas view.

Arguments:

parent

Parent View

bounds

A Rect describing the position and size of this view inside the parent.

Returns:

A new Canvas3D instance.

Matrix transforms

The following methods are used to create and apply matrix transforms, for translating, scaling and rotating.

.mIdentity

Create an identity matrix.

.mTranslate

Create a matrix for translation.

Arguments:

tx

X translation

ty

Y translation

tz

Z translation

.mScale

Create a matrix for scaling.

Arguments:

sX

X scaling

sY

Y scaling

sZ

Z scaling

.mRotateX

Create a matrix for rotating around X axis.

Arguments:

ax

X rotation

.mRotateY

Create a matrix for rotating around Y axis.

Arguments:

ay

Y rotation

.mRotateZ

Create a matrix for rotating around Z axis.

Arguments:

az

Z rotation

.matrixMatrixMul

Multiply a matrix with another matrix.

Returns:

A matrix.

.vectorMatrixMul

Multiply a vector with a matrix.

Arguments:

vector

A 3-element Array describing a 3D vector: [x,y,z]

matrix

A matrix

Returns:

A vector.

Instance Methods

Managing items

.items

Get/set the array of items in this canvas. The items should be instances of Canvas3DItem

.add

Add an item to the canvas.

Arguments:

item

An instance of Canvas3DItem

.remove

Remove an item from the canvas.

Arguments:

item

An instance of Canvas3DItem

Global settings and transforms

.background

Set the background color.

Arguments:

b

A Color

.scale

Get/set the global pixel scale.

.perspective

Get/set the perspective factor. Useful range is 0.0 (no perspective) to below 1.0.

.distance

Get/set the view distance. Useful range is around 2.0.

.transforms

Get/set an array of transformation matrices to be applied to the whole canvas.

Displaying

.refresh

Update the canvas.

.preDrawFunc

Function to evaluate at the beginning of the draw function.

.postDrawFunc

Function to evaluate at the end of the draw function.

.animate

Start animation.

Arguments:

rate

Animation rate in frames per second. If nil, stop animation.

func

Animation callback. This function is called once for each frame, and is passed the frame count as argument.

Discussion:

See Animated canvas example below.

Examples

Hello cube

First a very simple example. Set up a canvas and add a slightly rotated cube.

Rotate canvas with mouse

In this example, we add a cube and a custom spiral-shape, and set up a mouseMoveAction to rotate the canvas.

Animated canvas

A more advanced example, with three items and animation where we spin both the canvas and one of the items.

Drawing realtime sound

Another more advanced example.

spinning toruses

examples by redFrik

knots

examples by jhr

demoscene

examples by kymatica