manubb / Leaflet.PixiOverlay

Bring Pixi.js power to Leaflet maps
MIT License
463 stars 85 forks source link
leaflet leaflet-layer leaflet-map leaflet-overlay leaflet-plugin leaflet-plugins leafletjs pixi pixi-js pixijs webgl

Leaflet.PixiOverlay

npm version

An overlay class for Leaflet, a JS library for interactive maps. Allows drawing overlay using Pixi.js, a JavaScript library for drawing using WebGL that seamlessly falls back to HTML5's canvas if needed. Thanks to Leaflet.D3SvgOverlay for inspiration.

screenshot

Features

Supports leaflet@0.7, 1 and pixi.js@4, 5, 6, 7

Demo

A very basic demo (pixi.js@7) .

Largest US cities (pixi.js@7, 1000 animated markers).

French cities (pixi.js@7, 36700 animated markers).

One million markers

Rotating markers with constant size during zoom

French presidential 2017 election results: first round and second round (36000 polygons).

French legislative 2017 election results: first round and second round (36000 polygons).

(graph-draw is used to display boundaries in the election demos when rendered in WebGL)

Installation

Leaflet.PixiOverlay is available as a npm package:

npm install leaflet-pixi-overlay

or can be included in a page with jsDelivr CDN.

Usage

Include Pixi.js and the PixiOverlay libraries:

<script src="https://github.com/manubb/Leaflet.PixiOverlay/raw/master/pixi.min.js"></script>
<script src="https://github.com/manubb/Leaflet.PixiOverlay/raw/master/L.PixiOverlay.min.js"></script>

Create a map:

const map = L.map(...);

Create an overlay:

const pixiOverlay = L.pixiOverlay((utils) => {
    // your drawing code here
}, new PIXI.Container());

Add it to the map:

pixiOverlay.addTo(map);

Examples

Draw a marker

const markerTexture = await PIXI.Assets.load('img/marker-icon.png');
const markerLatLng = [51.5, -0.09];
const marker = new PIXI.Sprite(markerTexture);
marker.anchor.set(0.5, 1);

const pixiContainer = new PIXI.Container();
pixiContainer.addChild(marker);

let firstDraw = true;
let prevZoom;

const pixiOverlay = L.pixiOverlay((utils) => {
    const zoom = utils.getMap().getZoom();
    const container = utils.getContainer();
    const renderer = utils.getRenderer();
    const project = utils.latLngToLayerPoint;
    const scale = utils.getScale();

    if (firstDraw) {
        const markerCoords = project(markerLatLng);
        marker.x = markerCoords.x;
        marker.y = markerCoords.y;
    }

    if (firstDraw || prevZoom !== zoom) {
        marker.scale.set(1 / scale);
    }

    firstDraw = false;
    prevZoom = zoom;
    renderer.render(container);
}, pixiContainer);

pixiOverlay.addTo(map);

Draw a triangle

const polygonLatLngs = [
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047],
    [51.509, -0.08]
];
let projectedPolygon;
const triangle = new PIXI.Graphics();

const pixiContainer = new PIXI.Container();
pixiContainer.addChild(triangle);

let firstDraw = true;
let prevZoom;

const pixiOverlay = L.pixiOverlay((utils) => {
    const zoom = utils.getMap().getZoom();
    const container = utils.getContainer();
    const renderer = utils.getRenderer();
    const project = utils.latLngToLayerPoint;
    const scale = utils.getScale();

    if (firstDraw) {
        projectedPolygon = polygonLatLngs.map((coords) => project(coords));
    }
    if (firstDraw || prevZoom !== zoom) {
        triangle.clear();
        triangle.lineStyle(3 / scale, 0x3388ff, 1);
        triangle.beginFill(0x3388ff, 0.2);
        projectedPolygon.forEach((coords, index) => {
            if (index == 0) triangle.moveTo(coords.x, coords.y);
            else triangle.lineTo(coords.x, coords.y);
        });
        triangle.endFill();
    }
    firstDraw = false;
    prevZoom = zoom;
    renderer.render(container);
}, pixiContainer);

pixiOverlay.addTo(map);

API

Factory method

L.pixiOverlay(<function> drawCallback, <PIXI.Container> container, <options> options?)

Drawing callback function

drawCallback(utils, eventOrCustomData)

Overlay options object

available fields:

Utils object

available methods:

Instance methods

Changelog

1.9.3 (Feb 22, 2023)

1.8.4 (Feb 19, 2023)

1.8.2 (May 18, 2021)

1.8.1 (May 2, 2019)

1.8.0 (Apr 30, 2019)

1.7.0 (Mar 20, 2019)

1.6.0 (Nov 26, 2018)

1.5.0 (Apr 13, 2018)

1.4.2 (Mar 27, 2018)

1.4.0 (Mar 25, 2018)

1.3.0 (Jan 22, 2018)

1.2.0 (Jan 20, 2018)

1.1.3 (Jan 20, 2018)

1.0.0 (Sep 2, 2017)

License

This code is provided under the MIT License (MIT).

Sponsors

Thanks to Stadia Maps for providing the Stamen Toner Lite map tiles in our project demos.