Skip to content

SparkLayer

Example

Example Source Code
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import { MbMap, MbTiandituLayer } from '@mapbox-react/core'
import {
  MbGradientBuildingLayer,
  MbSparkLayer,
} from '@mapbox-react/effect-layers'

const App = () => {
  const [mapCenter] = useState([116.179267984000035, 39.918961127600085])
  const [zoom, setZoom] = useState(14.5)
  const [pitch, setPitch] = useState(60)
  const mapInst = useRef<any>()

  const [data, setData] = useState<any[]>([])
  const [speed, setSpeed] = useState(5)

  const loadData = () => {
    const sparkData: any[] = []
    for (let i = 0; i < 100; i++) {
      sparkData.push({
        coordinates: [
          116.168 + Math.random() * 0.014,
          39.917 + Math.random() * 0.007,
        ],
        properties: {
          height: 400 + Math.random() * 500,
          color: [255, 100 + Math.random() * 150, 100 + Math.random() * 150],
        },
      })
    }
    setData(sparkData)
  }

  useEffect(() => {
    loadData()
  }, [])

  return (
    <div className="map-wrapper">
      <MbMap ref={mapInst} center={mapCenter} zoom={zoom} pitch={pitch}>
        <MbGradientBuildingLayer
          data="https://mapbox-web.github.io/mapbox-react/showcase/building.geojson"
          getFillColor={[0, 128, 128]}
          dataTransform={(d) => d.features}
          getPolygon={(d) => d.geometry.coordinates[0]}
          getElevation={(d) => d.properties.Floor * 50}
        />
        <MbSparkLayer
          data={data}
          getPosition={(d) => d.coordinates}
          getHeight={(d) => d.properties.height}
          getColor={(d) => d.properties.color}
          trailLength={100}
          speed={speed}
        />
      </MbMap>
    </div>
  )
}

ReactDOM.render(<App />, document.querySelector('#root'))

API

PROPS

NameDescriptionTypeDefault
idLayer IDstring-
getPositionFunction to retrieve the position.AnyFunc-
getHeightFunction to retrieve the height.number / AnyNumberFunc1000
trailLengthLength of the trail (e.g., in number of segments or time units).number100
speedSpeed of movement/animation.number10
getColorFunction to retrieve the color. Note: Typo in original table, should be get-color.string / AnyFunc'black'
dataSource data for the layer.string / IndexAny / AnyArr / Promise<any>''
showWhether the layer is visible.booleantrue
opacityThe opacity of the layer (0-1).number1
pickableWhether the layer responds to mouse events. If false, the component will not emit mouse-related events.booleanfalse
autoHighlightWhen true and pickable is also true, the hovered object will be highlighted.booleanfalse
highlightColorThe color to blend with the original color of the highlighted object.string / number[][255, 255, 128, 1]
highlightedObjectIndexThe index of the currently highlighted element.number-1
onDataLoadCallback function invoked after data loading is complete.AnyFunc-
dataTransformFunction to transform the data before rendering.AnyFunc-
transitionsTransition settings for the layer.IndexAny{}

EVENTS

NameDescriptionParameters

METHODS

NameDescriptionDefinition