-
Notifications
You must be signed in to change notification settings - Fork 1
/
floating-circles-canvas (1).html
212 lines (183 loc) · 8.05 KB
/
floating-circles-canvas (1).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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Floating Circles with Uniform Explosive Release</title>
<style>
body { margin: 0; overflow: hidden; background-color: #f0f0f0; }
canvas { display: block; }
#controls { position: absolute; top: 10px; left: 10px; background: rgba(255,255,255,0.7); padding: 10px; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="controls">
<label>Blend Mode:
<select id="blendMode">
<option value="source-over">Normal</option>
<option value="screen">Screen</option>
<option value="multiply">Multiply</option>
<option value="overlay">Overlay</option>
<option value="difference">Difference</option>
</select>
</label>
<br>
<label>Opacity: <input type="range" id="opacity" min="0" max="1" step="0.1" value="0.7"></label>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const blendModeSelect = document.getElementById('blendMode');
const opacityInput = document.getElementById('opacity');
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
function remToPx(rem) {
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
}
const circleDiameter = 10; // in rem
const circleRadius = remToPx(circleDiameter) / 2;
const frictionFactor = 0.995;
const minSpeed = 0.5;
const maxSpeed = 10;
const returnDuration = 1000;
const explosiveSpeed = 30; // Consistent explosive speed for all circles
function hexToRgb(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}
const circles = [
{ x: canvas.width * 0.2, y: canvas.height * 0.2, radius: circleRadius, color: hexToRgb('#ef476f'), vx: 6, vy: 5 },
{ x: canvas.width * 0.4, y: canvas.height * 0.4, radius: circleRadius, color: hexToRgb('#ffd166'), vx: -5, vy: 6 },
{ x: canvas.width * 0.6, y: canvas.height * 0.6, radius: circleRadius, color: hexToRgb('#06d6a0'), vx: -4, vy: -5.5 },
{ x: canvas.width * 0.8, y: canvas.height * 0.8, radius: circleRadius, color: hexToRgb('#118ab2'), vx: 3, vy: -4 },
{ x: canvas.width * 0.5, y: canvas.height * 0.5, radius: circleRadius, color: hexToRgb('#073b4c'), vx: -2, vy: 3 },
{ x: canvas.width / 2, y: canvas.height / 2, radius: circleRadius, color: [135, 206, 235], vx: 0, vy: 0 } // Sky blue (static)
];
let isReturning = false;
let returnStartTime;
const gifUrls = [
'/api/placeholder/300/300',
'/api/placeholder/300/300',
'/api/placeholder/300/300',
'/api/placeholder/300/300',
'/api/placeholder/300/300'
];
let currentGifIndex = 0;
const cloudGif = new Image();
cloudGif.src = gifUrls[currentGifIndex];
function shuffleGif() {
currentGifIndex = (currentGifIndex + 1) % gifUrls.length;
cloudGif.src = gifUrls[currentGifIndex];
}
function applyFriction(circle) {
let speed = Math.sqrt(circle.vx * circle.vx + circle.vy * circle.vy);
if (speed > minSpeed) {
circle.vx *= frictionFactor;
circle.vy *= frictionFactor;
} else {
let angle = Math.atan2(circle.vy, circle.vx);
circle.vx = minSpeed * Math.cos(angle);
circle.vy = minSpeed * Math.sin(angle);
}
}
function distanceToCenter(circle) {
const dx = circles[5].x - circle.x;
const dy = circles[5].y - circle.y;
return Math.sqrt(dx * dx + dy * dy);
}
function initiateReturn() {
isReturning = true;
returnStartTime = Date.now();
circles.slice(0, 5).forEach(circle => {
circle.startX = circle.x;
circle.startY = circle.y;
circle.distance = distanceToCenter(circle);
});
}
function updateReturnMotion(circle) {
const elapsedTime = Date.now() - returnStartTime;
const progress = Math.min(elapsedTime / returnDuration, 1);
const easeProgress = 1 - Math.pow(1 - progress, 3);
const dx = circles[5].x - circle.startX;
const dy = circles[5].y - circle.startY;
circle.x = circle.startX + dx * easeProgress;
circle.y = circle.startY + dy * easeProgress;
if (progress === 1 && circle === circles[0]) {
isReturning = false;
initiateExplosiveRelease();
shuffleGif();
}
}
function initiateExplosiveRelease() {
circles.slice(0, 5).forEach(circle => {
const angle = Math.random() * Math.PI * 2;
circle.vx = Math.cos(angle) * explosiveSpeed;
circle.vy = Math.sin(angle) * explosiveSpeed;
});
}
function drawSkyCircle() {
const skyCircle = circles[5];
ctx.save();
ctx.beginPath();
ctx.arc(skyCircle.x, skyCircle.y, skyCircle.radius, 0, Math.PI * 2);
ctx.clip();
ctx.fillStyle = `rgb(${skyCircle.color[0]}, ${skyCircle.color[1]}, ${skyCircle.color[2]})`;
ctx.fillRect(skyCircle.x - skyCircle.radius, skyCircle.y - skyCircle.radius, skyCircle.radius * 2, skyCircle.radius * 2);
ctx.drawImage(cloudGif, skyCircle.x - skyCircle.radius, skyCircle.y - skyCircle.radius, skyCircle.radius * 2, skyCircle.radius * 2);
ctx.restore();
ctx.beginPath();
ctx.arc(skyCircle.x, skyCircle.y, skyCircle.radius, 0, Math.PI * 2);
ctx.strokeStyle = `rgb(${skyCircle.color[0]}, ${skyCircle.color[1]}, ${skyCircle.color[2]})`;
ctx.lineWidth = 2;
ctx.stroke();
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSkyCircle();
ctx.globalCompositeOperation = blendModeSelect.value;
const opacity = opacityInput.value;
circles.slice(0, 5).forEach(circle => {
if (isReturning) {
updateReturnMotion(circle);
} else {
applyFriction(circle);
circle.x += circle.vx;
circle.y += circle.vy;
if (circle.x + circle.radius > canvas.width || circle.x - circle.radius < 0) {
circle.vx *= -1;
}
if (circle.y + circle.radius > canvas.height || circle.y - circle.radius < 0) {
circle.vy *= -1;
}
}
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2);
ctx.fillStyle = `rgba(${circle.color[0]}, ${circle.color[1]}, ${circle.color[2]}, ${opacity})`;
ctx.fill();
});
requestAnimationFrame(animate);
}
cloudGif.onload = animate;
canvas.addEventListener('click', () => {
if (!isReturning) {
initiateReturn();
}
});
window.addEventListener('resize', () => {
resizeCanvas();
const newRadius = remToPx(circleDiameter) / 2;
circles.forEach(circle => {
circle.radius = newRadius;
});
circles[5].x = canvas.width / 2;
circles[5].y = canvas.height / 2;
});
</script>
</body>
</html>