-
Notifications
You must be signed in to change notification settings - Fork 2
/
selfishgame.js
86 lines (76 loc) · 1.61 KB
/
selfishgame.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var UPDATE_DELAY = 40;
var THEMES = [
'Dementia',
'Never inside',
'Capture frame',
'Slow seconds',
'Sticky feature',
'Gender',
'Inappropriate medium',
'Past and lie',
'Paul',
'Streamed',
'Navigations',
'Advertising',
'Smelly',
'Inside out',
'Is this random?',
'I don’t want to grow up',
'Loop',
'Vengeance',
'Indians',
'Hardcore',
'War and Peace',
'Light',
'It’s not possible',
'Garden',
'I’m home!',
'One more time',
'It’s not funny anymore'
];
var button = document.querySelector('#generator');
var colon = button.previousElementSibling;
var init = function() {
off(button, 'click', init);
colon.hidden = false;
start(button);
};
on(button, 'click', init);
function nextTheme(index) {
if (++index < THEMES.length) return index;
return 0;
}
function start(elt) {
var currentTheme = 0;
var displayedTheme = null;
var stopped = true;
var update = function() {
setTimeout(update, UPDATE_DELAY);
if (stopped) return;
currentTheme = nextTheme(currentTheme);
};
var draw = function() {
raf(draw);
if (stopped) return;
if (displayedTheme === currentTheme) return;
elt.innerHTML = THEMES[(displayedTheme = currentTheme)];
};
on(document, 'click', function(event) {
if (event.target === elt && stopped) {
stopped = false;
} else if (!stopped) {
stopped = true;
}
});
update();
draw();
}
var raf = requestAnimationFrame || function(cb) {
setTimeout(cb, 10);
};
function on(elt, name, cb) {
elt.addEventListener(name, cb);
}
function off(elt, name, cb) {
elt.removeEventListener(name, cb);
}