added timeline with satellite tracking

This commit is contained in:
Vasilisk9812 2025-12-11 23:30:32 +09:00
parent 60fe848b0c
commit eb7066ac6b
3 changed files with 491 additions and 28 deletions

View file

@ -0,0 +1,393 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
import type { Prediction } from "$lib/types";
let { prediction }: { prediction: Prediction | null } = $props();
const dispatch = createEventDispatcher<{
timeUpdate: { index: number; lat: number; lng: number; alt: number; datetime: Date };
}>();
let isPlaying = $state(false);
let currentIndex = $state(0);
let playbackSpeed = $state(1);
let animationFrame: number | null = null;
let lastUpdateTime = 0;
$effect(() => {
if (prediction && currentIndex >= flightPathLength) {
currentIndex = 0;
}
});
const flightPathLength = $derived(prediction?.flight_path?.length || 0);
const progress = $derived(flightPathLength > 0 ? (currentIndex / flightPathLength) * 100 : 0);
const currentPosition = $derived.by(() => {
if (!prediction || !prediction.flight_path[currentIndex]) return null;
const point = prediction.flight_path[currentIndex];
let lat: number, lng: number, alt: number;
if (Array.isArray(point)) {
lat = point[0];
lng = point[1];
alt = point[2] || 0;
} else {
lat = point.lat;
lng = point.lng;
alt = point.alt || 0;
}
const totalTime = prediction.flight_time;
const timeProgress = (currentIndex / flightPathLength) * totalTime;
const launchTime = prediction.launch.datetime instanceof Date
? prediction.launch.datetime.getTime()
: new Date(prediction.launch.datetime).getTime();
const datetime = new Date(launchTime + timeProgress * 1000);
return { lat, lng, alt, datetime };
});
const timeElapsed = $derived.by(() => {
if (!prediction || !currentPosition) return "00:00:00";
const launchTime = prediction.launch.datetime instanceof Date
? prediction.launch.datetime.getTime()
: new Date(prediction.launch.datetime).getTime();
const totalSeconds = Math.floor(
(currentPosition.datetime.getTime() - launchTime) / 1000,
);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
});
function animate(timestamp: number) {
if (!isPlaying) return;
if (!lastUpdateTime) lastUpdateTime = timestamp;
const deltaTime = timestamp - lastUpdateTime;
if (deltaTime >= 50 / playbackSpeed) {
if (currentIndex < flightPathLength - 1) {
currentIndex++;
if (currentPosition) {
dispatch("timeUpdate", { ...currentPosition, index: currentIndex });
}
} else {
stop();
}
lastUpdateTime = timestamp;
}
animationFrame = requestAnimationFrame(animate);
}
function play() {
if (!prediction) return;
if (currentIndex >= flightPathLength - 1) {
currentIndex = 0;
}
isPlaying = true;
lastUpdateTime = 0;
animationFrame = requestAnimationFrame(animate);
}
function pause() {
isPlaying = false;
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
}
function stop() {
pause();
currentIndex = 0;
if (currentPosition) {
dispatch("timeUpdate", { ...currentPosition, index: currentIndex });
}
}
function handleSliderChange(event: Event) {
const target = event.target as HTMLInputElement;
currentIndex = parseInt(target.value);
if (currentPosition) {
dispatch("timeUpdate", { ...currentPosition, index: currentIndex });
}
}
function changeSpeed() {
const speeds = [1, 2, 5, 10];
const currentSpeedIndex = speeds.indexOf(playbackSpeed);
playbackSpeed = speeds[(currentSpeedIndex + 1) % speeds.length];
}
$effect(() => {
return () => {
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
}
};
});
</script>
<div class="timeline-container">
<div class="timeline-info">
<div class="info-section">
<span class="label">Time:</span>
<span class="value">{timeElapsed}</span>
</div>
{#if currentPosition}
<div class="info-section">
<span class="label">Altitude:</span>
<span class="value">{Math.round(currentPosition.alt)} m</span>
</div>
<div class="info-section">
<span class="label">Position:</span>
<span class="value"
>{currentPosition.lat.toFixed(4)}, {currentPosition.lng.toFixed(4)}</span
>
</div>
{/if}
</div>
<div class="timeline-controls">
<div class="control-buttons">
<button
class="control-btn"
on:click={stop}
disabled={!prediction || currentIndex === 0}
title="Start"
>
<i class="bi bi-skip-start-fill"></i>
</button>
{#if !isPlaying}
<button class="control-btn play-btn" on:click={play} disabled={!prediction} title="Play">
<i class="bi bi-play-fill"></i>
</button>
{:else}
<button class="control-btn" on:click={pause} title="Pause">
<i class="bi bi-pause-fill"></i>
</button>
{/if}
<button
class="control-btn speed-btn"
on:click={changeSpeed}
disabled={!prediction}
title="Speed"
>
{playbackSpeed}x
</button>
</div>
<div class="progress-container">
<input
type="range"
min="0"
max={flightPathLength - 1}
value={currentIndex}
on:input={handleSliderChange}
disabled={!prediction}
class="timeline-slider"
/>
<div class="progress-bar" style="width: {progress}%"></div>
</div>
</div>
</div>
<style>
.timeline-container {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: rgba(255, 255, 255, 0.95);
border-radius: 12px;
padding: 16px 20px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
min-width: 600px;
max-width: 800px;
z-index: 1000;
backdrop-filter: blur(10px);
}
.timeline-info {
display: flex;
justify-content: space-around;
margin-bottom: 12px;
gap: 16px;
}
.info-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
}
.label {
font-size: 11px;
color: #666;
text-transform: uppercase;
font-weight: 600;
letter-spacing: 0.5px;
}
.value {
font-size: 14px;
color: #000;
font-weight: 600;
font-family: monospace;
}
.timeline-controls {
display: flex;
align-items: center;
gap: 12px;
}
.control-buttons {
display: flex;
gap: 8px;
align-items: center;
}
.control-btn {
width: 40px;
height: 40px;
border: none;
border-radius: 50%;
background: #007bff;
color: white;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
transition: all 0.2s ease;
}
.control-btn:hover:not(:disabled) {
background: #0056b3;
transform: scale(1.1);
}
.control-btn:disabled {
background: #ccc;
cursor: not-allowed;
opacity: 0.5;
}
.play-btn {
width: 48px;
height: 48px;
font-size: 22px;
background: #28a745;
}
.play-btn:hover:not(:disabled) {
background: #218838;
}
.speed-btn {
border-radius: 8px;
width: 50px;
font-size: 12px;
font-weight: 700;
}
.progress-container {
flex: 1;
position: relative;
height: 40px;
display: flex;
align-items: center;
}
.timeline-slider {
width: 100%;
height: 6px;
border-radius: 3px;
outline: none;
background: #e0e0e0;
-webkit-appearance: none;
appearance: none;
cursor: pointer;
position: relative;
z-index: 2;
}
.timeline-slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: #007bff;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.timeline-slider::-moz-range-thumb {
width: 18px;
height: 18px;
border-radius: 50%;
background: #007bff;
cursor: pointer;
border: none;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.timeline-slider:disabled {
cursor: not-allowed;
opacity: 0.5;
}
.progress-bar {
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
height: 6px;
background: linear-gradient(90deg, #007bff 0%, #0056b3 100%);
border-radius: 3px;
transition: width 0.05s linear;
pointer-events: none;
z-index: 1;
}
@media (max-width: 768px) {
.timeline-container {
min-width: 90%;
max-width: 90%;
padding: 12px 16px;
}
.timeline-info {
flex-direction: column;
gap: 8px;
margin-bottom: 8px;
}
.info-section {
flex-direction: row;
justify-content: space-between;
}
.control-btn {
width: 36px;
height: 36px;
font-size: 16px;
}
.play-btn {
width: 42px;
height: 42px;
font-size: 20px;
}
}
</style>