dav*_*ami 3 reactjs mapbox-gl deck.gl
我试图在满足特定条件时在 iconLayer 中渲染自定义 svg,在此示例中,https://deck.gl/gallery/icon-layer我的 getIcon 选项是
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
Run Code Online (Sandbox Code Playgroud)
我认为 url 应该是我想要渲染的图像的路径但是,地图上没有显示任何内容。
完整的组件是
import { IconLayer } from "@deck.gl/layers";
import icons from "../assets/images/monsoon_icons.png";
const COLORS = {
...colors
};
const ICON_MAPPING = {
circle: { x: 0, y: 0, width: 70, height: 70, mask: true },
rectangle: { x: 70, y: 0, width: 70, height: 70, mask: true },
triangle: { x: 140, y: 0, width: 70, height: 70, mask: true },
star: { x: 210, y: 0, width: 70, height: 70, mask: true },
};
export const RenderLayers = (props) => {
let { data } = props;
if(props.isSaved) {
return [
new IconLayer({
...other icon props
// iconMapping: ICON_MAPPING,
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
...other icon props,
}),
new IconLayer({
...other icon props
// iconMapping: ICON_MAPPING,
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
...other icon props,
}),
];
};
if (!data?.length) {
console.log("NO DATA TO MAP");
return;
};
return [
new IconLayer({
...normal icon layer props
})
];
};
Run Code Online (Sandbox Code Playgroud)
Adr*_*lid 12
您需要将 svg 转换为数据 URL。
按照官方的 dial.gl 示例:
import yourSvg from 'whatever/path';
function svgToDataURL(svg) {
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}
new IconLayer({
getIcon: () => ({
url: svgToDataURL(yourSvg),
width: 24,
height: 24
}),
})
Run Code Online (Sandbox Code Playgroud)
另外,您可以在此处查看完整的工作示例 。
干杯!
| 归档时间: |
|
| 查看次数: |
4258 次 |
| 最近记录: |