Atlaslog
Atlaslog / Field Notes / Leaflet containers & layers
Field note · Leaflet containers & layers

preferCanvas leaves ghost trails while panning

Quick answer

The Canvas renderer pre-draws only the viewport plus 10% padding, so panning beyond that padding leaves old circles on the canvas until the redraw catches up. Zooming also scales the canvas, thickening 1 px lines into dark blobs.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet
Symptom
Checklist
  1. Switch preferCanvas off and confirm the ghosting disappears under SVG.
  2. Drop to 500 features to see whether it is a redraw-time problem.
  3. Print layer.getRenderer().options for padding, tolerance and updateWhenZooming.
  4. Check whether one renderer serves both points and large polygons.
Fix

Create L.canvas({ padding: 0.5, tolerance: 5, updateWhenZooming: true }) and pass it as the layer renderer, or split point and polygon layers onto separate canvas renderers so each clears independently.

Code
console.log(layer.getRenderer().options);
// { padding: 0.1, tolerance: 0, updateWhenZooming: false }
var canvasRenderer = L.canvas({
  padding: 0.5,        // 预绘制 50% 视口边距
  tolerance: 5,        // 点击命中的像素容差
  updateWhenZooming: true
});

var layer = L.geoJSON(data, {
  renderer: canvasRenderer,
  pointToLayer: function (feature, latlng) {
    return L.circleMarker(latlng, {
      radius: 4,
      weight: 1,
      color: '#1c7ed6',
      fillOpacity: 0.6
    });
  }
}).addTo(map);
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). preferCanvas leaves ghost trails while panning. https://www.jingweizhi.com/en/cookbook/leaflet-canvas-ghost.html (accessed 2026-11-03).