1
1
<script lang="ts" setup>
2
- import { ref } from ' vue'
2
+ import { onMounted , onUnmounted , ref } from ' vue'
3
3
import { useFps , useRafFn } from ' @vueuse/core'
4
4
import type { LechesControl } from ' ../types'
5
5
6
6
defineProps <{
7
7
label: string
8
8
control: LechesControl
9
9
}>()
10
- const width = 160
11
- const height = 40
10
+
11
+ const containerRef = ref <HTMLElement | null >(null )
12
+ const width = ref (160 )
13
+ const height = ref (40 )
12
14
const strokeWidth = 2
13
15
const updateInterval = 100 // Update interval in milliseconds
14
16
const topOffset = 20 // Offset from the top
15
17
const fps = useFps ({ every: updateInterval })
16
18
17
19
const points = ref (' ' )
18
20
const frameTimes = ref ([])
19
- const maxFrames = ref (width / strokeWidth )
21
+ const maxFrames = ref (width . value / strokeWidth )
20
22
21
23
let lastUpdateTime = performance .now ()
24
+ let resizeObserver: ResizeObserver | null = null
25
+
26
+ onMounted (() => {
27
+ if (containerRef .value ) {
28
+ resizeObserver = new ResizeObserver ((entries ) => {
29
+ for (const entry of entries ) {
30
+ width .value = entry .contentRect .width
31
+ maxFrames .value = Math .floor (width .value / strokeWidth )
32
+ // Trim frame times if needed after resize
33
+ if (frameTimes .value .length > maxFrames .value ) {
34
+ frameTimes .value = frameTimes .value .slice (- maxFrames .value )
35
+ }
36
+ }
37
+ })
38
+ resizeObserver .observe (containerRef .value )
39
+ }
40
+ })
41
+
42
+ onUnmounted (() => {
43
+ if (resizeObserver ) {
44
+ resizeObserver .disconnect ()
45
+ }
46
+ })
22
47
23
48
useRafFn (({ timestamp }) => {
24
49
if (timestamp - lastUpdateTime >= updateInterval ) {
@@ -34,7 +59,7 @@ useRafFn(({ timestamp }) => {
34
59
.map (
35
60
(fps , index ) =>
36
61
` ${index * strokeWidth },${
37
- height + topOffset - strokeWidth / 2 - (fps * (height + topOffset - strokeWidth )) / 120
62
+ height . value + topOffset - strokeWidth / 2 - (fps * (height . value + topOffset - strokeWidth )) / 120
38
63
} ` ,
39
64
)
40
65
.join (' ' )
@@ -43,20 +68,20 @@ useRafFn(({ timestamp }) => {
43
68
</script >
44
69
45
70
<template >
46
- <div class =" tl-flex tl-px-4 tl-justify-between tl-gap-4 tl- items-center tl-mb-2" >
71
+ <div class =" tl-flex tl-px-4 tl-items-center tl-mb-2" >
47
72
<label class =" tl-text-gray-500 tl-w-1/3" >{{ label }}</label >
48
73
49
74
<div
75
+ ref =" containerRef"
50
76
class ="
51
77
tl-relative
52
78
tl-w-2/3
53
- tl-p -1
79
+ tl-py -1
54
80
tl-rounded
55
81
tl-text-right
56
82
tl-text-xs
57
83
tl-text-gray-400
58
84
tl-bg-gray-100
59
- tl-focus:border-gray-200
60
85
tl-outline-none
61
86
tl-border-none
62
87
tl-font-sans
0 commit comments