API Docs for:

Canvas

A Canvas manages a Scene's HTML canvas and its WebGL context.

Overview

  • Each Scene provides a Canvas as a read-only property on itself.
  • When a Scene is configured with the ID of an existing HTMLCanvasElement, then the Canvas will bind to that, otherwise the Canvas will automatically create its own.
  • A Canvas will fire a boundary event whenever the HTMLCanvasElement resizes.
  • A Canvas is responsible for obtaining a WebGL context from the HTMLCanvasElement.
  • A Canvas also fires a Canvas/webglContextLost:event event when the WebGL context is lost, and a webglContextRestored when it is restored again.
  • The various components within the parent Scene will transparently recover on the webglContextRestored event.

A Canvas also has

  • a Progress, which shows a busy progress when a Model is loading, or when directed by application logic, and

Examples

Usage

In the example below, we're creating a Scene without specifying an HTML canvas element for it. This causes the Scene's Canvas component to create its own default element within the page. Then we subscribe to various events fired by that Canvas component.

var scene = new xeogl.Scene();

// Get the Canvas off the Scene
// Since we did not configure the Scene with the ID of a DOM canvas element,
// the Canvas will create its own canvas element in the DOM
var canvas = scene.canvas;

// Get the WebGL context off the Canvas
var gl = canvas.gl;

// Subscribe to Canvas size updates
canvas.on("boundary", function(boundary) {
   //...
});

// Subscribe to WebGL context loss events on the Canvas
canvas.on("webglContextLost", function() {
       //...
    });

// Subscribe to WebGL context restored events on the Canvas
canvas.on("webglContextRestored", function(gl) {
       var newContext = gl;
       //...
    });

When we want to bind the Canvas to an existing HTML canvas element, configure the Scene with the ID of the element, like this:

// Create a Scene, this time configuring it with the
// ID of an existing DOM canvas element
var scene = new xeogl.Scene({
         canvasId: "myCanvas"
    });

// ..and the rest of this example can be the same as the previous example.

The Scene will attempt to get use WebGL 2, or fall back on WebGL 1 if that's absent. If you just want WebGL 1, disable WebGL 2 like so:

var scene = new xeogl.Scene({
         canvasId: "myCanvas",
         webgl2 : true
    });

// ..and the rest of this example can be the same as the previous examples.

Methods

create

(
  • [cfg]
)

Convenience method for creating a Component within this Component's Scene.

The method is given a component configuration, like so:

var material = myComponent.create({
     type: "xeogl.PhongMaterial",
     diffuse: [1,0,0],
     specular: [1,1,0]
}, "myMaterial");

Parameters:

  • [cfg] optional

    Configuration for the component instance.

Returns:

:

destroy

()

Destroys this component.

Fires a destroyed event on this Component.

Automatically disassociates this component from other components, causing them to fall back on any defaults that this component overrode on them.

TODO: describe effect with respect to #create

error

(
  • message
)

Logs an error for this component to the JavaScript console.

