client: refactor sparkle.vue to composition api

This commit is contained in:
Norm 2022-09-10 00:42:39 -04:00 committed by Johann150
parent 2e8e475f32
commit f4b9244a88
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -63,62 +63,60 @@
</span> </span>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, onMounted, onUnmounted, ref } from 'vue'; import { onMounted, onUnmounted } from 'vue';
export default defineComponent({ type Particle = {
setup() { id: string;
const particles = ref([]); x: number;
const el = ref<HTMLElement>(); y: number;
const width = ref(0); size: number;
const height = ref(0); dur: number;
const colors = ['#FF1493', '#00FFFF', '#FFE202', '#FFE202', '#FFE202']; color: string;
let stop = false; };
let ro: ResizeObserver | undefined;
onMounted(() => { let particles: Particle[] = $ref([]);
ro = new ResizeObserver(() => { let el: HTMLElement = $ref();
width.value = el.value?.offsetWidth + 64; let width = $ref(0);
height.value = el.value?.offsetHeight + 64; let height = $ref(0);
}); const colors = ['#FF1493', '#00FFFF', '#FFE202', '#FFE202', '#FFE202'];
ro.observe(el.value); let stop = false;
const add = () => { let ro: ResizeObserver | undefined;
if (stop) return;
const x = (Math.random() * (width.value - 64));
const y = (Math.random() * (height.value - 64));
const sizeFactor = Math.random();
const particle = {
id: Math.random().toString(),
x,
y,
size: 0.2 + ((sizeFactor / 10) * 3),
dur: 1000 + (sizeFactor * 1000),
color: colors[Math.floor(Math.random() * colors.length)],
};
particles.value.push(particle);
window.setTimeout(() => {
particles.value = particles.value.filter(x => x.id !== particle.id);
}, particle.dur - 100);
window.setTimeout(() => { onMounted(() => {
add(); ro = new ResizeObserver(() => {
}, 500 + (Math.random() * 500)); width = el.offsetWidth + 64;
}; height = el.offsetHeight + 64;
add(); });
}); ro.observe(el);
const add = (): void => {
onUnmounted(() => { if (stop) return;
if (ro) ro.disconnect(); const x = (Math.random() * (width - 64));
stop = true; const y = (Math.random() * (height - 64));
}); const sizeFactor = Math.random();
const particle = {
return { id: Math.random().toString(),
el, x,
width, y,
height, size: 0.2 + ((sizeFactor / 10) * 3),
particles, dur: 1000 + (sizeFactor * 1000),
color: colors[Math.floor(Math.random() * colors.length)],
}; };
}, particles.push(particle);
window.setTimeout(() => {
particles = particles.filter(p => p.id !== particle.id);
}, particle.dur - 100);
window.setTimeout(() => {
add();
}, 500 + (Math.random() * 500));
};
add();
});
onUnmounted(() => {
if (ro) ro.disconnect();
stop = true;
}); });
</script> </script>