This module offers a convenient way to render ThreeJS objects onto a WebGL canvas, with built-in interaction capabilities:
- hover/click events
- tooltips
- camera movement with animated transitions
- trackball, orbit or fly controls
All the renderer/scene/camera scaffolding is already included and any instance of Object3D can be rendered with minimal setup.
import ThreeRenderObjects from 'three-render-objects';
or using a script tag
<script src="//unpkg.com/three-render-objects"></script>
then
const myCanvas = ThreeRenderObjects();
myCanvas(<myDOMElement>)
.objects(<myData>);
ThreeRenderObjects({ configOptions })(<domElement>)
Config options |
Description |
Default |
controlType: str |
Which type of control to use to control the camera. Choice between trackball, orbit or fly. |
trackball |
rendererConfig: object |
Configuration parameters to pass to the ThreeJS WebGLRenderer constructor. |
{ antialias: true, alpha: true } |
extraRenderers: array |
If you wish to include objects that require a dedicated renderer besides WebGL , such as CSS3DRenderer, include in this array those extra renderer instances. |
[] |
waitForLoadComplete: boolean |
Whether to wait until all the asynchronous loading operations are finished (such as the background image) before rendering the objects in the scene for the first time. |
true |
Method |
Description |
Default |
objects([array]) |
Getter/setter for the list of objects to render. Each object should be an instance of Object3D. |
[] |
Method |
Description |
Default |
width([px]) |
Getter/setter for the canvas width. |
<window width> |
height([px]) |
Getter/setter for the canvas height. |
<window height> |
skyRadius([number]) |
Radius of the sphere that bounds the scene, in GL units. |
50000 |
backgroundColor([str]) |
Getter/setter for the canvas background color. |
#000011 |
backgroundImageUrl([url]) |
Getter/setter for the URL of the image to be used as scene background. If no image is provided, the background color is shown instead. |
null |
onBackgroundImageLoaded([fn]) |
Callback function triggered when the background image has finished loading asynchronously and is rendered on the scene. |
|
showNavInfo([boolean]) |
Getter/setter for whether to show the navigation controls footer info. |
true |
Method |
Description |
Default |
tick() |
Re-render all the objects on the canvas. Essentially this method should be called at every frame, and can be used to control the animation ticks. |
|
cameraPosition([{x,y,z}], [lookAt], [ms]) |
Getter/setter for the camera position, in terms of x , y , z coordinates. Each of the coordinates is optional, allowing for motion in just some dimensions. The optional second argument can be used to define the direction that the camera should aim at, in terms of an {x,y,z} point in the 3D space at the distance of 1000 away from the camera. The 3rd optional argument defines the duration of the transition (in ms) to animate the camera motion. A value of 0 (default) moves the camera immediately to the final position. |
By default the camera will face the center of the graph at a z distance of 1000 . |
zoomToFit([ms], [px], [objFilterFn]) |
Automatically moves the camera so that all of the objects in the scene become visible within its field of view, while aiming at the scene center (0,0,0). If no objects are found no action is taken. It accepts three optional arguments: the first defines the duration of the transition (in ms) to animate the camera motion (default: 0ms). The second argument is the amount of padding (in px) between the edge of the canvas and the outermost object position (default: 10px). The third argument specifies a custom object filter: obj => <boolean> , which should return a truthy value if the object is to be included. This can be useful for focusing on a portion of the scene. |
(0, 10, obj => true) |
fitToBbox(bbox, [ms], [px], [objFilterFn]) |
Automatically moves the camera to fit the specified bounding box within its field of view, while aiming at the scene center (0,0,0). The bounding box should follow the syntax { x: [<num>, <num>], y: [<num>, <num>], z: [<num>, <num>] } . If no bounding box is specified no action is taken. It accepts two optional arguments: the first defines the duration of the transition (in ms) to animate the camera motion (default: 0ms). The second argument is the amount of padding (in px) between the edge of the canvas and the outermost object position (default: 10px). |
(0, 10) |
postProcessingComposer() |
Access the post-processing composer. Use this to add post-processing rendering effects to the scene. By default the composer has a single pass (RenderPass) that directly renders the scene without any effects. |
|
renderer() |
Access the WebGL renderer object. |
|
camera() |
Access the perspective camera object. |
|
scene() |
Access the Scene object. |
|
controls() |
Access the camera controls object. |
|
Method |
Description |
Default |
onClick(fn) |
Callback function for object clicks with left mouse button. The object (or null if there's no object under the mouse line of sight), the event object and the coordinates of the intersecting point in the object's surface are included as arguments onClick(object, event, { x, y, z }) . |
- |
onRightClick(fn) |
Callback function for object right-clicks. The object (or null if there's no object under the mouse line of sight), the event object and the coordinates of the intersecting point in the object's surface are included as arguments onRightClick(object, event, { x, y, z }) . |
- |
onHover(fn) |
Callback function for object mouse over events. The object (or null if there's no object under the mouse line of sight) is included as the first argument, and the previous hovered object (or null ) as second argument: onHover(obj, prevObj) . |
- |
hoverOrderComparator([fn]) |
Getter/setter for the comparator function to use when hovering over multiple objects under the same line of sight. This function can be used to prioritize hovering some objects over others. |
By default, hovering priority is based solely on camera proximity (closest object wins). |
hoverFilter([fn]) |
Getter/setter for the filter function that defines whether an object is eligible for hovering and other interactions. This function receives an object as sole argument and should return a boolean value |
() => true |
lineHoverPrecision([int]) |
Getter/setter for the precision to use when detecting hover events over Line objects. |
1 |
tooltipContent([str or fn]) |
Object accessor function or attribute for label (shown in tooltip). Supports plain text or HTML content. |
|
enablePointerInteraction([boolean]) |
Getter/setter for whether to enable the mouse tracking events. This activates an internal tracker of the canvas mouse position and enables the functionality of object hover/click and tooltip labels, at the cost of performance. If you're looking for maximum gain in your render performance it's recommended to switch off this property. |
true |
hoverDuringDrag([boolean]) |
Getter/setter for whether to trigger hover events while using the controls via pointer dragging. |
false |
clickAfterDrag([boolean]) |
Getter/setter for whether to trigger a click event after dragging using the controls. |
false |
getPointerPos() |
Getter for the current position of the pointer relative to the viewport, in { x, y } px coordinates. |
|
Method |
Description |
getBbox([objFilterFn]) |
Returns the current bounding box of the objects in the scene, formatted as { x: [<num>, <num>], y: [<num>, <num>], z: [<num>, <num>] } . If no objects are found, returns null . Accepts an optional argument to define a custom object filter: object => <boolean> , which should return a truthy value if the object is to be included. This can be useful to calculate the bounding box of a portion of the scene. |
getScreenCoords(x, y, z) |
Utility method to translate 3D coordinates to the viewport domain. Given a set of x ,y ,z coordinates, returns the current equivalent {x, y} in viewport coordinates. |
getSceneCoords(x, y, distance) |
Utility method to translate viewport distance coordinates to the scene 3D domain. Given a set of x ,y viewport coordinates and distance from the camera, returns the current equivalent {x, y, z} in 3D scene coordinates. If no distance is provided, defaults to 0 . |
intersectingObjects(x, y) |
Utility method to retrieve the list of objects under the line of sight of the given viewport coordinates. Returns an array of intersectObject, sorted by distance (from closest to farthest). |