-
Notifications
You must be signed in to change notification settings - Fork 0
/
free-camera-point.html
159 lines (137 loc) · 5.41 KB
/
free-camera-point.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Animate the camera around a point with 3D terrain</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
mapboxgl.accessToken = '...';
const map = new mapboxgl.Map({
container: 'map',
zoom: 11.53,
center: [138.7189, 35.1691],
pitch: 76,
bearing: -177.2,
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/satellite-v9',
interactive: false
});
map.on('style.load', () => {
map.setFog({}); // Set the default atmosphere style
// Add terrain
map.addSource('mapbox-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
'tileSize': 512,
'maxzoom': 14
});
map.setTerrain({ 'source': 'mapbox-dem', 'exaggeration': 1.5 });
});
function updateCameraPosition(position, altitude, target) {
const camera = map.getFreeCameraOptions();
camera.position = mapboxgl.MercatorCoordinate.fromLngLat(
position,
altitude
);
camera.lookAtPoint(target);
map.setFreeCameraOptions(camera);
}
let animationIndex = 0;
let animationTime = 0.0;
// wait for the terrain to load before starting animations
map.once('idle', () => {
// linearly interpolate between two altitudes/positions based on time
const lerp = (a, b, t) => {
if (Array.isArray(a) && Array.isArray(b)) {
const result = [];
for (let i = 0; i < Math.min(a.length, b.length); i++)
result[i] = a[i] * (1.0 - t) + b[i] * t;
return result;
} else {
return a * (1.0 - t) + b * t;
}
};
const animations = [
{
duration: 20000.0,
animate: (phase) => {
const start = [138.73375, 35.41914];
const end = [138.72649, 35.33974];
const alt = [7000.0, 6000.0];
// interpolate camera position while keeping focus on a target lat/lng
const position = lerp(start, end, phase);
const altitude = lerp(alt[0], alt[1], phase);
const target = [138.73036, 35.36197];
updateCameraPosition(position, altitude, target);
}
},
{
duration: 15000.0,
animate: (phase) => {
const start = [138.72649, 35.33974];
const end = [138.72623, 35.31977];
const alt = [6000.0, 6000.0];
const target1 = [138.73036, 35.36197];
const target2 = [138.74831, 35.34784];
// interpolate both the camera position and target
const position = lerp(start, end, phase);
const altitude = lerp(alt[0], alt[1], phase);
const target = lerp(target1, target2, phase);
updateCameraPosition(position, altitude, target);
}
},
{
duration: 15000.0,
animate: (phase) => {
// create easing function for the animation
const easeInOutQuad = (t) => {
return t < 0.5
? 2.0 * t * t
: (4.0 - 2.0 * t) * t - 1.0;
};
const start = [138.72623, 35.31977];
const end = [138.73375, 35.41914];
const alt = [6000.0, 7000.0];
const target1 = [138.74831, 35.34784];
const target2 = [138.73036, 35.36197];
// interpolate both the camera position and target
const position = lerp(start, end, easeInOutQuad(phase));
const altitude = lerp(alt[0], alt[1], phase);
const target = lerp(target1, target2, phase);
updateCameraPosition(position, altitude, target);
}
}
];
let lastTime = 0.0;
function frame(time) {
animationIndex %= animations.length;
const current = animations[animationIndex];
if (animationTime < current.duration) {
// Normalize the duration between 0 and 1 to interpolate the animation
const phase = animationTime / current.duration;
current.animate(phase);
}
// Elasped time since last frame, in milliseconds
const elapsed = time - lastTime;
animationTime += elapsed;
lastTime = time;
if (animationTime > current.duration) {
animationIndex++;
animationTime = 0.0;
}
window.requestAnimationFrame(frame);
}
window.requestAnimationFrame(frame);
});
</script>
</body>
</html>