-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
64 lines (51 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var createCamera = require('orbit-camera')
var createScroll = require('scroll-speed')
var mp = require('mouse-position')
var mb = require('mouse-pressed')
var key = require('key-pressed')
var panSpeed = 1
module.exports = attachCamera
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]
, [0, 0, 0]
, [0, 1, 0]
)
camera.tick = tick
return camera
function tick() {
var ctrl = key('<control>') || key('<alt>')
var alt = key('<shift>')
var height = canvas.height
var width = canvas.width
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 (opts.pan && mbut.right || (mbut.left && ctrl && !alt)) {
camera.pan([
panSpeed * (mpos.x - mpos.prevX) / width
, panSpeed * (mpos.y - mpos.prevY) / height
])
}
if (opts.scale && scroll[1]) {
camera.distance *= Math.exp(scroll[1] / height)
}
if (opts.scale && (mbut.middle || (mbut.left && !ctrl && alt))) {
var d = mpos.y - mpos.prevY
if (!d) return;
camera.distance *= Math.exp(d / height)
}
scroll.flush()
mpos.flush()
}
}