-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
174 lines (146 loc) · 5.31 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tools</title>
<link rel="stylesheet" href="Styles/MainPage.css">
</head>
<body style="overflow: hidden;">
<canvas id="BackgroundAnim"></canvas>
<h2 class="Centered">This page contains a list of tools I've made in JavaScript.</h1>
<br>
<div class="Centered">
<a href="Tools/FileToArray.html"><span style="text-align:center; font-size: 25px;">File To Array/List</span></a>
<br>
<a href="Tools/GridGenerator.html"><span style="text-align:center; font-size: 25px;">Grid Generator</span></a>
</div>
<script>
var Canvas = document.getElementById("BackgroundAnim")
var Canvas2D = Canvas.getContext("2d")
Canvas2D.imageSmoothingQuality = "high"
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame;
}
const Width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
const Height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
Canvas.setAttribute("width", Width)
Canvas.setAttribute("height", Height)
var AverageWidthHeight = (Width + Height) / 2
function CreateNode() {
// Radius, X, Y, VelX, VelY, Lifetime
var X = (Math.random() - 0.5) * Width, Y = (Math.random() - 0.5) * Height
if (X < 0) {
X -= Width
}
else {
X += Width
}
if (Y < 0) {
Y -= Height
}
else {
Y += Height
}
// Old Code
/*(Math.random() - 0.5) * 8, (Math.random() - 0.5) * 8*/
// AverageWidthHeight Is Used To Correct Direction Errors, Otherwise A Ball Spawning On 0, 0 Would Go To 0.5, 0.5 Which May Not Be The Resolution
return [(Math.random() + 0.3) * 30, X, Y, (((Width / 2) - X) / AverageWidthHeight) * 3, (((Height / 2) - Y) / AverageWidthHeight) * 3, 0]
}
var Nodes = []
var NodePopulation = 90;
var BestLifetime = 0
for (var i = 0; i < NodePopulation; i++) {
Nodes.push(CreateNode())
}
var TicksPerSecond = 125;
var TickDelayMS = (1 / TicksPerSecond) * 1000
var LastTime = 0, Delta = 0
var Counter = 0
function AnimationLoop(Time) {
Delta += Time - LastTime
while (Delta >= TickDelayMS) {
Counter++
// Update
{
while (Nodes.length < NodePopulation) {
Nodes.push(CreateNode())
}
var NewLifetimeTarget = 0
for (var a = 0; a < Nodes.length; a++) {
var NodeA = Nodes[a]
/*
if (NodeA[1] - NodeA[0] > Width || NodeA[1] + NodeA[0] < 0) {
NodeA[3] *= -1
}
else if (NodeA[2] - NodeA[0] > Height || NodeA[2] + NodeA[0] < 0) {
NodeA[4] *= -1
}
*/
if (NodeA[1] - NodeA[0] > Width * 2 || NodeA[1] + NodeA[0] < -(Width * 2) || NodeA[2] - NodeA[0] > Height * 2 || NodeA[2] + NodeA[0] < -(Height * 2)) {
Nodes.splice(a, 1)
continue
}
for (var b = 0; b < Nodes.length; b++) {
if (b == a) {
continue
}
var NodeB = Nodes[b]
let X1 = NodeA[1], X2 = NodeB[1],
Y1 = NodeA[2], Y2 = NodeB[2],
R1 = NodeA[0], R2 = NodeB[0],
VX1 = NodeA[3], VX2 = NodeB[3],
VY1 = NodeA[4], VY2 = NodeB[4]
if (Math.abs((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)) < (R1 + R2) * (R1 + R2)) {
//console.log(X1, Y1, X2, Y2, (R1 + R2) * (R1 + R2))
let Distance = Math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1))
let CollisionNormal = [(X2 - X1) / Distance, (Y2 - Y1) / Distance]
let Speed = (VX1 - VX2) * CollisionNormal[0] + (VY1 - VY2) * CollisionNormal[1];
if (Speed < 0) {
break;
}
let Impulse = 2 * Speed / (R1 + R2);
NodeA[3] -= (Impulse * R2 * CollisionNormal[0])
NodeA[4] -= (Impulse * R2 * CollisionNormal[1])
NodeB[3] += (Impulse * R1 * CollisionNormal[0])
NodeB[4] += (Impulse * R1 * CollisionNormal[1])
}
}
// Update Position With Velocity
NodeA[1] += NodeA[3]
NodeA[2] += NodeA[4]
// Update Lifetime If Node Is Visible
if (NodeA[1] - NodeA[0] < Width && NodeA[1] + NodeA[0] > 0 && NodeA[2] - NodeA[0] < Height && NodeA[2] + NodeA[0] > 0) {
NodeA[5] += 1
if (NodeA[5] > NewLifetimeTarget) {
NewLifetimeTarget = NodeA[5]
}
}
}
if (BestLifetime > NewLifetimeTarget) {
BestLifetime -= Math.max((BestLifetime - NewLifetimeTarget) / 100, 1)
}
else if (BestLifetime < NewLifetimeTarget) {
BestLifetime += Math.max((NewLifetimeTarget - BestLifetime) / 100, 1)
}
}
Delta -= TickDelayMS
}
// Draw
Canvas2D.clearRect(0, 0, Width, Height);
for (var Node of Nodes) {
Canvas2D.beginPath();
Canvas2D.arc(Node[1], Node[2], Node[0], 0, 2 * Math.PI, false);
Canvas2D.fillStyle = `rgb(${(Node[5] / BestLifetime) * 78},78,78)`;
Canvas2D.fill();
Canvas2D.lineWidth = 5;
Canvas2D.strokeStyle = `rgb(${(Node[5] / BestLifetime) * 55},55,55)`;
Canvas2D.stroke();
}
LastTime = Time;
window.requestAnimationFrame(AnimationLoop)
}
window.requestAnimationFrame(AnimationLoop)
</script>
</body>
</html>