-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfireworks.htm
More file actions
99 lines (94 loc) · 3.27 KB
/
fireworks.htm
File metadata and controls
99 lines (94 loc) · 3.27 KB
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
<style>
body {background-color:black;}
table {counter-reset: rowNumber;font-family: "FreeMono", monospace; }
table tr {counter-increment: rowNumber;}
table tr td:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;}
a {color: white;}
</style>
<meta name="KeyWords" content="javascript java script programming coding tutorial how to code example fireworks">
<meta name="Description" content="Let's welcome 2019 with fireworksr - easy JavaScript tutorial">
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<meta property="og:image" content='https://i.imgur.com/eVhTgjy.png' />
<title>Let's welcome 2019 with fireworks - easy JavaScript tutorial</title>
</head>
<body>
<font face="Arial,Helvetica,Sans-Serif" color="white">
<center>
<br><br>
<h1>Let's welcome 2019 with fireworks!</h1>
<canvas id="myCanvas" width="800" height="500"></canvas><br><br>
<script>
const max_fireworks = 5,
max_sparks = 50;
let canvas = document.getElementById('myCanvas');
let context = canvas.getContext('2d');
let fireworks = [];
for (let i = 0; i < max_fireworks; i++) {
let firework = {
sparks: []
};
for (let n = 0; n < max_sparks; n++) {
let spark = {
vx: Math.random() * 5 + .5,
vy: Math.random() * 5 + .5,
weight: Math.random() * .3 + .03,
red: Math.floor(Math.random() * 2),
green: Math.floor(Math.random() * 2),
blue: Math.floor(Math.random() * 2)
};
if (Math.random() > .5) spark.vx = -spark.vx;
if (Math.random() > .5) spark.vy = -spark.vy;
firework.sparks.push(spark);
}
fireworks.push(firework);
resetFirework(firework);
}
window.requestAnimationFrame(explode);
function resetFirework(firework) {
firework.x = Math.floor(Math.random() * canvas.width);
firework.y = canvas.height;
firework.age = 0;
firework.phase = 'fly';
}
function explode() {
context.clearRect(0, 0, canvas.width, canvas.height);
fireworks.forEach((firework,index) => {
if (firework.phase == 'explode') {
firework.sparks.forEach((spark) => {
for (let i = 0; i < 10; i++) {
let trailAge = firework.age + i;
let x = firework.x + spark.vx * trailAge;
let y = firework.y + spark.vy * trailAge + spark.weight * trailAge * spark.weight * trailAge;
let fade = i * 20 - firework.age * 2;
let r = Math.floor(spark.red * fade);
let g = Math.floor(spark.green * fade);
let b = Math.floor(spark.blue * fade);
context.beginPath();
context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',1)';
context.rect(x, y, 4, 4);
context.fill();
}
});
firework.age++;
if (firework.age > 100 && Math.random() < .05) {
resetFirework(firework);
}
} else {
firework.y = firework.y - 10;
for (let spark = 0; spark < 15; spark++) {
context.beginPath();
context.fillStyle = 'rgba(' + index * 50 + ',' + spark * 17 + ',0,1)';
context.rect(firework.x + Math.random() * spark - spark / 2, firework.y + spark * 4, 4, 4);
context.fill();
}
if (Math.random() < .001 || firework.y < 200) firework.phase = 'explode';
}
});
window.requestAnimationFrame(explode);
}
</script>
</body>
</html>