-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
67 lines (54 loc) · 1.8 KB
/
demo.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
65
66
var createBindingsUI = require('./');
var createKb = require('kb-bindings');
var createInteract = require('interact');
var raf = require('raf');
var bindings = {
// for demonstration purposes, based no voxel-engine defaultButtons
'W': 'forward'
, 'A': 'left'
, 'S': 'backward'
, 'D': 'right'
, '<up>': 'forward'
, '<left>': 'left'
, '<down>': 'backward'
, '<right>': 'right'
, '<mouse 1>': 'fire'
, '<mouse 3>': 'firealt'
, '<space>': 'jump'
, '<shift>': 'crouch'
, '<control>': 'alt'
, '<tab>': 'sprint'
, 'X': 'alert1'
, 'C': 'alert2'
};
var buttons = createKb(document.body, bindings);
buttons.disable();
buttons.down.on('alert1', function() {
alert('You pressed alert1! Try changing the key for this binding, then retest.');
});
buttons.up.on('alert2', function() {
alert('Now you pressed alert2. This is an example event handler triggered on key *up* (versus alert1, triggered on down).');
});
// Since kb-bindings prevents default mouse/keyboard interaction, it blocks use of
// the kb-bindings-ui datgui controls. To get around this, only enable kb-bindings
// when we have pointer lock enabled (click to enter, escape to exit).
var element = document.createElement('plaintext');
var interact = createInteract(element);
interact.on('attain', function() {
buttons.enable();
});
interact.on('release', function() {
buttons.disable();
});
// Show the polled state on every frame
element.textContent = 'loading...';
document.body.appendChild(element);
raf(document.body).on('data', function(dt) {
var s = 'Click here to acquire pointer lock (escape to release), then press buttons to test:\n\n';
for (var key in buttons) {
if (typeof buttons[key] !== 'number') continue; // skip reserved
s += key + ': ' + buttons[key] + '\n';
}
element.textContent = s;
});
createBindingsUI(null, {kb: buttons});