aboutsummaryrefslogtreecommitdiff
path: root/src/component/MosaicBackground.astro
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2025-08-10 13:27:49 +0200
committerWilliam Hergès <william@herges.fr>2025-08-10 13:27:49 +0200
commit5d703baed11f8f1b44df5ed7e03142cbdce839c5 (patch)
tree7aa1ab32ca4b9b0c5d5643d3c28dba7ae0eccb1b /src/component/MosaicBackground.astro
parent3f800fb6724a15e6355b73304246a4711231d28f (diff)
refactor(background): use event listener instead of onload
Diffstat (limited to 'src/component/MosaicBackground.astro')
-rw-r--r--src/component/MosaicBackground.astro16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/component/MosaicBackground.astro b/src/component/MosaicBackground.astro
index c8e01f5..8c38bcf 100644
--- a/src/component/MosaicBackground.astro
+++ b/src/component/MosaicBackground.astro
@@ -7,7 +7,7 @@ const { height = "400" } = Astro.props;
</div>
<script>
- window.onload = () => {
+ window.addEventListener("load", () => {
// because Astro doesn't support defer
document.querySelectorAll(".bg").forEach((e) => {
const children = e.children;
@@ -24,9 +24,15 @@ const { height = "400" } = Astro.props;
// adding elements
while (i < children.length && currentWidth <= max) {
let current = children[i++];
- let ratio = current.clientWidth / current.clientHeight;
- line.appendChild(current.cloneNode(true));
- currentWidth += ratio * height;
+ if (!(current instanceof HTMLScriptElement)) {
+ let ratio = current.clientWidth / current.clientHeight;
+ if (!isNaN(ratio)) {
+ line.appendChild(current.cloneNode(true));
+ currentWidth += ratio * height;
+ } else {
+ console.error("is NaN", current)
+ }
+ }
}
// setting height
let h = (max * height) / currentWidth;
@@ -39,5 +45,5 @@ const { height = "400" } = Astro.props;
}
e.replaceChildren(...newChildren);
});
- };
+ })
</script>