Leaflet.heat icon indicating copy to clipboard operation
Leaflet.heat copied to clipboard

Exception on certain map state changes

Open Nithanaroy opened this issue 7 years ago • 5 comments

Message: "Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source width is 0."

Stack trace:

"Error: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source width is 0.
    at Error (native)
    at Object.draw (http://0.0.0.0:8126/js/leaflet-heat/leaflet-heat.js:6:1414)
    at e._redraw (http://0.0.0.0:8126/js/leaflet-heat/leaflet-heat.js:11:2802)
    at e._reset (http://0.0.0.0:8126/js/leaflet-heat/leaflet-heat.js:11:1864)
    at e.fireEvent (http://0.0.0.0:8126/node_modules/leaflet/dist/leaflet.js:6:5055)
    at http://0.0.0.0:8126/node_modules/leaflet/dist/leaflet.js:6:513"

Local Variables screen shot 2016-10-25 at 11 06 55 am

screen shot 2016-10-25 at 11 05 28 am

Nithanaroy avatar Oct 25 '16 18:10 Nithanaroy

I get a similar case as when I create my maps they are of 0 size. And then get resized later in the lifecycle. Is it possible to put some guards around the getImageData call when the width or height are 0 otherwise we trigger a throw.

Bengreen avatar Nov 24 '17 10:11 Bengreen

Did you guys figure it out? I'm running into the same issue.

kojilab avatar Jul 18 '18 17:07 kojilab

Same issue here

mirko77 avatar Jun 09 '20 09:06 mirko77

One way to fix that is to add the initialization code in an interaction observer like this:

new IntersectionObserver(() => {
    map.invalidateSize();

    var heat = L.heatLayer(
        [
            [50.5, 30.5, 0.2], // lat, lng, intensity
            [50.6, 30.4, 0.5],
        ],
        { radius: 25 }
    ).addTo(map);
}).observe(document.getElementById(mapId));

Or even better create the whole map inside the IntersectionObserver callback, because this might add many layers one on top of the other.

KonstantinosPetrakis avatar Sep 09 '23 20:09 KonstantinosPetrakis

Same issue

UniCaLaore avatar Sep 26 '23 15:09 UniCaLaore