154 lines
5.0 KiB
HTML
154 lines
5.0 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Add an animated icon to the map</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 = 'pk.eyJ1IjoicWl1c2hpamllIiwiYSI6ImNsMDNvbWh3YjFmdDgzam1scG92MXF5YXcifQ.Gmzww19TIvd1O14xrxKCRQ';
|
|
const map = new mapboxgl.Map({
|
|
container: 'map',
|
|
center: [0, 0],
|
|
zoom: 2,
|
|
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
|
|
style: 'mapbox://styles/mapbox/streets-v12'
|
|
});
|
|
|
|
const size = 200;
|
|
|
|
// This implements `StyleImageInterface`
|
|
// to draw a pulsing dot icon on the map.
|
|
const pulsingDot = {
|
|
width: size,
|
|
height: size,
|
|
data: new Uint8Array(size * size * 4),
|
|
|
|
// When the layer is added to the map,
|
|
// get the rendering context for the map canvas.
|
|
onAdd: function () {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = this.width;
|
|
canvas.height = this.height;
|
|
this.context = canvas.getContext('2d');
|
|
},
|
|
|
|
// Call once before every frame where the icon will be used.
|
|
render: function () {
|
|
const duration = 1000;
|
|
const t = (performance.now() % duration) / duration;
|
|
|
|
const radius = (size / 2) * 0.3;
|
|
const outerRadius = (size / 2) * 0.7 * t + radius;
|
|
const context = this.context;
|
|
|
|
// Draw the outer circle.
|
|
// 将该区域内的像素设置为透明,擦除
|
|
context.clearRect(0, 0, this.width, this.height);
|
|
// 清空子路径列表,开始一个新的路径
|
|
context.beginPath();
|
|
// // 绘制圆
|
|
// context.arc(
|
|
// this.width / 2,
|
|
// this.height / 2,
|
|
// outerRadius,
|
|
// 0,
|
|
// Math.PI * 2
|
|
// );
|
|
// context.fillStyle = `rgba(255, 200, 200, ${1 - t})`;
|
|
// context.fill();
|
|
|
|
// 绘制外矩形,计算外矩形坐标和宽高
|
|
const outerWidth = this.width * 0.7 * t + radius;
|
|
const outerHeight = this.height * 0.7 * t + radius;
|
|
const startX = (this.width - outerWidth)/2;
|
|
const startY = (this.height - outerHeight)/2;
|
|
context.rect(startX,startY,outerWidth,outerHeight)
|
|
context.fillStyle = `rgba(255, 200, 200, ${1 - t})`;
|
|
context.fill();
|
|
|
|
|
|
|
|
|
|
// Draw the inner circle.
|
|
context.beginPath();
|
|
// context.arc(
|
|
// this.width / 2,
|
|
// this.height / 2,
|
|
// radius,
|
|
// 0,
|
|
// Math.PI * 2
|
|
// );
|
|
|
|
// 绘制内部矩形
|
|
const innerWidth = this.width * 0.3;
|
|
const innerHeight = this.height * 0.3;
|
|
const startX1 = (this.width - innerWidth)/2;
|
|
const startY1 = (this.height - innerHeight)/2;
|
|
context.rect(startX1,startY1,innerWidth,innerHeight)
|
|
|
|
context.fillStyle = 'rgba(255, 100, 100, 0.6)';
|
|
context.strokeStyle = 'white';
|
|
context.lineWidth = 2 + 4 * (1 - t);
|
|
context.fill();
|
|
context.stroke();
|
|
|
|
context.clearRect(innerWidth+20, innerHeight+20, innerWidth-20, innerHeight - 20);
|
|
|
|
// Update this image's data with data from the canvas.
|
|
this.data = context.getImageData(
|
|
0,
|
|
0,
|
|
this.width,
|
|
this.height
|
|
).data;
|
|
|
|
// Continuously repaint the map, resulting
|
|
// in the smooth animation of the dot.
|
|
map.triggerRepaint();
|
|
|
|
// Return `true` to let the map know that the image was updated.
|
|
return true;
|
|
}
|
|
};
|
|
|
|
map.on('load', () => {
|
|
map.addImage('pulsing-dot', pulsingDot, { pixelRatio: 2 });
|
|
|
|
map.addSource('dot-point', {
|
|
'type': 'geojson',
|
|
'data': {
|
|
'type': 'FeatureCollection',
|
|
'features': [
|
|
{
|
|
'type': 'Feature',
|
|
'geometry': {
|
|
'type': 'Point',
|
|
'coordinates': [0, 0] // icon position [lng, lat]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
});
|
|
map.addLayer({
|
|
'id': 'layer-with-pulsing-dot',
|
|
'type': 'symbol',
|
|
'source': 'dot-point',
|
|
'layout': {
|
|
'icon-image': 'pulsing-dot'
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html> |