This commit is contained in:
@@ -3,46 +3,43 @@ import { rand } from "@vueuse/core";
|
|||||||
import { ref, onMounted, onUnmounted, nextTick } from "vue";
|
import { ref, onMounted, onUnmounted, nextTick } from "vue";
|
||||||
|
|
||||||
interface Item {
|
interface Item {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
dx: number;
|
dx: number;
|
||||||
dy: number;
|
dy: number;
|
||||||
content: string;
|
content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = ref<HTMLDivElement | null>(null);
|
const container = ref<HTMLDivElement | null>(null);
|
||||||
const itemEls = ref<HTMLDivElement[]>([]);
|
const itemEls = ref<HTMLDivElement[]>([]);
|
||||||
|
|
||||||
const phrases = [
|
const phrases = [
|
||||||
"Welcome to my website",
|
"Welcome to my website",
|
||||||
"I'm looking to do a big revamp",
|
"<3 thx for visiting",
|
||||||
"A4 sheets of paper are what I'm used to",
|
"please get it touch ^_^",
|
||||||
"I'd love to know your recommendations",
|
"reccomend me your music :P",
|
||||||
"for very short books",
|
|
||||||
"Message me by any means necessary",
|
|
||||||
"I like anime, all kinds of music and sci fic",
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// Non-reactive animation state to avoid triggering Vue re-renders every frame
|
// Non-reactive animation state to avoid triggering Vue re-renders every frame
|
||||||
const animState = phrases.map((text, i) => ({
|
const animState = phrases.map((text, i) => ({
|
||||||
x: 0,
|
x: 0,
|
||||||
y: i * 20,
|
y: i * 20,
|
||||||
dx: rand(0, 60) / 100,
|
dx: rand(0, 60) / 100,
|
||||||
dy: 1.0,
|
dy: 1.0,
|
||||||
content: text,
|
content: text,
|
||||||
cachedW: 0,
|
cachedW: 0,
|
||||||
cachedH: 0,
|
cachedH: 0,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Reactive items only for initial render
|
// Reactive items only for initial render
|
||||||
const items = ref<Item[]>(
|
const items = ref<Item[]>(
|
||||||
animState.map((s) => ({
|
animState.map((s) => ({
|
||||||
x: s.x,
|
x: s.x,
|
||||||
y: s.y,
|
y: s.y,
|
||||||
dx: s.dx,
|
dx: s.dx,
|
||||||
dy: s.dy,
|
dy: s.dy,
|
||||||
content: s.content,
|
content: s.content,
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
let rafId = 0;
|
let rafId = 0;
|
||||||
@@ -52,76 +49,76 @@ let lastFrameTime = 0;
|
|||||||
const FRAME_INTERVAL = 1000 / 30;
|
const FRAME_INTERVAL = 1000 / 30;
|
||||||
|
|
||||||
function measureSizes() {
|
function measureSizes() {
|
||||||
const c = container.value;
|
const c = container.value;
|
||||||
if (c) {
|
if (c) {
|
||||||
cachedCW = c.clientWidth;
|
cachedCW = c.clientWidth;
|
||||||
cachedCH = c.clientHeight;
|
cachedCH = c.clientHeight;
|
||||||
}
|
|
||||||
itemEls.value.forEach((el, i) => {
|
|
||||||
if (el && animState[i]) {
|
|
||||||
animState[i].cachedW = el.offsetWidth;
|
|
||||||
animState[i].cachedH = el.offsetHeight;
|
|
||||||
}
|
}
|
||||||
});
|
itemEls.value.forEach((el, i) => {
|
||||||
|
if (el && animState[i]) {
|
||||||
|
animState[i].cachedW = el.offsetWidth;
|
||||||
|
animState[i].cachedH = el.offsetHeight;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function animate(timestamp: number) {
|
function animate(timestamp: number) {
|
||||||
if (!cachedCW || !cachedCH) {
|
if (!cachedCW || !cachedCH) {
|
||||||
|
rafId = requestAnimationFrame(animate);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timestamp - lastFrameTime < FRAME_INTERVAL) {
|
||||||
|
rafId = requestAnimationFrame(animate);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lastFrameTime = timestamp;
|
||||||
|
|
||||||
|
for (let i = 0; i < animState.length; i++) {
|
||||||
|
const s = animState[i];
|
||||||
|
const el = itemEls.value[i];
|
||||||
|
if (!el) continue;
|
||||||
|
|
||||||
|
s.x += s.dx;
|
||||||
|
s.y += s.dy;
|
||||||
|
|
||||||
|
if (s.x < 0 || s.x > cachedCW - s.cachedW) s.dx *= -1;
|
||||||
|
if (s.y < 0 || s.y > cachedCH - s.cachedH) s.dy *= -1;
|
||||||
|
|
||||||
|
el.style.transform = `translate(${s.x}px, ${s.y}px)`;
|
||||||
|
}
|
||||||
|
|
||||||
rafId = requestAnimationFrame(animate);
|
rafId = requestAnimationFrame(animate);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timestamp - lastFrameTime < FRAME_INTERVAL) {
|
|
||||||
rafId = requestAnimationFrame(animate);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
lastFrameTime = timestamp;
|
|
||||||
|
|
||||||
for (let i = 0; i < animState.length; i++) {
|
|
||||||
const s = animState[i];
|
|
||||||
const el = itemEls.value[i];
|
|
||||||
if (!el) continue;
|
|
||||||
|
|
||||||
s.x += s.dx;
|
|
||||||
s.y += s.dy;
|
|
||||||
|
|
||||||
if (s.x < 0 || s.x > cachedCW - s.cachedW) s.dx *= -1;
|
|
||||||
if (s.y < 0 || s.y > cachedCH - s.cachedH) s.dy *= -1;
|
|
||||||
|
|
||||||
el.style.transform = `translate(${s.x}px, ${s.y}px)`;
|
|
||||||
}
|
|
||||||
|
|
||||||
rafId = requestAnimationFrame(animate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let resizeObserver: ResizeObserver;
|
let resizeObserver: ResizeObserver;
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await nextTick();
|
await nextTick();
|
||||||
measureSizes();
|
measureSizes();
|
||||||
rafId = requestAnimationFrame(animate);
|
rafId = requestAnimationFrame(animate);
|
||||||
|
|
||||||
resizeObserver = new ResizeObserver(measureSizes);
|
resizeObserver = new ResizeObserver(measureSizes);
|
||||||
resizeObserver.observe(container.value!);
|
resizeObserver.observe(container.value!);
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
cancelAnimationFrame(rafId);
|
cancelAnimationFrame(rafId);
|
||||||
resizeObserver?.disconnect();
|
resizeObserver?.disconnect();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div ref="container" class="w-full h-full relative overflow-hidden">
|
<div ref="container" class="w-full h-full relative overflow-hidden">
|
||||||
<div
|
<div
|
||||||
v-for="(item, i) in items"
|
v-for="(item, i) in items"
|
||||||
:key="i"
|
:key="i"
|
||||||
ref="itemEls"
|
ref="itemEls"
|
||||||
class="absolute w-fit h-fit"
|
class="absolute w-fit h-fit"
|
||||||
>
|
>
|
||||||
<h1>
|
<h1>
|
||||||
{{ item.content }}
|
{{ item.content }}
|
||||||
</h1>
|
</h1>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user