@@ -26,10 +26,12 @@ function gradientPath2SVG (geojson) {
26
26
const bbox = turfbbox ( geojson )
27
27
// Grow the bbox a bit because we use it to position the svgOverlay. If it matches exaclty
28
28
// 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
33
35
34
36
// Create an id suffix to make linearGradient ids unique
35
37
const idSuffix = `${ bbox . join ( '_' ) } _${ gradient . length } `
@@ -43,7 +45,9 @@ function gradientPath2SVG (geojson) {
43
45
// The linear gradient to apply on the current segment
44
46
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>` )
45
47
// 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"/>` )
47
51
}
48
52
49
53
// Compute the svg extent we must map in the svgOverlay (= the projected geojson bbox)
@@ -60,10 +64,14 @@ function gradientPath2SVG (geojson) {
60
64
// svg html content, round linecap + stroke-width
61
65
svgElement . innerHTML = `<g stroke-linecap="round" stroke-width="${ geojson . properties . weight } "><defs>${ defs . join ( '' ) } </defs>${ lines . join ( '' ) } </g>`
62
66
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 } )
64
71
const opacity = _ . get ( geojson . properties , 'opacity' )
65
72
if ( opacity !== undefined )
66
73
layer . setOpacity ( opacity )
74
+ layer . getCenter = ( ) => L . latLng ( ( bbox [ 1 ] + bbox [ 3 ] ) / 2 , ( bbox [ 0 ] + bbox [ 2 ] ) / 2 )
67
75
return layer
68
76
}
69
77
0 commit comments