Search

Using Images with Leaflet

Advanced

Create floorplans and projections with images and CRS.

Using Images with Leaflet
View live example →

Use an image as a map by passing the url to the underlying Leaflet API.

Nodes are positioned using their coordinates property. The lat and lng values are interpreted as world coordinates in pixels.

You must also set the map.tiles property to null in the options prop.

See also

/* global L */
import React from "react";
import { createRoot } from "react-dom/client";

import { Chart } from "regraph";

import { items } from "./data";

import "@fortawesome/fontawesome-free/css/fontawesome.css";
import "@fortawesome/fontawesome-free/css/solid.css";
import "leaflet/dist/leaflet";
import "leaflet/dist/leaflet.css";
import "@ci/theme/rg/css/layout.css";

const floorPlan = "/img/floorplan.png";

function LeafletImages() {
  const handleChartChange = (change) => {
    const { leaflet } = change;
    if (leaflet) {
      const image = { url: floorPlan, w: 2058, h: 1050 };
      const bounds = [
        [0, 0],
        [image.h, image.w],
      ];
      const backgroundOverlay = L.imageOverlay(image.url, bounds);
      backgroundOverlay.addTo(leaflet);
    }
  };

  const options = {
    map: {
      tiles: null,
      leaflet: {
        crs: L.CRS.Simple,
        minZoom: -1.5,
        maxZoom: 2,
        zoomSnap: 0.5,
      },
    },
    backgroundColor: "white",
    iconFontFamily: "Font Awesome 5 Free",
    imageAlignment: {
      "fas fa-laptop": {
        size: 0.9,
      },
      "fas fa-cash-register": {
        size: 0.9,
      },
      "fas fa-cloud": {
        size: 0.95,
      },
      "fas fa-server": {
        size: 0.9,
      },
    },
    navigation: false,
  };

  return (
    <div className="story">
      <div className="chart-wrapper">
        <Chart
          map
          items={items}
          options={options}
          animation={{ time: 0 }}
          onChange={handleChartChange}
        />
      </div>
    </div>
  );
}

const FontReadyChart = React.lazy(() =>
  document.fonts.load("900 24px 'Font Awesome 5 Free'").then(() => ({
    default: LeafletImages,
  }))
);

export function Demo() {
  return (
    <React.Suspense fallback="">
      <FontReadyChart />
    </React.Suspense>
  );
}

const root = createRoot(document.getElementById("regraph"));
root.render(<Demo />);
import { style } from "@ci/theme/rg/js/storyStyles";

export const items = {
  "f1-t1-f1-t5": {
    width: 4,
    id1: "f1-t1",
    id2: "f1-t5",
    data: {},
    ...style.tertiary1,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  28: {
    width: 6,
    id1: "f1-t5",
    id2: "10",
    data: {},
    color: style.primary1.color,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  29: {
    width: 6,
    id1: "f1-t5",
    id2: "13",
    data: {},
    color: style.primary1.color,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  30: {
    width: 6,
    id1: "f1-t7",
    id2: "13",
    data: {},
    color: style.primary1.color,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  "f1-t7-f1-t2": {
    width: 4,
    id1: "f1-t7",
    id2: "f1-t2",
    data: {},
    ...style.tertiary1,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  "f1-t7-f1-t4": {
    width: 4,
    id1: "f1-t7",
    id2: "f1-t4",
    data: {},
    ...style.tertiary1,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  "f1-t7-f1-t8": {
    width: 4,
    id1: "f1-t7",
    id2: "f1-t8",
    data: {},
    ...style.tertiary1,
    label: {
      fontSize: 40,
      backgroundColor: "rgba(0,0,0,0)",
    },
  },
  26: {
    width: 6,
    id1: "f1-t1",
    id2: "13",
    data: {
      alert: true,
    },
    color: "#d81d46",
    label: {
      fontSize: 40,
      backgroundColor: "#4B4C57",
    },
  },
  "f1-t5": {
    fontIcon: {
      text: "fas fa-laptop",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 790,
      lng: 400,
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.201",
        },
      },
    ],
    size: 0.8,
  },
  "f1-t7": {
    fontIcon: {
      text: "fas fa-laptop",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 740,
      lng: 1180,
    },
    data: {
      location: "New York",
      type: "terminal",
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.150",
        },
      },
    ],
    size: 0.8,
  },
  "f1-t1": {
    fontIcon: {
      text: "fas fa-cash-register",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 140,
      lng: 310,
    },
    data: {
      location: "New York",
      type: "machine",
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.56",
        },
      },
    ],
    size: 0.8,
  },
  "f1-t2": {
    fontIcon: {
      text: "fas fa-cash-register",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 260,
      lng: 1280,
    },
    data: {
      location: "New York",
      type: "machine",
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.28",
        },
      },
    ],
    size: 0.8,
  },
  "f1-t4": {
    fontIcon: {
      text: "fas fa-print",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 560,
      lng: 1590,
    },
    data: {
      location: "New York",
      type: "machine",
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.122",
        },
      },
    ],
    size: 0.8,
  },
  "f1-t8": {
    fontIcon: {
      text: "fas fa-cash-register",
      color: style.primary3.color,
    },
    coordinates: {
      lat: 357,
      lng: 1750,
    },
    data: {
      location: "New York",
      type: "machine",
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 47,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "192.168.1.91",
        },
      },
    ],
    size: 0.8,
  },
  10: {
    coordinates: {
      lat: 1100,
      lng: 800,
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    fontIcon: {
      text: "fas fa-server",
      color: style.primary3.color,
    },
    data: {
      location: "Desktop",
    },
    glyphs: [
      {
        position: 180,
        radius: 50,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "Central Server",
        },
      },
    ],
  },
  13: {
    coordinates: {
      lat: 1100,
      lng: 1200,
    },
    color: "white",
    border: {
      color: style.primary3.color,
    },
    fontIcon: {
      text: "fas fa-cloud",
      color: style.primary3.color,
    },
    glyphs: [
      {
        position: 180,
        radius: 50,
        color: style.primary2.color,
        size: 1.9,
        border: {
          color: "rgba(0,0,0,0)",
        },
        label: {
          text: "Public Internet",
        },
      },
    ],
  },
};
<!doctype html>
<html>
  <body>
    <div id="regraph" style="height: 100vh"></div>
    <script type="module" src="./code.jsx"></script>
  </body>
</html>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.