-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
189 lines (175 loc) · 5.35 KB
/
index.html
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Life.io</title>
<style>
* { margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; background-color: white; }
#map { display: block; transform: scale(4); transform-origin: 0 0; }
#graphC {
position: fixed; right:20px; bottom: 20px; padding:10px;
width: 400px; height: 200px; background-color: rgba(255,255,255,0.8);
box-shadow: 0px 0px 4px rgba(0,0,0,0.8); border-radius: 4px;
}
#pauseScreen {
position: fixed; right:0px; bottom: 0px; left: 0px; top: 0px;
display: flex; align-items: center; justify-content: center;
background-color: rgba(0,0,0,0.25);
}
#pauseScreen > div {
padding: 20px; text-align: center;
background-color: rgba(255,255,255,0.8);
box-shadow: 0px 0px 4px rgba(0,0,0,0.8); border-radius: 4px;
font-size: 18px; font-family: Arial; font-weight: bold;
}
</style>
</head>
<body>
<canvas id="map"></canvas>
<div id="pauseScreen" style="display:none;">
<div>PAUSED</div>
</div>
<div id="graphC">
<canvas id="graph"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
<script>
// Initialisation du contexte de dessin et des variables
var c = document.getElementById('map'), ctx = c.getContext('2d')
var cG = document.getElementById('graph'), ctxG = cG.getContext('2d')
var image, imgData, px = [], w, h, nB = [], nR = [], myGraph, graphL = 100
var pauseState = false
// Fonction appelee pour gerer la pause
var togglePause = () => {
pauseState = !pauseState
var el = document.getElementById('pauseScreen');
if(pauseState){el.style.display = 'flex'}else{el.style.display = 'none'}
if(!pauseState){requestAnimationFrame(updateMap)}
}
// On écoute les clics pour activer ou enlever la pause
document.onclick = togglePause
// Fonction appelee pour initialiser les cellules
var initMap = () => {
[c.width,c.height] = [w,h] = [
Math.floor(window.innerWidth/4),
Math.floor(window.innerHeight/4)
]
px = Array(w).fill().map(() => Array(h).fill().map(() => {
return {
s: Math.random()>(5/8),
c: Math.random()>0.5
}
}))
drawMap()
}
// Fonction appelee pour mettre à jour les cellules
var updateMap = () => {
px = px.map((v,x,a) => v.map((w,y) => cellNextState(x,y,a)))
drawMap()
}
// Fonction qui donne l'état suivant d'une cellule
var cellNextState = (x,y,a) => {
var ax = x==0?w-1:x-1, bx = x==w-1?0:x+1
var ay = y==0?h-1:y-1, by = y==h-1?0:y+1
// Somme des cellules voisines
var s = [
a[ax][ay].s,a[x][ay].s,a[bx][ay].s,a[bx][y].s,
a[bx][by].s,a[x][by].s,a[ax][by].s,a[ax][y].s
].reduce((s,v) => s+1*v, 0)
// Somme des couleurs des voisines
var c = [
a[ax][ay].s*a[ax][ay].c,a[x][ay].s*a[x][ay].c,
a[bx][ay].s*a[bx][ay].c,a[bx][y].s*a[bx][y].c,
a[bx][by].s*a[bx][by].c,a[x][by].s*a[x][by].c,
a[ax][by].s*a[ax][by].c,a[ax][y].s*a[ax][y].c
].reduce((s,v) => s+1*v, 0)
return {
s: (s<2||s>3)?0:(s==3)?1:a[x][y].s,
c: s==3?c<2?0:1:a[x][y].c
}
}
// Fonction appelee pour dessiner sur le canvas
var drawMap = () => {
countCells()
ctx.clearRect(0,0,w,h)
image = ctx.createImageData(w,h); imgData = image.data
px.map((v,x) => v.map((w,y) => px[x][y].s && drawPixel(x,y,px[x][y].c)))
ctx.putImageData(image,0,0)
if(!pauseState){ requestAnimationFrame(updateMap) }
}
// Fonction pour dessiner le pixel situe en (x,y)
var drawPixel = (x,y,c) => {
var n = 4*(y*w+x)
imgData[n+3] = 255 // Opacité
if(c){ // Rouge
imgData[n] = 231; imgData[n+1] = 76; imgData[n+2] = 60
}else{ // Bleu
imgData[n] = 52; imgData[n+1] = 152; imgData[n+2] = 219
}
}
// Fonction appelee pour compter les cellules
var countCells = () => {
nB.push(px.reduce((s,v) => s+1*v.reduce((s,v) => s+1*v.s*!v.c, 0), 0))
nR.push(px.reduce((s,v) => s+1*v.reduce((s,v) => s+1*v.s*v.c, 0), 0))
if(nB.length>graphL){nB.shift()}
if(nR.length>graphL){nR.shift()}
myGraph.data.datasets[0].data = nR
myGraph.data.datasets[1].data = nB
myGraph.update()
}
// Fonction appelee pour dessiner le graphique
var initGraph = () => {
myGraph = new Chart(ctxG, {
type: 'line',
data: {
labels: Array(graphL).fill(''),
datasets: [{
label: 'Red',
backgroundColor: 'rgb(231, 76, 60)',
borderColor: 'rgb(231, 76, 60)',
data: [],
fill: false
},{
label: 'Blue',
backgroundColor: 'rgb(52, 152, 219)',
borderColor: 'rgb(52, 152, 219)',
data: [],
fill: false
}]
},
options: {
elements: {
line: {
tension: 0
},
point: {
radius: 0
}
},
animation: {
duration: 200,
},
tooltips: {
enabled: false
},
legend: {
display: false
},
responsive: true,
scales: {
xAxes: [{
display: false
}],
yAxes: [{
display: true
}]
}
}
})
}
initGraph() // Initialisation du graphique
initMap() // Lancement du jeu de la vie
</script>
</body>
</html>