Multilayer map (canvas + svg)Edit

A canvas layer and an SVG layer. This technique is useful if you have a background layer that would require a large number of DOM nodes. Rendering that layer with canvas will speed up the page. The shapes you actually care about are in SVG to make styling and mouse interaction easier.

  import { LayerCake, Svg, Canvas, Html } from 'layercake';
  import { feature } from 'topojson-client';
  import { geoAlbersUsa } from 'd3-geo';
  import { scaleQuantize } from 'd3-scale';

  // For a map example with a tooltip, check out

  import MapSvg from './components/Map.svg.svelte';
  import MapCanvas from './components/Map.canvas.svelte';
  import MapLabels from './components/MapLabels.html.svelte';
  // import MapLabels from './components/MapLabels.html.svelte';

  // This example loads json data as json using @rollup/plugin-json
  import usStates from './data/us-states.topojson.json';
  import stateData from './data/us-states-data.json';
  import stateLabels from './data/us-states-labels.json';

  const colorKey = 'myValue';
  const labelCoordinatesKey = 'center';
  const labelNameKey = 'abbr';

  const geojson = feature(usStates, usStates.objects.collection);
  const projection = geoAlbersUsa();

  /* --------------------------------------------
   * Create lookups to more easily join our data
  const joinKey = 'name';
  const dataLookup = new Map();
  const labelLookup = new Map();

  stateData.forEach(d => {
    dataLookup.set(d[joinKey], d);

  stateLabels.forEach(d => {
    labelLookup.set(d[joinKey], d);

  geojson.features.forEach(d => {
    // This will overwrite any existing keys on
    // so watch out for any name collision
    Object.assign(, dataLookup.get([joinKey]));
    Object.assign(, labelLookup.get([joinKey]));

  // Create a flat array of objects that LayerCake can use to measure
  // extents for the color scale
  const flatData = =>;
  const colors = ['#ffdecc', '#ffc09c', '#ffa06b', '#ff7a33'];

    The wrapper div needs to have an explicit width and height in CSS.
    It can also be a flexbox child or CSS grid element.
    The point being it needs dimensions since the <LayerCake> element will
    expand to fill it.
  .chart-container {
    width: 100%;
    height: 100%;

<div class="chart-container">
      getLabelCoordinates: feature =>[labelCoordinatesKey],
      getLabelName: feature =>[labelNameKey]

        features={geojson.features.slice(40, 50)}

        features={[...geojson.features.slice(0, 50), geojson.features[0]]}