Examples
Styles (11)Add a default markerAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapDisplay a map with a custom styleRender world copiesDisplay a satellite mapChange a map's styleDisplay a mapLayers (32)Display buildings in 3DExtrude polygons for 3D indoor mappingAdd a 3D modelAdjust a layer's opacityAnimate a lineAnimate a series of imagesAnimate a pointChange building color based on zoom levelChange the case of labelsDisplay HTML clusters with custom propertiesCreate and style clustersChange a layer's color with buttonsAdd a custom style layerStyle circles with a data-driven propertyStyle lines with a data-driven propertyUse a fallback imageAdd a pattern to a polygonAdd a new layer below labelsAdd a GeoJSON lineDraw GeoJSON pointsAdd a GeoJSON polygonCreate a heatmap layerAdd hillshadingCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceDisplay and style rich text labelsStyle ocean depth dataShow and hide layersChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelVariable label placementVisualize population densitySources (10)Add a canvas sourceJoin local JSON data with vector tile geometriesAdd an imageAdd live realtime dataUpdate a feature in realtimeAdd a raster tile sourceAdd a third party vector tile sourceAdd a videoAdd a vector tile sourceAdd a WMS sourceUser interaction (21)Customize camera animationsAnimate 3D buildings based on ambient soundsDisable map rotationCreate a draggable MarkerFilter features within map viewFilter symbols by text inputFilter symbols by toggling a listCreate a hover effectDisplay a non-interactive mapChange a map's languageGet coordinates of the mouse pointerMeasure distancesHighlight features containing similar dataSelect features around a clicked pointGet features under the mouse pointerRestrict map panning to an areaCreate a time sliderToggle interactionsHighlight features within a bounding boxFit to the bounds of a LineStringCreate a draggable pointCamera (12)Animate map camera around a pointAnimate a point along a routeCenter the map on a clicked symbolSlowly fly to a locationFit a map to a bounding boxFly to a locationNavigate the map with game-like controlsJump to a series of locationsOffset the vanishing point using paddingPlay map locations as a slideshowFly to a location based on scroll positionSet pitch and bearingControls and overlays (17)Animate a markerChange the default position for attributionAdd custom icons with MarkersDisable scroll zoomView a fullscreen mapLocate the userSwipe between mapsDisplay driving directionsShow drawn polygon areaAdd a geocoderAdd a marker using a place nameDisplay map navigation controlsShow polygon information on clickDisplay a popup on clickDisplay a popup on hoverDisplay a popupAttach a popup to a marker instanceGeocoder (9)Supplement forward geocoding search results from another data sourceAccept coordinates as input to a geocoderUse a custom render function with a geocoderLimit geocoder results to a named regionUse the geocoder without a mapUse a custom camera animation with a geocoderPlace the geocoder input outside the mapLocalize the geocoder to a given languageSet a point after Geocoder resultBrowser support (1)Check if Mapbox GL JS is supportedInternationalization support (2)Use locally generated ideographsAdd support for right-to-left scripts

Offset the vanishing point using padding

Offset the apparent center or vanishing point of the map to reduce distortion when floating elements are displayed over the map.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Offset the vanishing point using padding</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.rounded-rect {
background: white;
border-radius: 10px;
box-shadow: 0 0 50px -25px black;
}
.flex-center {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
}
.flex-center.left {
left: 0px;
}
.flex-center.right {
right: 0px;
}
.sidebar-content {
position: absolute;
width: 95%;
height: 95%;
font-family: Arial, Helvetica, sans-serif;
font-size: 32px;
color: gray;
}
.sidbear-toggle {
position: absolute;
width: 1.3em;
height: 1.3em;
overflow: visible;
display: flex;
justify-content: center;
align-items: center;
}
.sidbear-toggle.left {
right: -1.5em;
}
.sidbear-toggle.right {
left: -1.5em;
}
.sidbear-toggle:hover {
color: #0aa1cf;
cursor: pointer;
}
.sidebar {
transition: transform 1s;
z-index: 1;
width: 300px;
height: 100%;
}
/*
The sidebar styling has them "expanded" by default, we use CSS transforms to push them offscreen
The toggleSidebar() function removes this class from the element in order to expand it.
*/
.left.collapsed {
transform: translateX(-295px);
}
.right.collapsed {
transform: translateX(295px);
}
</style>
<div id="map">
<div id="left" class="sidebar flex-center left collapsed">
<div class="sidebar-content rounded-rect flex-center">
Left Sidebar
<div
class="sidbear-toggle rounded-rect left"
onclick="toggleSidebar('left')"
>
&rarr;
</div>
</div>
</div>
<div id="right" class="sidebar flex-center right collapsed">
<div class="sidebar-content rounded-rect flex-center">
Right Sidebar
<div
class="sidbear-toggle rounded-rect right"
onclick="toggleSidebar('right')"
>
&larr;
</div>
</div>
</div>
</div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = '<your access token here>';
var center = [-77.01866, 38.888];
var map = new mapboxgl.Map({
container: 'map',
zoom: 12.5,
center: center,
pitch: 60,
style: 'mapbox://styles/mapbox/streets-v11'
});
new mapboxgl.Marker().setLngLat(center).addTo(map);
function toggleSidebar(id) {
var elem = document.getElementById(id);
var classes = elem.className.split(' ');
var collapsed = classes.indexOf('collapsed') !== -1;
var padding = {};
if (collapsed) {
// Remove the 'collapsed' class from the class list of the element, this sets it back to the expanded state.
classes.splice(classes.indexOf('collapsed'), 1);
padding[id] = 300; // In px, matches the width of the sidebars set in .sidebar CSS class
map.easeTo({
padding: padding,
duration: 1000 // In ms, CSS transition duration property for the sidebar matches this value
});
} else {
padding[id] = 0;
// Add the 'collapsed' class to the class list of the element
classes.push('collapsed');
map.easeTo({
padding: padding,
duration: 1000
});
}
// Update the class list on the element
elem.className = classes.join(' ');
}
map.on('load', function() {
toggleSidebar('left');
});
</script>
</body>
</html>
Was this page helpful?