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,29 +63,36 @@
</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;
color: string;
};
let particles: Particle[] = $ref([]);
let el: HTMLElement = $ref();
let width = $ref(0);
let height = $ref(0);
const colors = ['#FF1493', '#00FFFF', '#FFE202', '#FFE202', '#FFE202']; const colors = ['#FF1493', '#00FFFF', '#FFE202', '#FFE202', '#FFE202'];
let stop = false; let stop = false;
let ro: ResizeObserver | undefined; let ro: ResizeObserver | undefined;
onMounted(() => { onMounted(() => {
ro = new ResizeObserver(() => { ro = new ResizeObserver(() => {
width.value = el.value?.offsetWidth + 64; width = el.offsetWidth + 64;
height.value = el.value?.offsetHeight + 64; height = el.offsetHeight + 64;
}); });
ro.observe(el.value); ro.observe(el);
const add = () => { const add = (): void => {
if (stop) return; if (stop) return;
const x = (Math.random() * (width.value - 64)); const x = (Math.random() * (width - 64));
const y = (Math.random() * (height.value - 64)); const y = (Math.random() * (height - 64));
const sizeFactor = Math.random(); const sizeFactor = Math.random();
const particle = { const particle = {
id: Math.random().toString(), id: Math.random().toString(),
@ -95,9 +102,9 @@ export default defineComponent({
dur: 1000 + (sizeFactor * 1000), dur: 1000 + (sizeFactor * 1000),
color: colors[Math.floor(Math.random() * colors.length)], color: colors[Math.floor(Math.random() * colors.length)],
}; };
particles.value.push(particle); particles.push(particle);
window.setTimeout(() => { window.setTimeout(() => {
particles.value = particles.value.filter(x => x.id !== particle.id); particles = particles.filter(p => p.id !== particle.id);
}, particle.dur - 100); }, particle.dur - 100);
window.setTimeout(() => { window.setTimeout(() => {
@ -111,15 +118,6 @@ export default defineComponent({
if (ro) ro.disconnect(); if (ro) ro.disconnect();
stop = true; stop = true;
}); });
return {
el,
width,
height,
particles,
};
},
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>