简体   繁体   English

从 react-icons 组件中获取 SVG

[英]Get SVG from react-icons components

I need to get the svg element from a react-icon component to render the image using a different Javascript library.我需要从 react-icon 组件获取 svg 元素以使用不同的 Javascript 库呈现图像。

I'm using paperjs as the drawing engine for this demo I'm working on, for the UI I use react-icons and react-bootstrap .我正在使用paperjs作为我正在处理的这个演示的绘图引擎,对于 UI,我使用react-iconsreact-bootstrap Now paperjs allows to importSVG images, so I try the following:现在paperjs允许导入SVG图像,所以我尝试以下操作:

import { MdMemory } from "react-icons/md";

const addDevice = () => {
  const svgGroup = Paper.project.importSVG(<MdMemory />);
  svgGroup.position = pointA.clone();
}

But when I do so I get the following error: Error: Unsupported SVG source: [object Object]但是当我这样做时,我收到以下错误: Error: Unsupported SVG source: [object Object]

When I inspect the other place where I use <MdMemory /> I get an svg element, so I wonder if what I'm trying to achieve is possible as I don't want to load duplicated assets.当我检查另一个使用<MdMemory />地方时,我得到了一个svg元素,所以我想知道我想要实现的目标是否可行,因为我不想加载重复的资产。

UPDATE更新

After spending a bit more time, I came up with the following:花了更多时间后,我想出了以下内容:

console.log(MdMemory().props.children[0].props.d);
const svgGroup = Paper.project.importSVG(`<svg><path d=${MdMemory().props.children[0].props.d}></path></svg>`);

Where MdMemory().props.children[0].props.d is the actual svg path, but I'm still unable to render anything...其中MdMemory().props.children[0].props.d是实际的 svg 路径,但我仍然无法渲染任何东西......

Got it!知道了! to achieve this you will need to要实现这一点,您需要

  1. Extract the path element like so MdMemory().props.children[0].props.d像这样提取路径元素MdMemory().props.children[0].props.d
  2. Create an svg string like const svg = "..."创建一个像const svg = "..."这样的svg字符串
const addDevice = () => {
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <path d="${MdMemory().props.children[0].props.d}"></path></svg>`;
  const svgGroup = Paper.project.importSVG(svg);
  svgGroup.fillColor = 'black';
  svgGroup.position = pointA.clone();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM