-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathanimatedBackground.html
143 lines (116 loc) · 3.65 KB
/
animatedBackground.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
<style>
.main {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
svg {
width: 100%;
height: 100%;
transform: scale(1, -1);
transform-origin: center;
}
#myPath {
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: draw 100s forwards;
stroke-width: 0.01;
fill: "none";
}
@keyframes draw {
to {
stroke-dashoffset: 0;
}
}
</style>
<div class="main">
<svg
class="svg-container"
viewbox="0 0 115 72"
xmlns="http://www.w3.org/2000/svg"
></svg>
</div>
<script defer type="module">
function splitPathIntoSubpathsAndAddCSS(pathData, rateOfDrawing = 10) {
// units/second
const subpaths = pathData.split(/(?=M)/);
let styleContent = "";
let cumulativeAnimationTime = 0;
// Create a temporary SVG to compute path lengths
const tempSvg = document.createElementNS(
"http://www.w3.org/2000/svg",
"svg",
);
console.log(document.body);
document.body.appendChild(tempSvg);
const subpathElements = subpaths.map((subpath, index) => {
const pathElement = document.createElementNS(
"http://www.w3.org/2000/svg",
"path",
);
pathElement.setAttribute("d", subpath.trim());
pathElement.setAttribute("fill", "none");
pathElement.setAttribute("stroke", "black");
// Append to tempSvg to compute its length
tempSvg.appendChild(pathElement);
const pathLength = pathElement.getTotalLength();
tempSvg.removeChild(pathElement);
pathElement.setAttribute("id", `line${index + 1}`);
const animationDurationForThisPath = pathLength / rateOfDrawing;
// Create the style for this path
styleContent += `
#line${index + 1} {
stroke-dasharray: ${pathLength};
stroke-dashoffset: ${pathLength};
animation-name: drawLine${index + 1};
animation-duration: ${animationDurationForThisPath}s;
animation-delay: ${cumulativeAnimationTime % 1.4}s;
animation-fill-mode: forwards;
stroke-width: .1;
}
@keyframes drawLine${index + 1} {
to {
stroke-dashoffset: 0;
}
}
`;
// Update cumulative animation time for the next iteration
cumulativeAnimationTime += animationDurationForThisPath;
return pathElement;
});
// Remove the temporary SVG
document.body.removeChild(tempSvg);
// Add the styles
const styleElement = document.createElement("style");
styleElement.textContent = styleContent;
document.head.appendChild(styleElement);
return subpathElements;
}
const pathData = ``;
const subpathElements = splitPathIntoSubpathsAndAddCSS(pathData);
const svgElement = document.querySelector(".svg-container");
subpathElements.forEach((element) => svgElement.appendChild(element));
setTimeout(() => {
fadeIn(document.querySelector("nav"), 1500);
document.querySelectorAll(".fade-in").forEach((el) => {
fadeIn(el, 1500);
});
}, 1000);
function fadeIn(el, duration = 2000) {
let start;
function animate(timestamp) {
if (!start) start = timestamp;
const elapsed = timestamp - start;
// Calculate the current opacity
const opacity = Math.min(elapsed / duration, 1);
el.style.opacity = opacity;
// Continue the animation until the duration is reached
if (elapsed < duration) {
requestAnimationFrame(animate);
}
}
// Start the animation
requestAnimationFrame(animate);
}
</script>