-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (40 loc) · 1.01 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
const util = require('./lib/util');
const joystick = require('./lib/joystick');
const canvas = require('./lib/canvas');
const accelerometer = require('./lib/accelerometer');
const RAINBOX_DELAY = 5000;
const JOYSTICK_POLL = 500;
const dot = { x: canvas.SIZE / 2, y: canvas.SIZE / 2 };
canvas.addDot(dot);
let timeout;
function tick() {
if (!joystick.isStill()) {
if (joystick.pressed) {
canvas.cycle();
}
accelerometer.update();
move('x');
move('y');
} else if (Date.now() - joystick.lastPress >= RAINBOX_DELAY) {
// Increase FPS for an awesome rainbow
schedule(1);
return canvas.drawRainbow();
}
schedule(JOYSTICK_POLL);
}
function schedule(time) {
clearTimeout(timeout);
timeout = setTimeout(tick, time);
}
function move(axis) {
const delta = joystick[axis] + accelerometer[axis];
if (delta) {
const n = dot[axis] + delta;
dot[axis] = util.clamp(n, 0, canvas.SIZE - 1);
canvas.draw();
}
}
tick();
canvas.draw();
// Ensure even a quick press is not ignored
joystick.on('changed', tick);