p5.js + mappa.js | Unable to place map in parent div

I am working with the mappa.js library found in the p5 libraries section of the p5 site. I am looking to place the map within a container div, but cannot for the life of me figure out how to do this.

This issue is demonstrated in this codepen. In the pen you can see the map and canvas overlay are inserted below the footer.

Does anyone have experience with this library and placement of a map within specific page elements? Thank you in advance for your time and insight.

Unable to place map in parent div https://codepen.io/moriartp/pen/yvOjXx


  • Answer ✓

    So, after hours of unsuccessful attempts, I found a solution shortly after posting this.

    At the end of the p5 draw function, I added a little bit of jQuery to reassign the parent div.


    for more general reference $("#div_to_move").detach().appendTo("#new_parent_container");

  • You really shouldn't be doing any DOM manipulation in draw if you can avoid it: very bad for performance! I don't have time to look into this now, but will try and look into it tomorrow...

  • @moriartp - I found the issue in the Mappa source:

    const container = L.DomUtil.create('div', 'leaflet-layer');

    Mappa appears to be a very loose wrapper around a number of map providers. The above code creates a div container for the Leaflet map and moves the p5 canvas into the same div: i.e. your canvas.parent("container"); makes no difference as it's overridden...

    Unless Mappa itself provides an option to set a parent then there's no solution other than DOM manipulation :/

    You could raise an issue with the author; though TBH if you know you want to use Leaflet I'd suggest you just use it directly and figure out how to overlay the p5 canvas - it's not so difficult and it'll give you direct access to the Leaflet API: which allows you to set the parent ID on instantiation ;)

  • edited February 2018


    L.overlay = L.Layer.extend({
      onAdd: () => {
        const overlayPane = overlay.getPane();
        const container = L.DomUtil.create('div', 'leaflet-layer');
      drawLayer: () => {},
    const overlay = new L.overlay();


    createMap() {
      this.map = L.map(this.id, {
        center: [
          this.options.lat, this.options.lng,
        zoom: this.options.zoom,
        inertia: false,


    myMap = mappa.tileMap(options);
    myMap.overlay(canvas, () => container.appendChild( myMap.map.getContainer() ));
Sign In or Register to comment.