leafletMapWithD3PathsAndAnimation = {
const container = html`<div style="height: 600px">`;
const center = [40.7128, -74.0060];
const zoom = 10;
const map = L.map(container, {
minZoom: zoom,
keyboard: false,
scrollWheelZoom: false,
}).setView(center, zoom);
L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
subdomains: 'abcd',
ext: 'png'
}).addTo(map);
const overlayPane = d3.select(map.getPanes().overlayPane);
L.svg({ pane: 'overlayPane' }).addTo(map);
const boroughsSvg = overlayPane.select('svg').classed('leaflet-zoom-hide', true);
const projection = d3.geoTransform({
point: function(longitude, latitude) {
const point = map.latLngToLayerPoint(new L.LatLng(latitude, longitude));
this.stream.point(point.x, point.y);
}
});
const path = d3.geoPath().projection(projection);
function boroughColorForFeature(feature) {
return boroughColorsByCode.get(+feature.properties.boro_code);
}
const strokeWidth = 3;
const animationStrokeWidth = 6;
const boroughPaths =
boroughsSvg.selectAll('path').data(boroughsGeoJSON.features)
.enter()
.append('path')
.attr('id', d => `borough-path-${d.properties.boro_code}`)
.style('pointer-events', 'auto')
.style('cursor', 'pointer')
.attr('d', path)
.attr('fill', boroughColorForFeature)
.attr('fill-opacity', 0.2)
.attr('stroke', boroughColorForFeature)
.attr('stroke-width', strokeWidth);
map.on('zoomend', function() {
boroughPaths.attr('d', path);
});
container.animateBorough = function(boroughCode) {
const boroughPath = d3.select(`#borough-path-${boroughCode}`);
boroughPath
.raise()
.transition()
.duration(animationTransitionDurationMs)
.ease(d3.easeBounce)
.attr('stroke-width', animationStrokeWidth)
.end()
.then(() => {
boroughPath.attr('stroke-width', strokeWidth);
})
.catch((err) => {
boroughPath.attr('stroke-width', strokeWidth);
});
};
yield container;
}