refactor(client): use composition api

This commit is contained in:
syuilo 2022-06-21 14:39:23 +09:00 committed by Chloe Kudryavtsev
parent a1e3eada2e
commit 82f506e62f

View file

@ -9,82 +9,71 @@
<polygon <polygon
:points="polygonPoints" :points="polygonPoints"
fill="#fff" fill="#fff"
fill-opacity="0.5"/> fill-opacity="0.5"
/>
<polyline <polyline
:points="polylinePoints" :points="polylinePoints"
fill="none" fill="none"
stroke="#fff" stroke="#fff"
stroke-width="2"/> stroke-width="2"
/>
<circle <circle
:cx="headX" :cx="headX"
:cy="headY" :cy="headY"
r="3" r="3"
fill="#fff"/> fill="#fff"
/>
</mask> </mask>
</defs> </defs>
<rect <rect
x="-10" y="-10" x="-10" y="-10"
:width="viewBoxX + 20" :height="viewBoxY + 20" :width="viewBoxX + 20" :height="viewBoxY + 20"
:style="`stroke: none; fill: url(#${ gradientId }); mask: url(#${ maskId })`"/> :style="`stroke: none; fill: url(#${ gradientId }); mask: url(#${ maskId })`"
/>
</svg> </svg>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { onUnmounted, watch } from 'vue';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import * as os from '@/os';
export default defineComponent({ const props = defineProps<{
props: { src: number[];
src: { }>();
type: Array,
required: true
}
},
data() {
return {
viewBoxX: 50,
viewBoxY: 30,
gradientId: uuid(),
maskId: uuid(),
polylinePoints: '',
polygonPoints: '',
headX: null,
headY: null,
clock: null
};
},
watch: {
src() {
this.draw();
}
},
created() {
this.draw();
// VueWatch const viewBoxX = 50;
this.clock = window.setInterval(this.draw, 1000); const viewBoxY = 50;
}, const gradientId = uuid();
beforeUnmount() { const maskId = uuid();
window.clearInterval(this.clock); let polylinePoints = $ref('');
}, let polygonPoints = $ref('');
methods: { let headX = $ref<number | null>(null);
draw() { let headY = $ref<number | null>(null);
const stats = this.src.slice().reverse(); let clock = $ref<number | null>(null);
const peak = Math.max.apply(null, stats) || 1;
const polylinePoints = stats.map((n, i) => [ function draw(): void {
i * (this.viewBoxX / (stats.length - 1)), const stats = props.src.slice().reverse();
(1 - (n / peak)) * this.viewBoxY const peak = Math.max.apply(null, stats) || 1;
]);
this.polylinePoints = polylinePoints.map(xy => `${xy[0]},${xy[1]}`).join(' '); const _polylinePoints = stats.map((n, i) => [
i * (viewBoxX / (stats.length - 1)),
(1 - (n / peak)) * viewBoxY,
]);
this.polygonPoints = `0,${ this.viewBoxY } ${ this.polylinePoints } ${ this.viewBoxX },${ this.viewBoxY }`; polylinePoints = _polylinePoints.map(xy => `${xy[0]},${xy[1]}`).join(' ');
this.headX = polylinePoints[polylinePoints.length - 1][0]; polygonPoints = `0,${ viewBoxY } ${ polylinePoints } ${ viewBoxX },${ viewBoxY }`;
this.headY = polylinePoints[polylinePoints.length - 1][1];
} headX = _polylinePoints[_polylinePoints.length - 1][0];
} headY = _polylinePoints[_polylinePoints.length - 1][1];
}
watch(() => props.src, draw, { immediate: true });
// VueWatch
clock = window.setInterval(draw, 1000);
onUnmounted(() => {
window.clearInterval(clock);
}); });
</script> </script>