reactjs - 如何在 Deck.gl 的 IconLayer 中渲染自定义 svg
问题描述
当满足特定条件时,我正在尝试在 iconLayer 中呈现自定义 svg,在此示例中,https: //deck.gl/gallery/icon-layer我的 getIcon 选项是
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
我认为 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
})
];
};
解决方案
您需要将 svg 转换为数据 URL。
按照官方的 deck.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
}),
})
此外,您可以在此处查看完整的工作示例 。
干杯!
推荐阅读
- javascript - 想根据图片大小限制js可拖动
- r - R数据长范围重组
- python - 将项目添加到下拉列表而不刷新
- c# - 如何在 .Net 应用程序中使用 Ignite Cache TextQuery
- google-apps-script - Google App Scripts 错误“此文档太大,无法离线编辑。请在线继续编辑。”
- javascript - 我的 javascript 字符串是这样的:“接受 === 接受 && 50 > 100” 现在我必须返回关于这个字符串是否有效的布尔值
- regex - 正则表达式在等号之前不匹配表达式?
- c++ - 关于使用 freopen() 和 cin, cout 从文件中读取大输入的问题
- bootstrap-4 - 为什么我的卡列表组中的行没有完全消耗,您认为这是什么问题?
- php - 数据表:从另一个表(yajra,laravel)中排序关系时如何保留id