1.29.3 • Published 2 months ago

three-render-objects v1.29.3

Weekly downloads
6,306
License
MIT
Repository
github
Last release
2 months ago

three-render-objects

NPM package Build Size NPM Downloads

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.

Quick start

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>);

API reference

Initialisation

ThreeRenderObjects({ configOptions })(<domElement>)
Config optionsDescriptionDefault
controlType: strWhich type of control to use to control the camera. Choice between trackball, orbit or fly.trackball
rendererConfig: objectConfiguration parameters to pass to the ThreeJS WebGLRenderer constructor.{ antialias: true, alpha: true }
extraRenderers: arrayIf you wish to include objects that require a dedicated renderer besides WebGL, such as CSS3DRenderer, include in this array those extra renderer instances.[]
waitForLoadComplete: booleanWhether 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

Data input

MethodDescriptionDefault
objects(array)Getter/setter for the list of objects to render. Each object should be an instance of Object3D.[]
lights(array)Getter/setter for the list of lights to use in the scene. Each item should be an instance of Light.[]

Container layout

MethodDescriptionDefault
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

Render control

MethodDescriptionDefault
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.

Interaction

MethodDescriptionDefault
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.

Utility

MethodDescription
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).
1.29.1

2 months ago

1.29.2

2 months ago

1.29.3

2 months ago

1.29.0

6 months ago

1.28.5

10 months ago

1.28.6

10 months ago

1.28.1

1 year ago

1.28.2

1 year ago

1.28.0

1 year ago

1.28.3

1 year ago

1.28.4

12 months ago

1.27.6

1 year ago

1.27.7

1 year ago

1.27.5

1 year ago

1.27.8

1 year ago

1.27.3

2 years ago

1.27.4

2 years ago

1.27.2

2 years ago

1.27.0

2 years ago

1.27.1

2 years ago

1.26.8

2 years ago

1.26.7

2 years ago

1.26.5

2 years ago

1.26.6

2 years ago

1.26.4

3 years ago

1.26.3

3 years ago

1.26.1

3 years ago

1.26.2

3 years ago

1.26.0

3 years ago

1.25.0

3 years ago

1.25.1

3 years ago

1.24.6

3 years ago

1.24.5

4 years ago

1.24.4

4 years ago

1.24.3

4 years ago

1.24.2

4 years ago

1.24.1

4 years ago

1.24.0

4 years ago

1.23.0

4 years ago

1.22.1

4 years ago

1.21.1

4 years ago

1.22.0

4 years ago

1.21.0

4 years ago

1.20.1

4 years ago

1.20.0

4 years ago

1.19.0

4 years ago

1.18.0

4 years ago

1.16.0

4 years ago

1.17.0

4 years ago

1.15.1

4 years ago

1.15.0

4 years ago

1.14.1

4 years ago

1.14.0

4 years ago

1.13.4

4 years ago

1.13.3

4 years ago

1.13.2

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.6

4 years ago

1.12.5

4 years ago

1.12.4

4 years ago

1.12.3

4 years ago

1.12.2

4 years ago

1.12.1

4 years ago

1.12.0

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.9.0

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago