Canvas Class
A Canvas manages a Viewer's HTML canvas and its WebGL context.
Overview
- Each Viewer provides a Canvas as a read-only property on itself.
- When a Viewer 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 resized event whenever the HTMLCanvasElement resizes.
- A Canvas is responsible for obtaining a WebGL context from the HTMLCanvasElement.
- A Canvas also fires a webglContextLost event when the WebGL context is lost, and a webglContextRestored when it is restored again.
- The various components within the parent Viewer will transparently recover on the webglContextRestored event.
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;
//...
});
Item Index
Methods
destroy
()
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
StringThe 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
StringThe event type name
-
value
ObjectThe event
-
[forget=false]
Boolean optionalWhen 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
StringThe message to log
off
-
handle
Parameters:
-
handle
StringSubscription handle
on
-
event
-
callback
-
[scope=this]
Subscribes to an event on this component.
The callback is be called with this component as scope.
Parameters:
-
event
StringPublication event
-
callback
FunctionCalled when fresh data is available at the event
-
[scope=this]
Object optionalScope for the callback
Returns:
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
StringData event to listen to
-
callback
Function(data)Called when fresh data is available at the event
-
[scope=this]
Object optionalScope 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
NumberX-axis Canvas coordinate.
-
canvasY
NumberY-axis Canvas coordinate.
-
[options]
optionalPick options.
-
[rayPick=false]
Boolean optionalWhether 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
StringThe 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
Items in this map
Unknown
metadata
Object
Metadata on this component.
Events
destroyed
Fired when this Component is destroyed.
picked
Event Payload:
-
objectId
StringThe ID of the picked GameObject within the parent Viewer.
-
canvasX
NumberThe X-axis Canvas coordinate that was picked.
-
canvasY
NumberThe Y-axis Canvas coordinate that was picked.
resized
Fired whenever the canvas has resized
Event Payload:
-
width
NumberThe new canvas width
-
height
NumberThe new canvas height
-
aspect
NumberThe 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
ObjectThe WebGL context object