Skip to main content
For the complete documentation index, see llms.txt.

Modify the camera's field of view

This example uses setVerticalFieldOfView() to adjust the map camera's vertical field of view, producing telephoto or wide-angle effects. Horizontal field of view is not independently controllable — it's derived from the vertical field of view and the Map's aspect ratio, and is available as a read-only value via getHorizontalFieldOfView().

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modify the camera's field of view</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.30.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.30.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.controls {
position: absolute;
top: 20px;
left: 20px;
background: rgba(255, 255, 255, 0.95);
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
sans-serif;
z-index: 1;
min-width: 250px;
}

.controls h3 {
margin: 0 0 15px 0;
font-size: 16px;
color: #333;
}
.controls span {
min-width: 44px;
display: inline-block;
}

.slider-group label {
display: block;
margin-bottom: 5px;
font-size: 14px;
color: #555;
}

.slider-group input[type='range'] {
width: 100%;
cursor: pointer;
}

.value {
font-weight: bold;
color: #0066cc;
}
</style>
<div class="controls">
<div class="slider-group">
<input type="range" id="fov-slider" min="1" max="60" step="0.1" value="36.87">
<label><strong>Vertical Field of View:</strong>
<span id="v-fov-value" class="value">36.87</span></label>
<label><strong>Horizontal Field of View</strong> (derived):
<span id="h-fov-value" class="value">64.01</span></label>
</div>
</div>
<!-- Map -->
<div id="map"></div>
<script>
const map = new mapboxgl.Map({
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
container: 'map',
config: {
basemap: {
showPlaceLabels: false,
showPointOfInterestLabels: false,
showRoadLabels: false
}
},
center: [-71.06288, 42.36834],
zoom: 16.85,
bearing: 141.6,
pitch: 75.67
});

// FOV slider
document.getElementById('fov-slider').addEventListener('input', (e) => {
const fov = parseFloat(e.target.value);
document.getElementById('v-fov-value').textContent = fov.toFixed(2);
document.getElementById('h-fov-value').textContent = map
.getHorizontalFieldOfView()
.toFixed(2);
map.setVerticalFieldOfView(fov);
});
</script>

</body>
</html>
This code snippet will not work as expected until you replace YOUR_MAPBOX_ACCESS_TOKEN with an access token from your Mapbox account.
Was this example helpful?