-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapes.js
51 lines (39 loc) · 1.5 KB
/
shapes.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
function animateShapes() {
const shapes = document.querySelectorAll('.shape');
shapes.forEach(shape => {
// Set initial position
// setPosition(shape);
// Animate
animateShape(shape);
});
}
function setPosition(element) {
const maxX = window.innerWidth - element.offsetWidth;
const maxY = window.innerHeight - element.offsetHeight;
const x = Math.random() * maxX;
const y = Math.random() * maxY;
element.style.position = 'absolute';
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function animateShape(element) {
const animationDuration = 15 + Math.random() * 10; // Between 15 and 25 seconds
const xDistance = Math.random() * 100 - 50; // Move between -50px and 50px horizontally
const yDistance = Math.random() * 100 - 50; // Move between -50px and 50px vertically
// Get the current position from the element's style
const startX = parseFloat(element.style.left) || 0;
const startY = parseFloat(element.style.top) || 0;
const endX = startX + xDistance;
const endY = startY + yDistance;
element.animate([
{ transform: `translate(${0}px, ${0}px)` },
{ transform: `translate(${xDistance}px, ${yDistance}px)` }
], {
duration: animationDuration * 1000,
easing: 'ease-in-out',
iterations: Infinity,
direction: 'alternate'
});
}
// Call the function when the DOM is loaded
document.addEventListener('DOMContentLoaded', animateShapes);