Skip to content

Commit 2d57d22

Browse files
wip: updated gradient path as svg (#1107)
1 parent 207008b commit 2d57d22

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

map/client/mixins/map/mixin.geojson-layers.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ function gradientPath2SVG (geojson) {
2626
const bbox = turfbbox(geojson)
2727
// Grow the bbox a bit because we use it to position the svgOverlay. If it matches exaclty
2828
// it'll crop the svg lines when using a big stroke-width, they'll exceed the geojson bbox ...
29-
bbox[0] -= 1
30-
bbox[1] -= 1
31-
bbox[2] += 1
32-
bbox[3] += 1
29+
const width = bbox[2] - bbox[0]
30+
const height = bbox[3] - bbox[1]
31+
bbox[0] -= width * 0.1
32+
bbox[1] -= height * 0.1
33+
bbox[2] += width * 0.1
34+
bbox[3] += height * 0.1
3335

3436
// Create an id suffix to make linearGradient ids unique
3537
const idSuffix = `${bbox.join('_')}_${gradient.length}`
@@ -43,7 +45,9 @@ function gradientPath2SVG (geojson) {
4345
// The linear gradient to apply on the current segment
4446
defs.push(`<linearGradient gradientUnits="userSpaceOnUse" x1="${p0[0]}" y1="${p0[1]}" x2="${p1[0]}" y2="${p1[1]}" id="gradient${i}_${idSuffix}"><stop offset="0" stop-color="${gradient[i]}"/><stop offset="1" stop-color="${gradient[i+1]}"/></linearGradient>`)
4547
// The associated line segment, vector-effect="non-sclaing-stroke" make it so stroke-width is a final pixel value, independent of zoom level
46-
lines.push(`<line x1="${p0[0]}" y1="${p0[1]}" x2="${p1[0]}" y2="${p1[1]}" stroke="url(#gradient${i}_${idSuffix})" vector-effect="non-scaling-stroke"/>`)
48+
// lines.push(`<line x1="${p0[0]}" y1="${p0[1]}" x2="${p1[0]}" y2="${p1[1]}" stroke="url(#gradient${i}_${idSuffix})" vector-effect="non-scaling-stroke"/>`)
49+
// Use paths instead because leaflet CSS defines 'pointer' cursor only on 'path' elements, not lines
50+
lines.push(`<path d="M ${p0[0]} ${p0[1]} L ${p1[0]} ${p1[1]}" stroke="url(#gradient${i}_${idSuffix})" vector-effect="non-scaling-stroke" class="leaflet-interactive"/>`)
4751
}
4852

4953
// Compute the svg extent we must map in the svgOverlay (= the projected geojson bbox)
@@ -60,10 +64,14 @@ function gradientPath2SVG (geojson) {
6064
// svg html content, round linecap + stroke-width
6165
svgElement.innerHTML = `<g stroke-linecap="round" stroke-width="${geojson.properties.weight}"><defs>${defs.join('')}</defs>${lines.join('')}</g>`
6266
var svgElementBounds = [ [ bbox[1], bbox[0] ], [ bbox[3], bbox[2] ] ];
63-
const layer = L.svgOverlay(svgElement, svgElementBounds)
67+
// TODO: interactive should be set to false, and layer.addInteractiveTarget(layer._image) should be called, but it needs the map
68+
// we don't want 'leaflet-interactive' class on the svg html element because it requests 'pointer' cursor
69+
// but we want the map to know about interactive targets
70+
const layer = L.svgOverlay(svgElement, svgElementBounds, { interactive: true })
6471
const opacity = _.get(geojson.properties, 'opacity')
6572
if (opacity !== undefined)
6673
layer.setOpacity(opacity)
74+
layer.getCenter = () => L.latLng((bbox[1]+bbox[3])/2,(bbox[0]+bbox[2])/2)
6775
return layer
6876
}
6977

0 commit comments

Comments
 (0)