Lottie图片加载器(ImageLottieLoader)
ImageLottieLoader 提供预加载Lottie动画图标
示例
示例源码
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import lottie from 'lottie_canvas'
import {
MbImageLottieLoader,
MbMap,
MbSymbolLayer,
MbTiandituLayer,
} from '@mapbox-react/core'
const App = () => {
const [mapCenter] = useState([115.124368, 29.216049])
const [zoom, setZoom] = useState(12)
const symbolDataSource = [
{
coordinates: [115.125368, 29.216049],
properties: {
icon: 'lottieIcon',
},
},
{
coordinates: [115.126368, 29.226049],
properties: {
icon: 'lottieIcon',
},
},
]
const lottieImages = [
{
name: 'lottieIcon',
size: [140, 100],
onAdd(context: any) {
lottie.loadAnimation({
renderer: 'canvas',
loop: true,
autoplay: true,
animationData: window.animData, // JSON文件路径
rendererSettings: {
context, // the canvas context, only support "2d" context
// preserveAspectRatio: 'xMinYMin slice', // Supports the same options as the svg element's preserveAspectRatio property
clearCanvas: true,
},
})
},
},
]
return (
<div className="map-wrapper">
<MbMap center={mapCenter} zoom={zoom}>
<MbImageLottieLoader images={lottieImages} />
<MbTiandituLayer types={['vec']} />
<MbSymbolLayer
data={symbolDataSource}
iconImageField="icon"
iconAllowOverlap
textAllowOverlap
/>
</MbMap>
</div>
)
}
ReactDOM.render(<App />, document.querySelector('#root'))
API
PROPS
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
images | 图片属性数组 数组中每个对象需包含必需的name属性以及图片相关信息 | Array<{ name: string; size: number[], onAdd: (context: CanvasRenderingContext2D) => void }> | - |
- 需要在onAdd里面添加lottie动画到context(CanvasRenderingContext2D)
- size为必填属性
EVENTS
名称 | 描述 | 参数 |
---|---|---|
onCreated | 地图初始化完成事件 | - |
METHODS
名称 | 描述 | 定义 |
---|