hotel-ai-widget
Version:
A customizable hotel chat widget for React and vanilla HTML
40 lines (34 loc) • 1.09 kB
text/typescript
export function easeInOutCubic(t: number): number {
return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
export function flyTo(
map: google.maps.Map,
target: { lat: number; lng: number },
options: { duration?: number; steps?: number; zoom?: number } = {}
): void {
const { steps = 100, zoom = 13 } = options;
const center = map.getCenter();
if (!center) return;
const startLat = center.lat();
const startLng = center.lng();
const targetLat = target.lat;
const targetLng = target.lng;
const startZoom = map.getZoom() ?? 5;
const zoomDiff = zoom - startZoom;
let step = 0;
function animate() {
step++;
const progress = easeInOutCubic(step / steps);
const lat = startLat + (targetLat - startLat) * progress;
const lng = startLng + (targetLng - startLng) * progress;
const currZoom = startZoom + zoomDiff * progress;
map.setCenter({ lat, lng });
map.setZoom(currZoom);
if (step < steps) requestAnimationFrame(animate);
else {
map.setCenter(target);
map.setZoom(zoom);
}
}
animate();
}