1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 06:28:42 +01:00
Files
dozzle/docs/.vitepress/theme/components/Counter.vue
2024-09-03 09:01:21 -07:00

40 lines
828 B
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
const {
start,
end,
duration,
formatter = (value: number) => value.toLocaleString(),
} = defineProps<{
start: number;
end: number;
duration: number;
formatter?: (value: number) => string;
}>();
const text = ref(0);
onMounted(() => {
let startTimestamp: number | undefined = undefined;
const step = (timestamp: number) => {
if (!startTimestamp) startTimestamp = timestamp;
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
text.value = Math.floor(progress * (end - start) + start);
if (progress < 1) {
requestAnimationFrame(step);
}
};
requestAnimationFrame(step);
});
const formmated = computed(() => formatter(text.value));
</script>
<template>
{{ formmated }}
</template>