wangzhibo
昨天 5e756b5a69bcd957f8eafc5f99a4f6413691d6dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { onBeforeUnmount, ref, type Ref } from 'vue';
import type { LngLat } from 'vue-tianditu2';
import { toStamp } from '../hooks/use-map';
 
export type TrackPoint = { longitude: number; latitude: number; time?: any; speed?: number };
 
export function useTrackPlayback(points: Ref<TrackPoint[]>) {
    const playing = ref(false);
    const speed = ref(5);
    const progress = ref(0);
    const playPos = ref<LngLat | null>(null);
    let raf = 0;
    let startedAt = 0;
    let elapsed = 0;
 
    function path(): LngLat[] {
        return points.value
            .filter(p => Number(p.longitude) && Number(p.latitude))
            .map(p => [Number(p.longitude), Number(p.latitude)] as LngLat);
    }
 
    function duration() {
        const list = points.value;
        if (list.length < 2) return 8000;
        const t0 = toStamp(list[0].time);
        const t1 = toStamp(list[list.length - 1].time);
        const real = t1 > t0 ? t1 - t0 : list.length * 2000;
        return Math.max(real, 4000);
    }
 
    function at(ratio: number): LngLat | null {
        const list = path();
        if (!list.length) return null;
        if (list.length === 1) return list[0];
        const times = points.value.map((p, i) => toStamp(p.time) || i * 1000);
        const t0 = times[0];
        const t1 = times[times.length - 1];
        const span = Math.max(t1 - t0, 1);
        const target = t0 + span * ratio;
        let i = 0;
        while (i < times.length - 1 && times[i + 1] < target) i += 1;
        const a = list[i];
        const b = list[Math.min(i + 1, list.length - 1)];
        const seg = Math.max(times[Math.min(i + 1, times.length - 1)] - times[i], 1);
        const k = Math.min(Math.max((target - times[i]) / seg, 0), 1);
        return [a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k];
    }
 
    function tick(now: number) {
        if (!playing.value) return;
        const total = duration() / Math.max(speed.value, 1);
        elapsed += now - startedAt;
        startedAt = now;
        const ratio = Math.min(elapsed / total, 1);
        progress.value = Math.round(ratio * 100);
        playPos.value = at(ratio);
        if (ratio >= 1) {
            playing.value = false;
            return;
        }
        raf = requestAnimationFrame(tick);
    }
 
    function play() {
        if (!points.value.length) return;
        if (progress.value >= 100) {
            progress.value = 0;
            elapsed = 0;
        }
        playing.value = true;
        startedAt = performance.now();
        raf = requestAnimationFrame(tick);
    }
 
    function pause() {
        playing.value = false;
        cancelAnimationFrame(raf);
    }
 
    function stop() {
        pause();
        elapsed = 0;
        progress.value = 0;
        playPos.value = points.value.length
            ? [Number(points.value[0].longitude), Number(points.value[0].latitude)]
            : null;
    }
 
    function reset() {
        stop();
        playPos.value = null;
    }
 
    onBeforeUnmount(pause);
 
    return { playing, speed, progress, playPos, path, play, pause, stop, reset };
}