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.
Symptom
- Old circle markers remain on screen while dragging and fade only after the pointer is released.
- At zoom 16 lines look several pixels thick with dark patches where they cross.
- The effect appears above roughly 3000 features and is invisible with a few hundred.
Checklist
- Switch preferCanvas off and confirm the ghosting disappears under SVG.
- Drop to 500 features to see whether it is a redraw-time problem.
- Print layer.getRenderer().options for padding, tolerance and updateWhenZooming.
- 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
- Setting updateWhenZooming: true with 8000 features on the default padding 0.1 makes every zoom frame slower without removing the ghost.
- Raising the circle radius to hide the artifact increases overdraw and worsens the smear.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). preferCanvas leaves ghost trails while panning. https://www.jingweizhi.com/en/cookbook/leaflet-canvas-ghost.html (accessed 2026-11-03).