An easy to use 3D camera with input binding.
Default controls:
| Button | Interaction |
|---|---|
| Left mouse | Rotate |
| Shift + left mouse or scroll horizontally | Roll |
| Right mouse | Pan |
| Middle mouse or scroll vertically | Zoom |
Here is a complete working example of how to use this module in an application:
var createCamera = require('3d-view-controls')
var bunny = require('bunny')
var perspective = require('gl-mat4/perspective')
var createMesh = require('gl-simplicial-complex')
var canvas = document.createElement('canvas')
document.body.appendChild(canvas)
window.addEventListener('resize', require('canvas-fit')(canvas))
var gl = canvas.getContext('webgl')
var camera = createCamera(canvas, {
eye: [50,0,0],
center: [0,0,0],
zoomMax: 500
})
var mesh = createMesh(gl, {
cells: bunny.cells,
positions: bunny.positions,
colormap: 'jet'
})
function render() {
requestAnimationFrame(render)
if(camera.tick()) {
gl.viewport(0, 0, canvas.width, canvas.height)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.enable(gl.DEPTH_TEST)
mesh.draw({
projection: perspective([], Math.PI/4, canvas.width/canvas.height, 0.01, 1000),
view: camera.matrix
})
}
}
render()You can try it out in your browser right now.
npm i 3d-view-controls
Creates a new camera object.
elementis a DOM node onto which thisoptionsis an object with the following optional properties:eye- the position of the camera in world coordinates (Default[0,0,10])center- the target of the camera in world coordinates (Default[0,0,0])up- the up vector of the camera (Default[0,1,0])mode- the interaction mode for the camera (Default'orbit')delay- amount to delay interactions by for interpolation in ms (Default16)rotateSpeed- rotation scaling factor (Default1)zoomSpeed- zoom scaling factor (Default1)translateSpeed- translation/panning scale factor (Default1)flipX- flip X axis for rotations (Defaultfalse)flipY- flip Y axis for rotations (Defaultfalse)zoomMin- minimum zoom distance (Default0.01)zoomMax- maximum zoom distance (DefaultInfinity)
Note that you can update any property by assigning to it. For example:
camera.eye = [100, 100, 100]
camera.matrix = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1]A 4x4 matrix encoded as a length 16 array representing the homogeneous transformation from world coordinates to view (camera) coordinates.
The current interaction mode for the camera. Possible values include:
orbit- free orbiting modeturntable- behaves like a turntable/gimbalmatrix- manual matrix control
The position of the camera in world coordinates
A vector pointing up in world coordinates
The target of the camera in world coordinates
Euclidean distance from eye to center
Updates the camera state. Call this before each frame is rendered to compute the current state of the camera.
Returns true if the state of the camera has changed since the last call to tick
Sets the camera center/eye/up vector to look at a fixed target
centeris the new center/target for the cameraeyeis the position of the camera in world coordinatesupis a vector pointing up
Applies an incremental rotation to the camera
yawis the amount to rotate about the y-axis (in xz plane of camera)pitchis the amount to rotate about the x-axis (in yz plane of camera)rollis the amount to rotate about the forward axis (in xy plane of camera)
Applies a relative motion to the camera, moving in view coordinates
dx,dy,dzare the components of the camera motion vector
Translates the camera in world coordinates
dx,dy,dzare the components of the translation vector
A 2D array representing the [lo,hi] bounds on the zoom distance. Note that 0 < lo < hi.
A flag controlling whether the camera rotation is flipped along the x-axis
A flag controlling whether the camera rotation is flipped along the y-axis
The amount of delay on the interpolation of the camera state in ms
Camera rotation speed scaling factor
Camera zoom speed scaling factor
Camera translation speed scaling factor
The DOM element the camera is attached to
Expand to support more input types:
- Touch
- Keyboard
- GamePad
- VR?
(c) 2015 Mikola Lysenko. MIT License