-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (110 loc) · 3.44 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// random walk on canvas
// autor: Adam Jurczyk
var bkg = document.getElementById('bkg');
var counter = document.getElementById('counter');
var ctx = bkg.getContext('2d');
var RECT_SIZE = 4;
var RECT_FILL_SIZE = 3;
var FG_COLORS = [
"#268bd2",
"#2aa198",
"#859900",
"#b58900",
"#cb4b16",
"#dc322f",
"#d33682",
"#6c71c4"];
var FG_COLOR_RAND_CHANCE = 0.01;
var fullWidth, fullHeight,
cols, rows,
steps_per_frame = 4,
walker, isWalking = true,
fillStyle = 'rand';
var randDir = function(){
return Math.floor(Math.random()*3-1);
};
var modulo = function(a,d){
return a - d * Math.floor(a/d);
};
var randFgColor = function(){
return FG_COLORS[Math.floor(Math.random() * FG_COLORS.length)];
};
var Walker = function(){
this.col = Math.round(cols/2),
this.row = Math.round(rows/2),
this.age = 0;
this.walk = function(){
if(fillStyle == 'rand' && Math.random() < FG_COLOR_RAND_CHANCE){
ctx.fillStyle = randFgColor();
}
this.col = modulo(this.col + randDir(), cols);
this.row = modulo(this.row + randDir(), rows);
ctx.fillRect(this.col*RECT_SIZE, this.row*RECT_SIZE,
RECT_FILL_SIZE, RECT_FILL_SIZE);
}
};
var onframe = function(timestamp){
if(isWalking)
window.requestAnimationFrame(onframe);
for(var i=steps_per_frame; i>0; --i){
walker.walk();
++walker.age;
}
counter.innerHTML = walker.age;
};
var init = function(evt){
fullWidth = bkg.width = bkg.offsetWidth;
fullHeight = bkg.height = bkg.offsetHeight;
cols = Math.round(fullWidth/RECT_SIZE);
rows = Math.round(fullHeight/RECT_SIZE);
ctx.globalAlpha = 0.1;
ctx.fillStyle = randFgColor();
walker = new Walker();
};
window.addEventListener('resize', init);
var controls = document.getElementById('controls');
var settings = document.getElementById('settings');
var playpause = controls.getElementsByClassName('playpause')[0];
playpause.addEventListener('click', function(evt){
if(isWalking){
playpause.classList.remove('fa-pause');
playpause.classList.add('fa-play');
}else{
playpause.classList.remove('fa-play');
playpause.classList.add('fa-pause');
window.requestAnimationFrame(onframe);
}
isWalking = !isWalking;
});
var gear = controls.getElementsByClassName('fa-gear')[0];
gear.addEventListener('click', function(evt){
if(settings.classList.contains('hidden'))
settings.classList.remove('hidden');
else
settings.classList.add('hidden');
});
var fgColorRadios = settings.querySelectorAll('input[name="fg-color"]');
Array.prototype.forEach.call(fgColorRadios, function(radio){
radio.addEventListener('change', function(e){
fillStyle = e.target.value;
if(fillStyle=='rand'){
ctx.fillStyle = randFgColor();
}else{
ctx.fillStyle = '#' + fillStyle;
}
});
});
var bgColorRadios = settings.querySelectorAll('input[name="bg-color"]');
Array.prototype.forEach.call(bgColorRadios, function(radio){
radio.addEventListener('change', function(e){
bkg.style.backgroundColor = '#'+e.target.value;
});
});
var speedRadios = settings.querySelectorAll('input[name="speed"]');
Array.prototype.forEach.call(speedRadios, function(radio){
radio.addEventListener('change', function(e){
steps_per_frame = +e.target.value;
});
});
init();
window.requestAnimationFrame(onframe);