Atlaslog
Atlaslog / Leaflet / API reference / Canvas
Leaflet · API reference

Canvas

Quick answer

L.canvas creates a renderer that draws every vector layer into one canvas element covering the viewport, so thousands of simple points produce a constant number of DOM nodes. Use it through the renderer option on paths or the map's preferCanvas flag when symbol counts are high and styling stays simple.

Last verified 2026-11-03 · Leaflet 1.9.4 · Cite this page
L.canvas( options ) → Canvas
Key points
Example
var canvas = L.canvas({ padding: 0.2, tolerance: 4 });

/* 3000 个随机点,全部画进同一张画布 */
var pts = [];
for (var i = 0; i < 3000; i++) {
  pts.push([
    39.85 + Math.random() * 0.10,
    116.32 + Math.random() * 0.16
  ]);
}

var group = L.layerGroup(pts.map(function (p) {
  return L.circleMarker(p, {
    renderer: canvas,
    radius: 3,
    weight: 1,
    color: '#ffffff',
    fillColor: '#1f5fd0',
    fillOpacity: 0.8
  });
})).addTo(map);

/* 点击命中仍按要素判断,e.layer 是对应的 circleMarker */
group.on('click', function (e) {
  console.log(L.Util.stamp(e.layer));
});
Notes
Runnable examplesChinese originalAll API references
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). Canvas. https://www.jingweizhi.com/en/leaflet/api/canvas.html (accessed 2026-11-03).