-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscillation.html
More file actions
153 lines (129 loc) · 3.28 KB
/
oscillation.html
File metadata and controls
153 lines (129 loc) · 3.28 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid Bouncing Universes</title>
<style>
html, body {
margin: 0;
background: black;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="universeCanvas"></canvas>
<script>
const canvas = document.getElementById("universeCanvas");
const ctx = canvas.getContext("2d");
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
const size = 100;
const radius = size / 2;
const cols = 10;
const rows = 10;
const nestedCircles = 20;
const scaleFactor = 0.65;
const universes = [];
class Universe {
constructor(x, y) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 1.5;
this.vy = (Math.random() - 0.5) * 1.5;
this.rotation = Math.random() * 2 * Math.PI;
this.rotationSpeed = (Math.random() - 0.5) * 0.01;
this.phaseOffset = Math.random() * Math.PI * 2;
}
update(time) {
this.x += this.vx;
this.y += this.vy;
this.rotation += this.rotationSpeed;
// Edge bounce
if (this.x - radius < 0 || this.x + radius > canvas.width) this.vx *= -1;
if (this.y - radius < 0 || this.y + radius > canvas.height) this.vy *= -1;
this.draw(time);
}
draw(time) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
for (let i = 0; i < nestedCircles; i++) {
let r = radius * Math.pow(scaleFactor, i);
let pulse = 1 + 0.05 * Math.sin(time / 300 + i + this.phaseOffset);
r *= pulse;
const hue = (240 + i * 7 + this.phaseOffset * 50) % 360;
ctx.strokeStyle = `hsl(${hue}, 80%, 60%)`;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(0, 0, r, 0, 2 * Math.PI);
ctx.stroke();
}
ctx.restore();
}
}
function initializeGrid() {
const startX = (canvas.width - cols * size) / 2;
const startY = (canvas.height - rows * size) / 2;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = startX + col * size + radius;
const y = startY + row * size + radius;
universes.push(new Universe(x, y));
}
}
}
function resolveCollisions() {
for (let i = 0; i < universes.length; i++) {
for (let j = i + 1; j < universes.length; j++) {
const a = universes[i];
const b = universes[j];
const dx = b.x - a.x;
const dy = b.y - a.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const minDist = size;
if (dist < minDist && dist > 0) {
// Normalize
const nx = dx / dist;
const ny = dy / dist;
// Relative velocity
const dvx = b.vx - a.vx;
const dvy = b.vy - a.vy;
const dot = dvx * nx + dvy * ny;
if (dot < 0) {
const impulse = dot;
a.vx += impulse * nx;
a.vy += impulse * ny;
b.vx -= impulse * nx;
b.vy -= impulse * ny;
}
// Positional separation
const overlap = (minDist - dist) / 2;
a.x -= nx * overlap;
a.y -= ny * overlap;
b.x += nx * overlap;
b.y += ny * overlap;
}
}
}
}
initializeGrid();
function animate(time) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
resolveCollisions();
for (const u of universes) {
u.update(time);
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>