Skip to content

Commit

Permalink
add options for disabling specific interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsk committed Aug 25, 2014
1 parent 0ce22cd commit ca10c9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ Based heavily on

[![NPM](https://nodei.co/npm/canvas-orbit-camera.png)](https://nodei.co/npm/canvas-orbit-camera/)

### camera = createCamera(canvas)
### camera = createCamera(canvas[, options])

Attaches a modified `orbit-camera` instance to the `canvas` – attaching the
required event listeners for interaction.

The following options are available:

* `rotate`: disable rotation interactions by passing `false`.
* `scale`: disable scaling interactions by passing `false`.
* `pan`: disable panning interactions by passing `false`.

See the [orbit-camera documentation](https://github.com/mikolalysenko/orbit-camera#readme)
for a full list of available methods.

Expand All @@ -48,10 +54,10 @@ var camera = createCamera(canvas)
update()
function update() {
raf(update)

// Returns your view matrix for you
var view = camera.view()

camera.tick()
}
```
Expand Down
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ var panSpeed = 1

module.exports = attachCamera

function attachCamera(canvas) {
var scroll = createScroll(canvas, true)
var mbut = mb(canvas, true)
function attachCamera(canvas, opts) {
opts = opts || {}
opts.pan = opts.pan !== false
opts.scale = opts.scale !== false
opts.rotate = opts.rotate !== false

var scroll = createScroll(canvas, opts.scale)
var mbut = mb(canvas, opts.rotate)
var mpos = mp(canvas)
var camera = createCamera(
[0, 10, 30]
Expand All @@ -28,25 +33,25 @@ function attachCamera(canvas) {
var height = canvas.height
var width = canvas.width

if (mbut.left && !ctrl && !alt) {
if (opts.rotate && mbut.left && !ctrl && !alt) {
camera.rotate(
[ mpos.x / width - 0.5, mpos.y / height - 0.5 ]
, [ mpos.prevX / width - 0.5, mpos.prevY / height - 0.5 ]
)
}

if (mbut.right || (mbut.left && ctrl && !alt)) {
if (opts.pan && mbut.right || (mbut.left && ctrl && !alt)) {
camera.pan([
panSpeed * (mpos.x - mpos.prevX) / width
, panSpeed * (mpos.y - mpos.prevY) / height
])
}

if (scroll[1]) {
if (opts.scale && scroll[1]) {
camera.distance *= Math.exp(scroll[1] / height)
}

if (mbut.middle || (mbut.left && !ctrl && alt)) {
if (opts.scale && (mbut.middle || (mbut.left && !ctrl && alt))) {
var d = mpos.y - mpos.prevY
if (!d) return

Expand Down

0 comments on commit ca10c9f

Please sign in to comment.