camera-spin
v3.0.1
Published
Mouse/touch-draggable first-person camera
Downloads
7
Maintainers
Readme
camera-spin
Mouse/touch-draggable first-person camera. Useful for out-of-the-box Google Street View-style camera controls.
Usage
camera = Camera(element, origin, up)
Creates a new camera instance. All arguments are optional:
element
is the DOM element to attach to and make interactive.origin
is a[x, y, z]
array specifying the position of the camera. Defaults to[0, 0, 0]
.up
is a[x, y, z]
array specifying the "up" vector to use. Defaults to[0, 1, 0]
.
view = camera.tick()
This function should be run once per frame in your requestAnimationFrame loop:
const raf = require('raf')
render()
function render () {
camera.tick()
raf(render)
}
It's responsible for updating the camera parameters in response to user input. Returns view
, your 4x4 Float32Array
view matrix to give your shaders.
view = camera.view()
Calculates and returns the 4x4 view
matrix provided by camera.tick()
without performing any of the user input handling.
camera.direction
A normalized [x, y, z]
array specifying the direction the camera is currently looking as a vector. You can use this, for example, to move the camera forward in the direction it's looking:
camera.origin[0] += camera.direction[0] * distance
camera.origin[1] += camera.direction[1] * distance
camera.origin[2] += camera.direction[2] * distance
The value is read-only: it'll be updated each time you call camera.tick()
.
camera.rotation
An [x, y]
array containing the horizontal and vertical rotation in radians. You can use this to manually point the camera in a specific direction.
camera.lookSpeed
Change this value to set the speed at which the camera moves around, i.e. its mouse sensitivity. Should be between 0 and 1. Defaults to 0.003
.
camera.dragRate
Change this value to set the rate of interpolation between rotation values. Should be between 0 and 1 — lower values will result in smoother motion, higher values will increase responsiveness. Defaults to 0.2
.
camera.dispose()
Tear everything down once you're no longer using the camera. Required if you need to clean up after yourself :)
License
MIT, see LICENSE.md for details.