admin管理员组文章数量:1313347
I need to get the svg element from a react-icon ponent to render the image using a different Javascript library.
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. Now paperjs allows to importSVG images, so I try the following:
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]
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.
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...
I need to get the svg element from a react-icon ponent to render the image using a different Javascript library.
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. Now paperjs allows to importSVG images, so I try the following:
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]
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.
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...
3 Answers
Reset to default 3This worked better for me:
ReactDOMServer.renderToString(<MdMemory/>)
I just searched for the icon on React Icons and inspected that icon through console, and then I copied and pasted it:
Browser
Copying
Paste
Got it! to achieve this you will need to
- Extract the path element like so
MdMemory().props.children[0].props.d
- Create an
svg
string likeconst svg = "..."
const addDevice = () => {
const svg = `<svg xmlns="http://www.w3/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();
}
本文标签: javascriptGet SVG from reacticons componentsStack Overflow
版权声明:本文标题:javascript - Get SVG from react-icons components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741946119a2406424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论