-
Notifications
You must be signed in to change notification settings - Fork 0
/
virus.html
246 lines (209 loc) · 6.16 KB
/
virus.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Viral Spread</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden; /* Disable scrollbars */
display: block; /* No floating content on sides */
}
</style>
</head>
<body>
<canvas id='c' style='position:absolute; left:0px; top:0px; background-color:#000;' ondblclick="openFullscreen()">
</canvas>
<script>
var
start = null,
gameSpeed = 30,
elem = document.documentElement;
canvas = document.getElementById('c'),
context = canvas.getContext('2d'),
personRadius = 3,
personEdge = 1,
colorPerson = "#fdd",
colorSick = "#8f3",
colorCured = "#09f",
colorDead = "#222",
colorOutline = "#abc",
personSpeed = 2,
peopleTotal = 750,
peopleSick = 1,
recoveryTime = 300,
immunityTime = 300,
survivalRate = 78,
useCircles = false;
function initialize() {
// Check and update dimension data whenever the window is resized
window.addEventListener('resize', resizeCanvas, false);
// populate
createPopulation( peopleTotal, peopleSick );
// request fullscreen
openFullscreen();
// Resize the canvas, re-draw as needed
resizeCanvas();
//setInterval( redraw, 20 );
window.requestAnimationFrame( redraw );
}
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { // FireFox
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) { // Chrome, Safari, Opera
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { // IE Edge
elem.msRequestFullscreen();
}
}
// handle when window is resized
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//redraw(); // old
}
// return a random point anywhere along the horizontal axis of the canvas
function anywhereX() {
return Math.round( Math.random() * (window.innerWidth - (personRadius * 2)) + personRadius );
}
// return a random point anywhere along the vertical axis of the canvas
function anywhereY() {
return Math.round( Math.random() * (window.innerHeight - (personRadius * 2)) + personRadius );
}
// this function gives a random integer between 1 and the value max
function diceRoll(max){
return Math.round( Math.random() * max );
}
function flipflop(){
var x = (Math.random() * 2) - 1;
return x;
}
// object definition for person
var person = {
x: 5,
y: 5,
speedx: 1,
speedy: 1,
health: "healthy",
lifespan: 1
};
// call this to create a new person
function createPerson(){
var newPerson = Object.create( person );
newPerson.x = anywhereX();
newPerson.y = anywhereY();
newPerson.speedx = flipflop() * personSpeed;
newPerson.speedy = flipflop() * personSpeed;
newPerson.health = "healthy";
newPerson.lifespan = 1;
return newPerson;
}
// returns a color value based on a person's health
function getHealthColor( person ){
if( person.health == "healthy" )
return colorPerson;
if( person.health == "infected" )
return colorSick;
if( person.health == "cured" )
return colorCured;
if( person.health == "dead" )
return colorDead;
}
function currentMillis(){
var t = new Date();
return t;
}
// draw a circle on the canvas representing a person
function drawPerson( person ) {
context.fillStyle = getHealthColor( person );
if( useCircles ){
//context.lineWidth = personEdge;
//context.strokeStyle = colorOutline;
context.beginPath();
context.arc(person.x, person.y, personRadius, 0, 2 * Math.PI);
context.fill();
} else {
context.fillRect( person.x-personRadius, person.y-personRadius, personRadius*2, personRadius*2 );
}
}
// update a person
function updatePerson( index, progress ){
var p = people[i];
// do boundary check
if( p.x + personRadius > window.innerWidth ) p.speedx = -Math.abs(p.speedx);
if( p.x - personRadius < 0 ) p.speedx = Math.abs(p.speedx);
if( p.y - personRadius < 0 ) p.speedy = Math.abs(p.speedy);
if( p.y + personRadius > window.innerHeight ) p.speedy = -Math.abs(p.speedy);
// Check for people interacting with each other
for( j = 0; j < peopleTotal - 1; j++ ){
if( (j != index) && (people[j].health != "dead") ){ // avoid checking same person, and dead people
if( dist( p.x, p.y, people[j].x, people[j].y ) < ( personRadius * 2.1 )){ // contact!
p.speedx *= -1;
p.speedy *= -1;
people[j].speedx *= -1;
people[j].speedy *= -1;
if( (p.health == "healthy") && (people[j].health == "infected") ){
p.health = "infected";
p.lifespan = 1;
}
if( (people[j].health == "healthy") && (p.health == "infected") ){
people[j].health = "infected";
people[j].lifespan = 1;
}
}
}
}
// update person position
p.x += (p.speedx * progress);
p.y += (p.speedy * progress);
// If sick, check on timeline and heal after duration
if( p.health == "infected" ){
p.lifespan += progress;
if( p.lifespan > recoveryTime ){
if( diceRoll(100) > 100 - survivalRate ){
p.health = "cured";
p.lifespan = 1;
} else { p.health = "dead"; }
}
}
// If cured, let immunity run out before a person can be infected against
if( p.health == "cured" ){
p.lifespan += progress;
if( p.lifespan > immunityTime ){
p.health = "healthy";
p.lifespan = 1;
}
}
}
function redraw( timestamp ) {
if( !start ) start = timestamp;
var progress = (timestamp - start) / gameSpeed;
start = timestamp;
context.clearRect( 0, 0, canvas.width, canvas.height );
for( i = 0; i < peopleTotal - 1; i++ ){
if( people[i].health != "dead" )
updatePerson( i, progress );
drawPerson( people[i] );
}
window.requestAnimationFrame( redraw );
}
function dist( x1, y1, x2, y2 ){
return Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function createPopulation( count, numSick ){
for( i = 0; i < count; i++ ){
people.push( createPerson() );
if( i < peopleSick )
people[i].health = "infected";
}
}
var people = [];
// Start listening to resize events and draw canvas
initialize();
</script>
</body>
</html>