Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 9,724 Bytes
ad35d80 b8952af ad35d80 099d0c4 b8952af ad35d80 b8952af 70f6fef b8952af 099d0c4 b8952af ad35d80 b8952af ad35d80 b8952af ad35d80 b8952af ad35d80 b8952af ad35d80 b8952af ad35d80 099d0c4 b8952af ad35d80 b8952af 099d0c4 b8952af ad35d80 b8952af | 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | import mapboxgl from 'mapbox-gl';
import { COLORS } from '../config.js';
export class FlightPathLayer {
constructor(map) {
this.map = map;
this.id = 'flight-path';
this.sourceId = 'flight-path-source';
this.animationFrame = null;
this.fullPath = null;
this.layersAdded = false;
this.jetMarker = null;
// Trail system with multiple independent fading trails
this.trails = [];
this.trailCounter = 0;
}
createArcPath(start, end, numPoints = 50) {
const points = [];
const [x1, y1] = start;
const [x2, y2] = end;
const dx = x2 - x1;
const dy = y2 - y1;
const distance = Math.sqrt(dx * dx + dy * dy);
const arcHeight = distance * 0.15;
for (let i = 0; i <= numPoints; i++) {
const t = i / numPoints;
const x = x1 + t * dx;
const y = y1 + t * dy;
const arcOffset = arcHeight * Math.sin(t * Math.PI);
points.push([x, y + arcOffset]);
}
return points;
}
createJetMarker() {
const el = document.createElement('div');
el.className = 'jet-marker';
// SVG jet pointing UP (north) - rotation 0 = north
el.innerHTML = `
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<!-- Fuselage pointing up -->
<path d="M16 2 L18 8 L18 22 L20 26 L20 28 L16 26 L12 28 L12 26 L14 22 L14 8 Z" fill="${COLORS.white}" stroke="${COLORS.blue}" stroke-width="0.5"/>
<!-- Left wing (blue) -->
<path d="M14 12 L4 18 L4 20 L14 17 Z" fill="${COLORS.blue}"/>
<!-- Right wing (red) -->
<path d="M18 12 L28 18 L28 20 L18 17 Z" fill="${COLORS.red}"/>
<!-- Left tail -->
<path d="M14 22 L10 26 L10 27 L14 24 Z" fill="${COLORS.blue}"/>
<!-- Right tail -->
<path d="M18 22 L22 26 L22 27 L18 24 Z" fill="${COLORS.red}"/>
<!-- Cockpit -->
<ellipse cx="16" cy="7" rx="1.5" ry="2.5" fill="${COLORS.blue}" opacity="0.6"/>
</svg>
`;
this.jetMarker = new mapboxgl.Marker({
element: el,
anchor: 'center',
rotationAlignment: 'map'
});
return this.jetMarker;
}
updateJetPosition(coords, nextCoords) {
if (!this.jetMarker) return;
this.jetMarker.setLngLat(coords);
// Calculate bearing/rotation based on direction of travel
// Only update if next coords are different (avoid resetting at destination)
if (nextCoords && (nextCoords[0] !== coords[0] || nextCoords[1] !== coords[1])) {
const bearing = this.calculateBearing(coords, nextCoords);
this.jetMarker.setRotation(bearing);
}
}
calculateBearing(start, end) {
const startLat = start[1] * Math.PI / 180;
const startLng = start[0] * Math.PI / 180;
const endLat = end[1] * Math.PI / 180;
const endLng = end[0] * Math.PI / 180;
const dLng = endLng - startLng;
const x = Math.sin(dLng) * Math.cos(endLat);
const y = Math.cos(startLat) * Math.sin(endLat) - Math.sin(startLat) * Math.cos(endLat) * Math.cos(dLng);
const bearing = Math.atan2(x, y) * 180 / Math.PI;
return (bearing + 360) % 360;
}
showJet(initialCoords) {
if (!this.jetMarker) {
this.createJetMarker();
}
// Set position before adding to map
if (initialCoords) {
this.jetMarker.setLngLat(initialCoords);
}
this.jetMarker.addTo(this.map);
}
hideJet() {
if (this.jetMarker) {
this.jetMarker.remove();
}
}
ensureLayers() {
if (this.layersAdded) return;
// Add source with empty data
if (!this.map.getSource(this.sourceId)) {
this.map.addSource(this.sourceId, {
type: 'geojson',
data: { type: 'Feature', geometry: { type: 'LineString', coordinates: [] } }
});
}
// Glow layer
if (!this.map.getLayer(`${this.id}-glow`)) {
this.map.addLayer({
id: `${this.id}-glow`,
type: 'line',
source: this.sourceId,
paint: {
'line-color': COLORS.blue,
'line-width': 6,
'line-opacity': 0.4
}
});
}
// Main white line
if (!this.map.getLayer(`${this.id}-main`)) {
this.map.addLayer({
id: `${this.id}-main`,
type: 'line',
source: this.sourceId,
paint: {
'line-color': COLORS.white,
'line-width': 2,
'line-opacity': 0.9
}
});
}
// Blue accent
if (!this.map.getLayer(`${this.id}-blue`)) {
this.map.addLayer({
id: `${this.id}-blue`,
type: 'line',
source: this.sourceId,
paint: {
'line-color': COLORS.blue,
'line-width': 2,
'line-opacity': 0.7,
'line-offset': 3
}
});
}
// Red accent
if (!this.map.getLayer(`${this.id}-red`)) {
this.map.addLayer({
id: `${this.id}-red`,
type: 'line',
source: this.sourceId,
paint: {
'line-color': COLORS.red,
'line-width': 2,
'line-opacity': 0.7,
'line-offset': -3
}
});
}
this.layersAdded = true;
}
updatePath(coordinates) {
const source = this.map.getSource(this.sourceId);
if (source) {
source.setData({
type: 'Feature',
geometry: { type: 'LineString', coordinates }
});
}
}
createTrail(coords) {
const trailId = `trail-${this.trailCounter++}`;
const sourceId = `${this.id}-${trailId}-source`;
const layerId = `${this.id}-${trailId}`;
// Add source
this.map.addSource(sourceId, {
type: 'geojson',
data: {
type: 'Feature',
geometry: { type: 'LineString', coordinates: coords }
}
});
// Add layer
this.map.addLayer({
id: layerId,
type: 'line',
source: sourceId,
paint: {
'line-color': COLORS.white,
'line-width': 1.5,
'line-opacity': 0.5
}
}, `${this.id}-glow`); // Insert below the main path layers
// Start fade animation
const fadeDuration = 5000;
const startTime = performance.now();
const trail = { sourceId, layerId, fadeFrame: null };
const fade = (currentTime) => {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / fadeDuration, 1);
const opacity = 0.5 * (1 - progress);
if (this.map.getLayer(layerId)) {
this.map.setPaintProperty(layerId, 'line-opacity', opacity);
}
if (progress < 1) {
trail.fadeFrame = requestAnimationFrame(fade);
} else {
// Clean up when fully faded
this.removeTrail(trail);
}
};
trail.fadeFrame = requestAnimationFrame(fade);
this.trails.push(trail);
}
removeTrail(trail) {
if (trail.fadeFrame) {
cancelAnimationFrame(trail.fadeFrame);
}
if (this.map.getLayer(trail.layerId)) {
this.map.removeLayer(trail.layerId);
}
if (this.map.getSource(trail.sourceId)) {
this.map.removeSource(trail.sourceId);
}
this.trails = this.trails.filter(t => t !== trail);
}
clearAllTrails() {
this.trails.forEach(trail => this.removeTrail(trail));
this.trails = [];
}
// Show full static path
showPath(from, to) {
this.stopAnimation();
this.hideJet();
this.ensureLayers();
this.fullPath = this.createArcPath(from, to);
this.updatePath(this.fullPath);
}
// Animate path being traced progressively with jet
animatePath(from, to, duration, onComplete) {
this.stopAnimation();
this.ensureLayers();
this.fullPath = this.createArcPath(from, to);
const totalPoints = this.fullPath.length;
// Show jet at start position with initial rotation
const initialCoords = this.fullPath[0];
const nextCoords = this.fullPath[1];
this.showJet(initialCoords);
// Set initial bearing
const initialBearing = this.calculateBearing(initialCoords, nextCoords);
this.jetMarker.setRotation(initialBearing);
const startTime = performance.now();
const animate = (currentTime) => {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
// Linear progression for consistent speed
const pointIndex = Math.max(0, Math.min(Math.floor(progress * totalPoints), totalPoints - 1));
// Update path
this.updatePath(this.fullPath.slice(0, pointIndex + 1));
// Update jet position and rotation
const currentCoords = this.fullPath[pointIndex];
const lookAheadIndex = Math.min(pointIndex + 1, totalPoints - 1);
const lookAheadCoords = this.fullPath[lookAheadIndex];
this.updateJetPosition(currentCoords, lookAheadCoords);
if (progress < 1) {
this.animationFrame = requestAnimationFrame(animate);
} else {
this.animationFrame = null;
this.hideJet();
// Create fading trail from completed path
this.createTrail(this.fullPath);
this.updatePath([]);
if (onComplete) onComplete();
}
};
this.animationFrame = requestAnimationFrame(animate);
}
stopAnimation() {
if (this.animationFrame) {
cancelAnimationFrame(this.animationFrame);
this.animationFrame = null;
}
this.hideJet();
}
hide() {
this.stopAnimation();
this.clearAllTrails();
this.updatePath([]);
this.fullPath = null;
}
remove() {
this.hide();
this.hideJet();
['glow', 'main', 'blue', 'red'].forEach(suffix => {
if (this.map.getLayer(`${this.id}-${suffix}`)) {
this.map.removeLayer(`${this.id}-${suffix}`);
}
});
if (this.map.getSource(this.sourceId)) {
this.map.removeSource(this.sourceId);
}
this.layersAdded = false;
}
}
|