I have a dynamic svg which had a data-id in it's path. I want to find out the id of the path clicked in the svg.
sample svg -
<svg width="208" height="94" viewBox="0 0 208 94" fill="none" xmlns="http://www.w3.org/2000/svg">
<path data-workSpaceId="1" d="M0" fill="#BFBEBE"/>
<path data-workSpaceId="2" d="M15.75" fill="white"/>
<path data-workSpaceId="3" d="M14." fill="#20968F"/>
</svg>
How I'm fetching the svg in react -
<img src={//The svg imported here} onClick={(e) => console.log(e.target.getAttribute('data-workSpaceId')) }}
Any help welcomed :)
It is possible to use fetch to get the SVG and use dangerouslySetInnerHTML to insert the SVG.
For instance, if you have the following test.svg file in the public folder:
<svg width="208" height="94" viewBox="0 0 208 94" fill="none" xmlns="http://www.w3.org/2000/svg">
<path data-id="1" d="M 10 10 H 90 V 90 H 10 L 10 10" fill="#BFBEBE"/>
<path data-id="2" d="M 5 5 H 50 V 50 H 5 L 5 5" fill="red"/>
<path data-id="3" d="M 120 120 H 50 V 50" fill="#20968F"/>
</svg>
you can write the following:
import {useEffect, useState} from "react";
function App() {
const [svgText, setSvgText] = useState(null);
useEffect(() => {
fetch("/test.svg")
.then(res => res.text())
.then(svgText => setSvgText(svgText))
}, []);
return (
<div dangerouslySetInnerHTML={{ __html: svgText}}
onClick={(event) => console.log(event.target.getAttribute('data-id'))}>
</div>
);
}
export default App;
Note that I simplified data-workSpaceId to data-id.
If you want to keep data-workSpaceId, the JavaScript code should be event.target.getAttribute('data-workspaceid').
This answer helped me.
Related
I'm trying to get SVG's in a object file. then use SVG's like this.
import {CustomIcons} from "./Icons"
<CustomIcons name="FrameIcon" />
<CustomIcons name="VectorIcon" />
I just want to import one file and get a custom icon based on the name. I been trying this for hours and I'm lost
CustomIcon.tsx
export const CustomIcons = {
FrameIcon: (
<svg
width="632"
height="330"
viewBox="0 0 632 330"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C695.409 238.987 667.885 262.248 636.416 276.436C612.104 287.397 589.793 302.386 564.742 311.911C523.785 327.491"
fill="#232323"
/>
</svg>
),
VectorIcon: (
<svg
width="632"
height="330"
viewBox="0 0 632 330"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C69"
fill="#232323"
/>
</svg>
),
};
I see a couple of issues here, I'll try to explain as much as I can:
The type of the exported value (e.g. FrameIcon) is supposed to be a function
For instance, your file should export this way:
export {
FrameIcon: () => (
<svg
width="632"
height="330"
viewBox="0 0 632 330"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C695.409 238.987 667.885 262.248 636.416 276.436C612.104 287.397 589.793 302.386 564.742 311.911C523.785 327.491"
fill="#232323"
/>
</svg>
),
VectorIcon: () => (
<svg
width="632"
height="330"
viewBox="0 0 632 330"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C69"
fill="#232323"
/>
</svg>
),
};
The import is supposed to be:
import { FrameIcon } from './Icons'
Extra tip(s)
Create a folder called Icon, create the following files in it:
index.js
FrameIcon.jsx
VectorIcon.jsx
Your index.js should export all the icons created in this folder, for instance:
export * from './FrameIcon';
export * from './VectorIcon';
Your FrameIcon for instance should be:
const FrameIcon = () => (
<svg
width="632"
height="330"
viewBox="0 0 632 330"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C695.409 238.987 667.885 262.248 636.416 276.436C612.104 287.397 589.793 302.386 564.742 311.911C523.785 327.491"
fill="#232323"
/>
</svg>
)
export { FrameIcon };
I hope this helps.
You should define CustomIcons as function component
something like this should work
import React from 'react';
const CustomIcons = (props) => {
const icons = {
FrameIcon :
<svg width="632" height="330" viewBox="0 0 632 330" fill="none" xmlns="http://www.w3.org/2000/svg" >
<path d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C695.409 238.987 667.885 262.248 636.416 276.436C612.104 287.397 589.793 302.386 564.742 311.911C523.785 327.491" fill="#232323"/>
</svg>,
VectorIcon:
<svg width="632" height="330" viewBox="0 0 632 330" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M460.923 -287.339L760.908 192.738C748.744 201.994 736.08 210.636 723.487 219.419C69" fill="#232323"/>
</svg>
}
return <>
{icons[props.name]}
</>
};
export default CustomIcons;
`
I have a simple SVG code, which is a circle progress bar with an image, and I used it to change the value of progress as below, I want to have this SVG with this feature in react native,
things I tried :
react-native-SVG package, but didn't work and I get some weird behavior with it
the code in React js , the props is changed from outside so I can change any props to change progress value , and image inside , as well as the width too ,
function CircularBar({ imgSrc, progress, width }) {
return (
<svg viewBox="0 0 200 210" width={width}>
<defs>
<mask id="m1">
<circle cx="100" cy="105" r="55" fill="white" />
</mask>
<linearGradient
id="lg1"
gradientTransform="rotate(0) skewX(-20) skewY(-40)"
>
<stop offset="0" stop-color="red" />
<stop offset="75%" stop-color="orange" />
</linearGradient>
</defs>
<image mask="url(#m1)" href={imgSrc} width="200" />
<path
pathLength="360"
d="M100 175 a 75 75 0 1 1 1 0"
stroke="LightSlateGray"
fill="none"
stroke-width="30"
stroke-linecap="round"
stroke-dasharray="270 360"
stroke-dashoffset="-45"
/>
<path
pathLength="360"
d="M100 175 a 75 75 0 1 1 1 0"
stroke="url(#lg1)"
fill="none"
stroke-width="15"
stroke-linecap="round"
stroke-dasharray={`${progress * 270} 360`}
stroke-dashoffset="-45"
/>
<g
transform="translate(100 180)"
font-size="16"
font-family="sans-serif"
font-weight="bold"
text-anchor="middle"
>
<text>Overall</text>
<text transform="translate(0 20)">Wellbeing</text>
</g>
</svg>
);
}
export default CircularBar;
this is where I'm stuck, this is the code for react native side
import React from 'react';
import Svg, {
Circle,
Defs,
G,
Image,
LinearGradient,
Mask,
Path,
Stop,
Text,
} from 'react-native-svg';
function CircularBar({imgSrc, progress, width}) {
return (
<Svg width="200" height="400">
<Defs>
<Mask id="m1">
<Circle cx="100" cy="105" r="500" fill="white" />
</Mask>
<LinearGradient
id="lg1"
gradientTransform="rotate(0) skewX(-20) skewY(-40)">
<Stop offset="0" stop-color="red" />
<Stop offset="75%" stop-color="orange" />
</LinearGradient>
</Defs>
<Image mask="url(#m1)" href={imgSrc} width="200" />
<Path
pathLength="360"
d="M100 175 a 75 75 0 1 1 1 0"
stroke="#778899"
fill="none"
stroke-width="30"
stroke-linecap="round"
stroke-dasharray="270 360"
stroke-dashoffset="-45"
/>
<Path
pathLength="360"
d="M100 175 a 75 75 0 1 1 1 0"
stroke="url(#lg1)"
fill="none"
stroke-width="15"
stroke-linecap="round"
stroke-dasharray={`200 360`}
stroke-dashoffset="-45"
/>
<G
transform="translate(100 180)"
font-size="16"
font-family="sans-serif"
font-weight="bold"
text-anchor="middle">
<Text>Overall</Text>
<Text transform="translate(0 20)">Wellbeing</Text>
</G>
</Svg>
);
}
so I hope you could help me with it
I normally use https://react-svgr.com/playground/?native=true to convert my svg to a react native component.
Make sure you check react-native on the left tab.
Example raw svg
<svg width="40" height="40" viewBox="0 0 40 40" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path opacity="0.1" d="M40 20C40 31.0457 31.0457 40 20 40C8.9543 40 0 31.0457 0
20C0 8.9543 8.9543 0 20 0C31.0457 0 40 8.9543 40 20ZM4 20C4 28.8366 11.1634 36
20 36C28.8366 36 36 28.8366 36 20C36 11.1634 28.8366 4 20 4C11.1634 4 4 11.1634
4 20Z" fill="#2B86C3"/>
<path d="M20 40C16.0979 40 12.281 38.8585 9.01954 36.7161C5.75811 34.5738
3.19463 31.5242 1.64491 27.943C0.0951845 24.3618 -0.373076 20.4054 0.297814
16.5614C0.968704 12.7174 2.74944 9.15359 5.42063 6.30906C8.09182 3.46453
11.5368 1.46353 15.3311 0.552601C19.1254 -0.358333 23.1033 -0.139414 26.7748
1.18239C30.4462 2.50419 33.6508 4.87112 35.9937 7.9916C38.3366 11.1121 39.7155
14.8498 39.9605 18.7442L35.9684 18.9954C35.7724 15.8798 34.6693 12.8897 32.795
10.3933C30.9206 7.8969 28.357 6.00335 25.4198 4.94591C22.4827 3.88847 19.3003
3.71333 16.2649 4.44208C13.2294 5.17083 10.4735 6.77162 8.3365 9.04725C6.19955
11.3229 4.77496 14.1739 4.23825 17.2491C3.70154 20.3244 4.07615 23.4894 5.31593
26.3544C6.5557 29.2193 8.60649 31.659 11.2156 33.3729C13.8248 35.0868 16.8783
36 20 36V40Z" fill="#2B86C3"/>
<path d="M12.6744 14.8182H11.1779L8.63743 16.4787V17.9503L11.0735
16.3594H11.1332V25H12.6744V14.8182ZM18.7894 25.1392C20.8377 25.1392 22.2994
23.6925 22.2944 21.7088C22.2994 19.7401 20.9272 18.3082 19.0728
18.3082C18.3171 18.3082 17.6161 18.5966 17.2184 18.9844H17.1587L17.4819
16.1357H21.8022V14.8182H16.2042L15.6325 19.9489L17.0394 20.1577C17.4222
19.8146 18.1083 19.581 18.7248 19.581C19.9329 19.5909 20.8079 20.4957 20.8079
21.7386C20.8079 22.9616 19.9577 23.8466 18.7894 23.8466C17.805 23.8466 17.0245
23.2202 16.945 22.3452H15.4535C15.5131 23.9659 16.9201 25.1392 18.7894
25.1392ZM27.655 25.169C30.0066 25.169 31.3837 23.25 31.3837 19.9141C31.3837
16.603 29.9867 14.679 27.655 14.679C25.3184 14.679 23.9263 16.598 23.9213
19.9141C23.9213 23.245 25.2985 25.1641 27.655 25.169ZM27.655 23.8416C26.2779
23.8416 25.4476 22.4595 25.4476 19.9141C25.4526 17.3786 26.2828 15.9815 27.655
15.9815C29.0222 15.9815 29.8574 17.3786 29.8574 19.9141C29.8574 22.4595
29.0272 23.8416 27.655 23.8416Z" fill="#2B86C3"/>
</svg>
after converting :
import * as React from "react"
import Svg, { Path } from "react-native-svg"
function SvgComponent(props) {
return (
<Svg
width={props.width}
height={40}
fill="none"
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<Path
opacity={0.1}
d="M40 20c0 11.046-8.954 20-20 20S0 31.046 0 20 8.954 0 20 0s20 8.954 20 20zM4 20c0 8.837 7.163 16 16 16s16-7.163 16-16S28.837 4 20 4 4 11.163 4 20z"
fill="#2B86C3"
/>
<Path
d="M20 40a20 20 0 1119.96-21.256l-3.992.251A16 16 0 1020 36v4z"
fill="#2B86C3"
/>
<Path
d="M12.674 14.818h-1.496l-2.54 1.66v1.472l2.435-1.59h.06V25h1.541V14.818zM18.79 25.14c2.049 0 3.51-1.447 3.505-3.43.005-1.969-1.367-3.4-3.221-3.4-.756 0-1.457.288-1.855.675h-.06l.324-2.848h4.32v-1.318h-5.598l-.572 5.13 1.407.21c.383-.343 1.07-.577 1.686-.577 1.208.01 2.083.915 2.083 2.158 0 1.223-.85 2.108-2.019 2.108-.984 0-1.765-.627-1.844-1.502h-1.492c.06 1.62 1.467 2.794 3.336 2.794zm8.866.03c2.352 0 3.729-1.919 3.729-5.255 0-3.311-1.397-5.235-3.729-5.235-2.337 0-3.729 1.919-3.734 5.235 0 3.331 1.378 5.25 3.734 5.255zm0-1.327c-1.377 0-2.207-1.383-2.207-3.928.005-2.535.835-3.933 2.207-3.933 1.367 0 2.202 1.398 2.202 3.933 0 2.545-.83 3.928-2.202 3.928z"
fill="#2B86C3"
/>
</Svg>
)
}
export default SvgComponent
and use in your component like:
import SvgComponent from '...path.../SvgComponent'
.
.
.
<View>
<SvgComponent width={40} />
<Text>your number</Text>
</View>
make your number over progress with absolute position
I specify width since I have value of width in my svg component is {props.width}
Similarly for numbers and use your own logic.
I found the solution you will have to take a raw SVG , and then used it in SVGXML from react_native_svg
let xml = `
<svg viewBox="0 0 200 210" width="100%"
>
<defs>
<linearGradient id="b" gradientTransform="matrix(1.3054 -.8391 -.36397 1 0 0)">
<stop offset="0" stop-color="red"/>
<stop offset="75%" stop-color="orange"/>
</linearGradient>
<mask id="a">
<circle cx="100" cy="100" r="55" fill="#fff"/>
</mask>
</defs>
<image mask="url(#a)" height="250" width="200" href="https://djelfa.cc/man.png" />
<path pathLength="360" d="M100 175a75 75 0 1 1 1 0" stroke="#789" fill="none" stroke-width="30" stroke-linecap="round" stroke-dasharray="270 360" stroke-dashoffset="-45"/>
<path pathLength="360" d="M100 175a75 75 0 1 1 1 0" stroke="url(#b)" fill="none" stroke-width="15" stroke-linecap="round" stroke-dasharray="${
counter ? counter * 270 : 0
} 360" stroke-dashoffset="-45"/>
<g font-size="16" font-family="sans-serif" font-weight="bold" text-anchor="middle">
<text transform="translate(100 180)">Overall</text>
<text transform="translate(100 200)">Wellbeing</text>
</g>
</svg>
`;
return <SvgXml xml={xml} width="100%" height="100%" />;
the only problem here is how to make the image fit inside the mask, I mean how to make the image contained inside it, that's the difference between my react code and this code even they are the same
I am in a situation where I need to export the following SVG as a pdf so I am using the dom-to-image plugin listed in NPM plugin directory.
So the problem is when I try to export the following SVG as an image the color of the SVG gets black.
I tried some options listed in the dom-to-image options but those are for the parent node the SVG I am trying to export is in its child node.
HTML:
<div id="foo"><svg class="cone" viewBox="-25 0 1175 1419" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Canvas" transform="translate(19600 8843)">
<g id="Group 5">
<g id="Group 6">
<g id="Group 5">
<g id="Vector 3.4">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#path10_fill" transform="translate(-19604.5 -7783)" fill="#3596ff">
</use>
</g>
</g>
</g>
</g>
</g>
<defs>
<path id="path10_fill" d="M 0 208L 85 0C 230.833 82 623.9 196.8 1047.5 0L 1138 208C 842 441.5 137 369.5 0 208Z">
</path>
</defs>
</svg>
</div>
JavaScript:
const render = node =>
domtoimage.toPng(node)
.then(dataUrl => {
console.log(performance.now()-pf)
const img = new Image();
img.src = dataUrl;
$('body').append(img);
})
.catch(error =>
console.error('oops, something went wrong!', error)
);
const foo = document.getElementById('foo');
var pf=performance.now();
render(foo);
Fiddle Link
NOTE: Please scroll down in the output part to see the result.
The plugin probably doesn't recognize the <use> tag. You can just change the fill="#3596ff" to the <svg> tag or the <path> tag if you're going to have more than one path.
how can i hide the date on the graph
<div class="highcharts-container" id="highcharts-6">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1330" height="532"><desc>Created with Highstock 1.3.7</desc><defs><clipPath id="highcharts-7"><rect fill="none" x="0" y="0" width="1239" height="290"></rect></clipPath></defs>
<path fill="none" d="M 71 45 L 71 335 180 335 180 45" zIndex="5"></path>
<text x="126" y="29" transform="translate(0,0)" visibility="visible">
<tspan x="126">7/9/15</tspan></text></svg></div>
If there's just one, then this code should work:
document.querySelector("tspan").style.display = "none";
If there are multiple then:
[].forEach.call(document.querySelectorAll("tspan"), function(item) {
item.style.display = "none";
});
Or if you want to use jQuery:
$("tspan").hide();
Can anybody help me with drawing hollow rectangle with svg WITHOUT using any js lib(e.g. Raphael)?
In basic, it should be same idea as "donut shape" but rectangle.
something like this:
Here you go
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 2624 1462" width="1312" height="736" id="svg2">
<path d="M 80,112 l 528,0 0,528 -528,0 z m -64,-64.000003 656,0 0,656.000023 -656,0 z" fill="gold" stroke="black" fill-rule="evenodd" />
</svg>
The path winds in one direction outside and the other inside and the evenodd fill-rule cuts out the middle.
Here's an example:
<svg viewBox="0 0 400 400" shape-rendering="crispEdges">
<path d="M10,10h100v100h-100zM20,20v80h80v-80z" fill="yellow" stroke="black"/>
</svg>