-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (61 loc) · 1.64 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
var mousewheel = require('mouse-wheel')
var mousechange = require('mouse-change')
var proj = require('glsl-proj4')
var EventEmitter = require('events').EventEmitter
module.exports = function (str) {
var p = proj(str)
if (!/\bx_0=/.test(str)) str += ' x_0=0'
if (!/\by_0=/.test(str)) str += ' y_0=0'
if (!/\bk_0=/.test(str)) str += ' k_0=0'
var ev = new EventEmitter
var ref = p.members('')
Object.keys(ref).forEach(function (key) {
ref[key.replace(/^\./,'')] = ref[key]
delete ref[key]
})
var prevx = -1, prevy = -1
ev.string = function () {
str = str.replace(/\bx_0=\S+/, 'x_0='+ref.x0)
str = str.replace(/\by_0=\S+/, 'y_0='+ref.y0)
str = str.replace(/\bk_0=\S+/, 'k_0='+ref.k0)
return str
}
ev.members = function (name) {
var m = p.members(name)
m[name+'.x0'] = function () { return ref.x0 }
m[name+'.y0'] = function () { return ref.y0 }
m[name+'.k0'] = function () { return ref.k0 }
return m
}
mousechange(function (buttons, x, y) {
var dx = x - prevx, dy = y - prevy
var m = Math.max(window.innerWidth, window.innerHeight)
if (prevx < 0 || prevy < 0) {
prevx = x, prevy = y
return
}
prevx = x, prevy = y
if (buttons & 1) {
ref.x0 += dx / m * 2
ref.y0 -= dy / m * 2
ev.emit('update', ref)
}
})
mousewheel(function (dx, dy) {
zoom(dy/1000)
})
function zoom (x) {
ref.k0 *= 1-x
ref.x0 *= 1-x
ref.y0 *= 1-x
ev.emit('update', ref)
}
window.addEventListener('keydown', function (ev) {
if (ev.key === '-') {
zoom(0.3)
} else if (ev.key === '+' || ev.key === '=') {
zoom(-0.3)
}
})
return ev
}