The console message will have this format: [ERROR] [<component type> =<component id>: <message>

Also fires the message as an error event on the parent Scene.

Parameters:

  • message String

    The message to log

fire

(
  • event
  • value
  • [forget=false]
)

Fires an event on this component.

Notifies existing subscribers to the event, optionally retains the event to give to any subsequent notifications on the event as they are made.

Parameters:

  • event String

    The event type name

  • value Object

    The event parameters

  • [forget=false] Boolean optional

    When true, does not retain for subsequent subscribers

getSnapshot

(
  • [params]
  • [ok]
)
String

Returns a snapshot of this Canvas as a Base64-encoded image.

When a callback is given, this method will capture the snapshot asynchronously, on the next animation frame, and return it via the callback.

When no callback is given, this method captures and returns the snapshot immediately. Note that is only possible when you have configured the Canvas's Scene to preserve the WebGL drawing buffer, which has a performance overhead.

Usage:

// Get snapshot asynchronously
myScene.canvas.getSnapshot({
        width: 500, // Defaults to size of canvas
        height: 500,
        format: "png" // Options are "jpeg" (default), "png" and "bmp"
    }, function(imageDataURL) {
        imageElement.src = imageDataURL;
    });

// Get snapshot synchronously, requires that Scene be
// configured with preserveDrawingBuffer; true
imageElement.src = myScene.canvas.getSnapshot({
        width: 500,
        height: 500,
        format: "png"
    });

Parameters:

  • [params] optional

    Capture options.

    • [width] Number optional

      Desired width of result in pixels - defaults to width of canvas.

    • [height] Number optional

      Desired height of result in pixels - defaults to height of canvas.

    • [format="jpeg"] String optional

      Desired format; "jpeg", "png" or "bmp".

  • [ok] Function optional

    Callback to return the image data when taking a snapshot asynchronously.

Returns:

String:

String-encoded image data when taking the snapshot synchronously. Returns null when the ok callback is given.

hasSubs

(
  • event
)
Boolean

Returns true if there are any subscribers to the given event on this component.

Parameters:

  • event String

    The event

Returns:

Boolean:

True if there are any subscribers to the given event on this component.

isType

(
  • type
)
Boolean

Tests if this component is of the given type, or is a subclass of the given type.

The type may be given as either a string or a component constructor.

This method works by walking up the inheritance type chain, which this component provides in property Component/superTypes:property, returning true as soon as one of the type strings in the chain matches the given type, of false if none match.

Examples:

var myRotate = new xeogl.Rotate({ ... });

myRotate.isType(xeogl.Component); // Returns true for all xeogl components
myRotate.isType("xeogl.Component"); // Returns true for all xeogl components
myRotate.isType(xeogl.Rotate); // Returns true
myRotate.isType(xeogl.Transform); // Returns true
myRotate.isType("xeogl.Transform"); // Returns true
myRotate.isType(xeogl.Mesh); // Returns false, because xeogl.Rotate does not (even indirectly) extend xeogl.Mesh

Parameters:

  • type String | Function

    Component type to compare with, eg "xeogl.PhongMaterial", or a xeogl component constructor.

Returns:

Boolean:

True if this component is of given type or is subclass of the given type.

log

(
  • message
)

Logs a console debugging message for this component.

The console message will have this format: [LOG] [<component type> <component id>: <message>

Also fires the message as a log event on the parent Scene.

Parameters:

  • message String

    The message to log

off

(
  • subId
)

Cancels an event subscription that was previously made with Component#on() or Component#once().

Parameters:

  • subId String

    Publication subId

on

(
  • event
  • callback
  • [scope=this]
)
String

Subscribes to an event on this component.

The callback is be called with this component as scope.

Parameters:

  • event String

    The event

  • callback Function

    Called fired on the event

  • [scope=this] Object optional

    Scope for the callback

Returns:

String:

Handle to the subscription, which may be used to unsubscribe with {@link #off}.

once

(
  • event
  • callback
  • [scope=this]
)

Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd.

This is equivalent to calling Component#on(), and then calling Component#off() inside the callback function.

Parameters:

  • event String

    Data event to listen to

  • callback Function(data)

    Called when fresh data is available at the event

  • [scope=this] Object optional

    Scope for the callback

warn

(
  • message
)

Logs a warning for this component to the JavaScript console.

The console message will have this format: [WARN] [<component type> =<component id>: <message>

Also fires the message as a warn event on the parent Scene.

Parameters:

  • message String

    The message to log

Properties

backgroundColor

Float32Array

A background color for the canvas. This is overridden by backgroundImage.

You can set this to a new color at any time.

Default: null

backgroundImage

String

URL of a background image for the canvas. This is overrided by Canvas/backgroundColor/property.

You can set this to a new file path at any time.

boundary

Array of Number final

Boundary of the Canvas in absolute browser window coordinates.

Usage:

var boundary = myScene.canvas.boundary;

var xmin = boundary[0];
var ymin = boundary[1];
var width = boundary[2];
var height = boundary[3];

canvas

HTMLCanvasElement final

The HTML canvas. When the Viewer was configured with the ID of an existing canvas within the DOM, then this property will be that element, otherwise it will be a full-page canvas that this Canvas has created by default, with a z-index of -10000.

destroyed

Boolean

True as soon as this Component has been destroyed

gl

WebGLRenderingContext final

The WebGL rendering context.

id

String final

Unique ID for this Component within its parent Scene.

metadata

Object

Arbitrary, user-defined metadata on this component.

model

Model final

The Model which contains this Component, if any.

Will be null if this Component is not in a Model.

scene

Scene final

The parent Scene that contains this Component.

spinner

Spinner final

The busy Spinner for this Canvas.

transparent

Boolean final

Indicates whether this Canvas is transparent.

Default: {false}

type

String final

JavaScript class name for this Component.

For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.

webgl2

Boolean final

True when WebGL 2 support is enabled.

Events

boundary

Fired whenever this Canvas's boundary property changes.

Event Payload:

  • value Object

    The property's new value

destroyed

Fired when this Component is destroyed.

webglContextFailed

Fired whenever the canvas failed to get a WebGL context, which probably means that WebGL is either unsupported or has been disabled.

webglcontextlost

Fired whenever the WebGL context has been lost

webglContextRestored

Fired whenever the WebGL context has been restored again after having previously being lost

Event Payload:

  • value Object

    The WebGL context object