-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
433 lines (371 loc) · 14.5 KB
/
scripts.js
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
const sunset = document.getElementById('sunset');
const sun = document.querySelector('.sun');
const clouds = [];
let birdCount = 0;
const maxBirdCount = 100;
class Cloud {
constructor() {
this.element = document.createElement('div');
this.element.classList.add('cloud');
this.size = Math.random() * 100 + 100; // Base size 100-200px
this.element.style.width = `${this.size * 2}px`;
this.element.style.height = `${this.size}px`;
this.element.style.top = `${Math.random() * 40}%`;
this.element.style.zIndex = Math.random() < 0.3 ? '11' : '1';
this.x = window.innerWidth;
this.speed = Math.random() * 0.2 + 0.1;
this.createCloudShape();
sunset.appendChild(this.element);
this.startTime = Date.now();
}
createCloudShape() {
const svgNS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%');
svg.setAttribute('viewBox', '0 0 200 150'); // Changed from '0 0 200 100' to '0 0 200 150'
const defs = document.createElementNS(svgNS, "defs");
const grad = document.createElementNS(svgNS, "radialGradient");
grad.setAttribute('id', `cloudGradient_${Date.now()}`);
grad.innerHTML = `
<stop offset="0%" stop-color="rgba(255,255,255,0.8)" />
<stop offset="100%" stop-color="rgba(255,255,255,0.4)" />
`;
defs.appendChild(grad);
svg.appendChild(defs);
this.group = document.createElementNS(svgNS, "g");
this.group.setAttribute('fill', `url(#${grad.id})`);
// Generate main cloud structure
this.circles = this.generateCloudStructure();
// Add circles to SVG
this.circles.forEach(circle => {
const elem = document.createElementNS(svgNS, "circle");
elem.setAttribute('cx', circle.cx);
elem.setAttribute('cy', circle.cy);
elem.setAttribute('r', circle.r);
this.group.appendChild(elem);
});
// Create bottom curve
this.path = document.createElementNS(svgNS, "path");
this.updatePath();
this.group.appendChild(this.path);
svg.appendChild(this.group);
this.element.appendChild(svg);
}
generateCloudStructure() {
const circles = [];
const numMainCircles = Math.floor(Math.random() * 3) + 3; // 3 to 5 main circles
let lastX = 0;
for (let i = 0; i < numMainCircles; i++) {
const r = Math.random() * 20 + 30; // Radius between 30 and 50
const cx = lastX + r + Math.random() * 20;
const cy = 50 + (Math.random() - 0.5) * 30;
circles.push({ cx, cy, r });
lastX = cx;
// Add 1-3 smaller circles around each main circle
const numSmallCircles = Math.floor(Math.random() * 3) + 1;
for (let j = 0; j < numSmallCircles; j++) {
const angle = Math.random() * Math.PI * 2;
const distance = r * 0.7;
const smallR = r * (0.3 + Math.random() * 0.3);
circles.push({
cx: cx + Math.cos(angle) * distance,
cy: cy + Math.sin(angle) * distance,
r: smallR
});
}
}
// Normalize positions
const maxX = Math.max(...circles.map(c => c.cx + c.r));
circles.forEach(c => c.cx = (c.cx / maxX) * 190 + 5);
return circles;
}
updatePath() {
const points = this.circles.map(c => ({ x: c.cx, y: c.cy + c.r }));
points.sort((a, b) => a.x - b.x);
const firstPoint = points[0];
const lastPoint = points[points.length - 1];
let path = `M ${firstPoint.x} 150 L ${firstPoint.x} ${firstPoint.y}`;
for (let i = 1; i < points.length; i++) {
const midX = (points[i - 1].x + points[i].x) / 2;
path += ` Q ${midX} ${points[i - 1].y}, ${points[i].x} ${points[i].y}`;
}
path += ` L ${lastPoint.x} 150 Z`;
this.path.setAttribute('d', path);
}
move() {
this.x -= this.speed;
this.element.style.left = `${this.x}px`;
// Subtle shape morphing
const elapsedTime = (Date.now() - this.startTime) / 1000;
this.circles.forEach((circle, index) => {
const circleElement = this.group.children[index];
const radiusVariation = Math.sin(elapsedTime + index) * 2;
const newRadius = circle.r + radiusVariation;
circleElement.setAttribute('r', newRadius);
const posVariation = Math.cos(elapsedTime + index) * 3;
circleElement.setAttribute('cx', circle.cx + posVariation);
circleElement.setAttribute('cy', circle.cy + posVariation);
});
this.updatePath();
if (this.x + this.size * 2 < 0) {
sunset.removeChild(this.element);
return false;
}
return true;
}
}
function createCloud() {
clouds.push(new Cloud());
}
function animateClouds() {
for (let i = clouds.length - 1; i >= 0; i--) {
if (!clouds[i].move()) {
clouds.splice(i, 1);
}
}
requestAnimationFrame(animateClouds);
}
function createRay() {
const ray = document.createElement('div');
ray.classList.add('ray');
const angle = Math.random() * 180 - 90;
ray.style.transform = `rotate(${angle}deg)`;
ray.style.height = '200px';
sunset.appendChild(ray);
let length = 200;
let growing = true;
let swayAngle = angle;
let swayDirection = Math.random() < 0.5 ? -1 : 1;
function animateRay() {
if (growing) {
length += 0.5;
if (length > 300) growing = false;
} else {
length -= 0.5;
if (length < 200) growing = true;
}
swayAngle += 0.05 * swayDirection;
if (Math.abs(swayAngle - angle) > 5) {
swayDirection *= -1;
}
ray.style.height = `${length}px`;
ray.style.transform = `rotate(${swayAngle}deg)`;
requestAnimationFrame(animateRay);
}
animateRay();
}
function createBird(direction = 'right', isInFlock = false) {
const bird = document.createElement('div');
bird.classList.add('bird');
bird.style.top = `${Math.random() * 60 + 10}%`;
bird.style.left = direction === 'right' ? '-20px' : 'calc(100% + 20px)';
bird.style.zIndex = Math.random() < 0.3 ? '7' : '1';
sunset.appendChild(bird);
let position = direction === 'right' ? -20 : window.innerWidth + 20;
let flapInterval = Math.random() * 3000;
let lastFlapTime = Date.now();
let isFlapped = false;
function animateBird() {
position += direction === 'right' ? 0.5 : -0.5;
bird.style.left = `${position}px`;
if (Date.now() - lastFlapTime > flapInterval) {
isFlapped = !isFlapped;
bird.style.clipPath = isFlapped
? 'polygon(0% 30%, 50% 0%, 100% 30%, 80% 30%, 50% 70%, 20% 30%)'
: 'polygon(0% 30%, 50% 0%, 100% 30%, 80% 30%, 50% 60%, 20% 30%)';
lastFlapTime = Date.now();
flapInterval = Math.random() * 6000 + 2000;
}
if ((direction === 'right' && position > window.innerWidth + 20) ||
(direction === 'left' && position < -20)) {
sunset.removeChild(bird);
} else {
requestAnimationFrame(animateBird);
}
}
animateBird();
if (!isInFlock && Math.random() < 0.2) {
for (let i = 0; i < Math.floor(Math.random() * 5) + 2; i++) {
setTimeout(() => createBird(direction, true), Math.random() * 1000);
}
}
}
function spawnBird() {
if (!(birdCount > maxBirdCount)) {
const direction = Math.random() < 0.5 ? 'right' : 'left';
createBird(direction);
}
setTimeout(spawnBird, Math.random() * 10000 + 5000);
}
function animateSun() {
let sunPosition = -40;
function moveSun() {
sunPosition += 0.05;
sun.style.bottom = `${sunPosition}px`;
sunset.style.background = `linear-gradient(0deg,
hsl(${15 - sunPosition / 2}, 100%, ${60 + sunPosition / 4}%),
hsl(${30 - sunPosition / 2}, 100%, ${70 + sunPosition / 4}%),
hsl(${45 - sunPosition / 2}, 100%, ${80 + sunPosition / 4}%),
hsl(${190 - sunPosition / 2}, 50%, ${50 + sunPosition / 4}%))`;
if (sunPosition < 100) {
requestAnimationFrame(moveSun);
}
}
moveSun();
}
const numRays = 30;
// Initialize
for (let i = 0; i < numRays; i++) {
createRay();
}
animateSun();
setInterval(createCloud, 3000);
animateClouds();
spawnBird();
// Responsive design
window.addEventListener('resize', () => {
document.querySelectorAll('.cloud, .ray, .bird').forEach(el => el.remove());
clouds.length = 0;
birdCount = 0;
for (let i = 0; i < 20; i++) {
createRay();
}
spawnBird();
});
// The following is the old script code that worked relatively well
// const sunset = document.getElementById('sunset');
// const sun = document.querySelector('.sun');
// function createCloud() {
// const cloud = document.createElement('div');
// cloud.classList.add('cloud');
// const size = Math.random() * 100 + 50;
// cloud.style.width = `${size}px`;
// cloud.style.height = `${size * 0.6}px`;
// cloud.style.top = `${Math.random() * 40}%`;
// cloud.style.right = '-100px';
// cloud.style.filter = `blur(${Math.random() * 3 + 1}px)`;
// // Create irregular shape
// const blobPath = `M${Math.random() * 30 + 35},${Math.random() * 20 + 40}
// Q${Math.random() * 40 + 30},${Math.random() * 20 + 20} ${Math.random() * 30 + 60},${Math.random() * 20 + 40}
// T${Math.random() * 30 + 90},${Math.random() * 20 + 40}
// T${Math.random() * 30 + 60},${Math.random() * 20 + 60}
// T${Math.random() * 30 + 30},${Math.random() * 20 + 60} Z`;
// cloud.style.clipPath = `path('${blobPath}')`;
// sunset.appendChild(cloud);
// let position = window.innerWidth;
// function moveCloud() {
// position -= 0.2;
// cloud.style.right = `${-position}px`;
// if (position < -100) {
// sunset.removeChild(cloud);
// } else {
// requestAnimationFrame(moveCloud);
// }
// }
// moveCloud();
// }
// function createRay() {
// const ray = document.createElement('div');
// ray.classList.add('ray');
// const angle = Math.random() * 180 - 90;
// ray.style.transform = `rotate(${angle}deg)`;
// ray.style.height = '200px';
// sunset.appendChild(ray);
// let length = 200;
// let growing = true;
// let swayAngle = angle;
// let swayDirection = Math.random() < 0.5 ? -1 : 1;
// function animateRay() {
// if (growing) {
// length += 0.5;
// if (length > 300) growing = false;
// } else {
// length -= 0.5;
// if (length < 200) growing = true;
// }
// swayAngle += 0.05 * swayDirection;
// if (Math.abs(swayAngle - angle) > 5) {
// swayDirection *= -1;
// }
// ray.style.height = `${length}px`;
// ray.style.transform = `rotate(${swayAngle}deg)`;
// requestAnimationFrame(animateRay);
// }
// animateRay();
// }
// function createBird(direction = 'right', isInFlock = false) {
// const bird = document.createElement('div');
// bird.classList.add('bird');
// bird.style.top = `${Math.random() * 60 + 10}%`;
// bird.style.left = direction === 'right' ? '-20px' : 'calc(100% + 20px)';
// sunset.appendChild(bird);
// let position = direction === 'right' ? -20 : window.innerWidth + 20;
// let flapInterval = Math.random() * 3000; // 2 to 8 seconds
// let lastFlapTime = Date.now();
// let isFlapped = false;
// function animateBird() {
// position += direction === 'right' ? 0.5 : -0.5;
// bird.style.left = `${position}px`;
// // Flap wings
// if (Date.now() - lastFlapTime > flapInterval) {
// isFlapped = !isFlapped;
// bird.style.clipPath = isFlapped
// ? 'polygon(0% 30%, 50% 0%, 100% 30%, 80% 30%, 50% 70%, 20% 30%)'
// : 'polygon(0% 30%, 50% 0%, 100% 30%, 80% 30%, 50% 60%, 20% 30%)';
// lastFlapTime = Date.now();
// flapInterval = Math.random() * 6000 + 2000; // New random interval
// }
// if ((direction === 'right' && position > window.innerWidth + 20) ||
// (direction === 'left' && position < -20)) {
// sunset.removeChild(bird);
// } else {
// requestAnimationFrame(animateBird);
// }
// }
// animateBird();
// // Spawn flock
// if (!isInFlock && Math.random() < 0.2) { // 20% chance to spawn a flock
// for (let i = 0; i < Math.floor(Math.random() * 5) + 2; i++) { // 2 to 6 additional birds
// setTimeout(() => createBird(direction, true), Math.random() * 1000);
// }
// }
// }
// function spawnBird() {
// const direction = Math.random() < 0.5 ? 'right' : 'left';
// createBird(direction);
// setTimeout(spawnBird, Math.random() * 10000 + 5000); // Spawn every 5 to 15 seconds
// }
// function animateSun() {
// let sunPosition = -40;
// function moveSun() {
// sunPosition += 0.05;
// sun.style.bottom = `${sunPosition}px`;
// sunset.style.background = `linear-gradient(0deg,
// hsl(${15 - sunPosition / 2}, 100%, ${60 + sunPosition / 4}%),
// hsl(${30 - sunPosition / 2}, 100%, ${70 + sunPosition / 4}%),
// hsl(${45 - sunPosition / 2}, 100%, ${80 + sunPosition / 4}%),
// hsl(${190 - sunPosition / 2}, 50%, ${50 + sunPosition / 4}%))`;
// if (sunPosition < 100) {
// requestAnimationFrame(moveSun);
// }
// }
// moveSun();
// }
// // Initialize
// for (let i = 0; i < 20; i++) {
// createRay();
// }
// animateSun();
// setInterval(createCloud, 3000); // Create a new cloud every 3 seconds
// spawnBird(); // Start spawning birds
// // Responsive design
// window.addEventListener('resize', () => {
// // Clear existing elements
// document.querySelectorAll('.cloud, .ray, .bird').forEach(el => el.remove());
// // Reinitialize
// for (let i = 0; i < 20; i++) {
// createRay();
// }
// spawnBird();
// });