forked from FoundKeyGang/FoundKey
client: refactor sparkle.vue to composition api
This commit is contained in:
parent
2e8e475f32
commit
f4b9244a88
1 changed files with 50 additions and 52 deletions
|
@ -63,29 +63,36 @@
|
|||
</span>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, onMounted, onUnmounted, ref } from 'vue';
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, onUnmounted } from 'vue';
|
||||
|
||||
export default defineComponent({
|
||||
setup() {
|
||||
const particles = ref([]);
|
||||
const el = ref<HTMLElement>();
|
||||
const width = ref(0);
|
||||
const height = ref(0);
|
||||
type Particle = {
|
||||
id: string;
|
||||
x: number;
|
||||
y: number;
|
||||
size: number;
|
||||
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'];
|
||||
let stop = false;
|
||||
let ro: ResizeObserver | undefined;
|
||||
|
||||
onMounted(() => {
|
||||
ro = new ResizeObserver(() => {
|
||||
width.value = el.value?.offsetWidth + 64;
|
||||
height.value = el.value?.offsetHeight + 64;
|
||||
width = el.offsetWidth + 64;
|
||||
height = el.offsetHeight + 64;
|
||||
});
|
||||
ro.observe(el.value);
|
||||
const add = () => {
|
||||
ro.observe(el);
|
||||
const add = (): void => {
|
||||
if (stop) return;
|
||||
const x = (Math.random() * (width.value - 64));
|
||||
const y = (Math.random() * (height.value - 64));
|
||||
const x = (Math.random() * (width - 64));
|
||||
const y = (Math.random() * (height - 64));
|
||||
const sizeFactor = Math.random();
|
||||
const particle = {
|
||||
id: Math.random().toString(),
|
||||
|
@ -95,9 +102,9 @@ export default defineComponent({
|
|||
dur: 1000 + (sizeFactor * 1000),
|
||||
color: colors[Math.floor(Math.random() * colors.length)],
|
||||
};
|
||||
particles.value.push(particle);
|
||||
particles.push(particle);
|
||||
window.setTimeout(() => {
|
||||
particles.value = particles.value.filter(x => x.id !== particle.id);
|
||||
particles = particles.filter(p => p.id !== particle.id);
|
||||
}, particle.dur - 100);
|
||||
|
||||
window.setTimeout(() => {
|
||||
|
@ -111,15 +118,6 @@ export default defineComponent({
|
|||
if (ro) ro.disconnect();
|
||||
stop = true;
|
||||
});
|
||||
|
||||
return {
|
||||
el,
|
||||
width,
|
||||
height,
|
||||
particles,
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
|
Loading…
Reference in a new issue