API Docs for: 2.0.0
Show:

Canvas Class

Extends Component
Module: canvas
Parent Module: BIMSURFER

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

Overview

Example

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

var viewer = new BIMSURFER.Viewer();

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

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

// Subscribe to Canvas resize events
canvas.on("resize", function(e) {
       var width = e.width;
       var height = e.height;
       var aspect = e.aspect;
       //...
    });

// 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;
       //...
    });

Methods

destroy

()

Destroys this component.

Removes this Component from its Viewer.

Fires a destroyed event on this Component.

error

(
  • message
)

Logs an error for this component to the JavaScript console.

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

Parameters:

  • message String

    The message to log

fire

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

Fires an event on this component.

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

Parameters:

  • event String

    The event type name

  • value Object

    The event

  • [forget=false] Boolean optional

    When true, does not retain for subsequent subscribers

log

(
  • message
)

Logs a console debugging message for this component.

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

Parameters:

  • message String

    The message to log

off

(
  • handle
)

Cancels an event subscription that was previously made with on or once.

Parameters:

  • handle String

    Subscription handle

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

    Publication event

  • callback Function

    Called when fresh data is available at 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 handled.

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

pick

(
  • canvasX
  • canvasY
  • [options]
)

Attempts to pick a GameObject at the given Canvas-space coordinates within the parent Viewer.

Ignores GameObject that are attached to either a Stage with Stage/pickable:property set false or a Modes with Modes/picking:property set false.

On success, will fire a picked event on this Canvas, along with a separate GameObject/picked:event event on the target GameObject.

Parameters:

  • canvasX Number

    X-axis Canvas coordinate.

  • canvasY Number

    Y-axis Canvas coordinate.

  • [options] optional

    Pick options.

    • [rayPick=false] Boolean optional

      Whether to perform a 3D ray-intersect pick.

warn

(
  • message
)

Logs a warning for this component to the JavaScript console.

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

Parameters:

  • message String

    The message to log

Properties

canvas

HTMLCanvasElement final

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

className

String final

JavaScript class name for this Component.

destroyed

Boolean

True as soon as this Component has been destroyed

id

String final

Unique ID for this Component within its parent Viewer.

Items in this map

Unknown

metadata

Object

Metadata on this component.

viewer

Viewer final

The Viewer that contains this Component.

Events

destroyed

Fired when this Component is destroyed.

picked

Fired whenever the pick method succeeds in picking a GameObject in the parent Viewer.

Event Payload:

  • objectId String

    The ID of the picked GameObject within the parent Viewer.

  • canvasX Number

    The X-axis Canvas coordinate that was picked.

  • canvasY Number

    The Y-axis Canvas coordinate that was picked.

resized

Fired whenever the canvas has resized

Event Payload:

  • width Number

    The new canvas width

  • height Number

    The new canvas height

  • aspect Number

    The new canvas aspect ratio

webglContextLost

Fired wheneber 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