Search code examples
svgpolygongeojsonpixi.js

Polygon rendering with pixijs


I'm trying to display more than 6000 Polygons on a mobile device. Currently, I'm doing this with SVG paths in Android WebView using the d3.js library. It works but I have to deal with performance issues, my map becomes very laggy when I drag my map or zoom.

My Idea now is to try the same with pixijs. My data comes originally from ESRI Shapefiles. I'm convert these Shapefiles to GeoJSON and then to SVG. My array of vertices looks like this, which I'm trying to pass to the drawPolygon function

0: 994.9867684400124 1: 22.308409862458518 2: 1042.2789743912592 3: 61.07148769269074

But when I try to render these polygon nothing being displayed. This is my code:

        var renderer = PIXI.autoDetectRenderer(1800, 1800, { backgroundColor: 0x000000, antialias: true });
        document.body.appendChild(renderer.view);

        var stage = new PIXI.Container();
        var graphics = new PIXI.Graphics();

        var totalShapes = feat.features.length; 

        for (var i = 1; i <= totalShapes -1; i++) {

            var shape = feat.features[i];
            var geometry = shape.geometry.bbox;

            graphics.beginFill(0xe74c3c);
            graphics.drawPolygon([ geometry]);
            graphics.endFill();
            stage.addChild(graphics);
            renderer.render(stage);

         }

Can someone help me or could suggest me a different way?


Solution

  • I have not seen that way of initializing a pixie project.

    Usually you add the application to the html document like:

      var app = new PIXI.Application({
        width: window.innerWidth,
        height: window.innerHeight,
        backgroundColor: 0x2c3e50
    });
    document.body.appendChild(app.view);
    

    If you do this you can add your draw calls to the setup of the application:

    app.loader.load(startup);
    
    function startup()
    {
        var g = new PIXI.Graphics();
        g.beginFill(0x5d0015);
        g.drawPolygon(
            10, 10, 120, 100, 120, 200, 70, 200
        );
        g.endFill();
    
        app.stage.addChild(g);
    }
    

    This will render the polygon once.