NSInvalidArgumentException when trying to use "Symbol" from react-native-svg library - reactjs

I'm trying to use the react-native-svg library particularly with the Symbol element as follows. I used the example code here:
<Svg
height="150"
width="110"
>
<Symbol id="symbol" viewbox="0 0 150 110" width="100" height="50">
<Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
<Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
</Symbol>
<Use
href="#symbol"
x="0"
y="0"
/>
When I run this code, I get the following error in the logs and the app crashes:
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument'
When I don't use the Symbol object everything works fine. Has anyone run into this issue before?

I was actually able to fix this by copying and pasting the example on this page:
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol
You need the width and height elements on the Use tag as follows:
<Svg height="150" width="110">
<Symbol id="symbol" viewbox="0 0 150 110" width="100" height="50">
<Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
<Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
</Symbol>
<Use href="#symbol" x="0" y="0" width="100" height="50"/>
</Svg>
I definitely thought I tried that before, so it may have also been me running the following clear cache and re-compiling commands as well:
rm -rf node_modules && npm install and npm start -- --reset-cache
Either way, problem fixed!

Related

Make placeholder loading responsive

I'm using create-content-loader package for creating placeholder loading but can't make it responsive.
How to make svg fill parent content. I don't want to set width hardcoded 1060px. I tried with viewbox property too.
<ContentLoader
height={40}
width={1060}
speed={2}
primaryColor="#d9d9d9"
secondaryColor="#ecebeb"
>
<rect x="10" y="40" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="60" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="100" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="80" rx="0" ry="0" width="75" height="10" />
<rect x="10" y="120" rx="0" ry="0" width="75" height="10" />
<rect x="110" y="40" rx="0" ry="0" width="370" height="125" />
<rect x="275" y="63" rx="0" ry="0" width="72" height="4" />
<rect x="430" y="5" rx="5" ry="5" width="75" height="20" />
<rect x="110" y="10" rx="0" ry="0" width="200" height="15" />
<rect x="10" y="5" rx="4" ry="4" width="75" height="20" />
<circle cx="493" cy="54" r="2" />
<circle cx="497" cy="47" r="7" />
<circle cx="497" cy="77" r="7" />
<circle cx="497" cy="107" r="7" />
</ContentLoader>```
ContentLoader uses html5 canvas for rendering, so viewbox won't work. What I would do is:
add ref to container element (which holds ContentLoader).
get that element width by using ref
pass that value to the ContentLoader width prop.
Keep in mind that on the first render ref.current will be null or undefined
Update:
Also you can try to extract svg from create-content-loader example and turn it into your component. Just change svg attributes to match rect related svg props. Than set css class on svg to have these properties:
.responsive-svg {
width: 100%;
height: auto;
}
Svg (before you turn it into react component) might look something like this:
<svg
role="img"
width="400"
height="160"
aria-labelledby="loading-aria"
viewBox="0 0 400 160"
preserveAspectRatio="none"
>
<title id="loading-aria">Loading...</title>
<rect
x="0"
y="0"
width="100%"
height="100%"
clip-path="url(#clip-path)"
style='fill: url("#fill");'
></rect>
<defs>
<clipPath id="clip-path">
<rect x="48" y="8" rx="3" ry="3" width="88" height="6" />
<rect x="48" y="26" rx="3" ry="3" width="52" height="6" />
<rect x="0" y="56" rx="3" ry="3" width="410" height="6" />
<rect x="0" y="72" rx="3" ry="3" width="380" height="6" />
<rect x="0" y="88" rx="3" ry="3" width="178" height="6" />
<circle cx="20" cy="20" r="20" />
</clipPath>
<linearGradient id="fill">
<stop
offset="0.599964"
stop-color="#f3f3f3"
stop-opacity="1"
>
<animate
attributeName="offset"
values="-2; -2; 1"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
<stop
offset="1.59996"
stop-color="#ecebeb"
stop-opacity="1"
>
<animate
attributeName="offset"
values="-1; -1; 2"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
<stop
offset="2.59996"
stop-color="#f3f3f3"
stop-opacity="1"
>
<animate
attributeName="offset"
values="0; 0; 3"
keyTimes="0; 0.25; 1"
dur="2s"
repeatCount="indefinite"
></animate>
</stop>
</linearGradient>
</defs>
</svg>

how to place an image in svg path as wall tiles

hi i have svg path where i am placing an image https://i.imgur.com/NFhr6hq.jpg from . when i place its just display in center of path and not filling others area as following my desired image https://i.imgur.com/tdjZpKh.jpg
how i can place image on wall as tiles ?
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image x="0" y="0" xlink:href="stile.jpg" width="100" height="100" />
</pattern>
</defs>
<path d="M 752 180 v 192 h 215 V 145 L 752 180 Z M 916 358 l -130 0.246 v -78.369 L 916 270 V 358 Z"/>
</path>
<script>
jQuery(function($){
$('path').click(function(){
this.style.fill = "url(#img1)";
alert(this.id);
});
});
</script>
when i click on path with jquery am applying image on it
The problem is that your image has white borders. If you want to use that image you need to put the image inside a symbol with a viewBox that is cropping the white border of the image like so:
<svg width="100%" height="100%" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="s" viewBox="2.5 2.5 38 35">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg" width="48" height="45" />
</symbol>
<pattern id="img1" patternUnits="userSpaceOnUse" width="30" height="30">
<use xlink:href="#s" width="30" height="30" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#img1)" />
</svg>
Alternatively you may use a different image with no white borders and in this case you may use the #Alexandr_TT's solution.
UPDATE:
The OP is commenting:
i have updatted question, there i have no rect but path . when i click on path , jquery update path fill area with image
In this case instead of filling the rect you fill the path like so:
<svg viewBox="750 145 230 230" xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="s" viewBox="3.1 2.5 38 35">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg" width="48" height="45" />
</symbol>
<pattern id="img1" patternUnits="userSpaceOnUse" width="38" height="35">
<use xlink:href="#s" width="38" height="35" />
</pattern>
</defs>
<!--<rect width="100%" height="100%" fill="url(#img1)" />-->
<path d="M 752 180 v 192 h 215 V 145 L 752 180 Z M 916 358 l -130 0.246 v -78.369 L 916 270 V 358 Z" fill="url(#img1)"/>
</svg>
Pattern filling any shape
<rect width="100%" height="100%" fill="url(#img1)" />
<svg width="100%" height="100%" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg" width="100px" height="100px" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#img1)" />
</svg>
Update
Tile Fill Path
<svg width="100%" height="100%" viewBox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="25" height="25">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg" width="25px" height="25px" />
</pattern>
</defs>
<path stroke="gray" fill="url(#img1)" transform="translate(-700 -100)" d="M 752 180 v 192 h 215 V 145 L 752 180 Z M 916 358 l -130 0.246 v -78.369 L 916 270 V 358 Z" />
</svg>

React + SVG with "defs" -- styles are ALWAYS ignored

Here's one of my original SVG:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#000000;stroke-width:0.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
</style>
<g>
<path class="st0" d="M34.2,19h-4l-0.6-2.5c-0.2-0.9-1-1.5-1.9-1.5h-3.9c-0.9,0-1.7,0.6-1.9,1.5L21.3,19h-4c-1.7,0-3,1.3-3,3v12
c0,0.6,0.4,1,1,1h21c0.6,0,1-0.4,1-1V22C37.2,20.3,35.9,19,34.2,19z"/>
<rect x="17.2" y="17" class="st0" width="2" height="2"/>
<path class="st0" d="M27.2,19h-3c-0.6,0-1-0.4-1-1l0,0c0-0.6,0.4-1,1-1h3c0.6,0,1,0.4,1,1l0,0C28.2,18.6,27.8,19,27.2,19z"/>
<circle class="st0" cx="25.7" cy="27.5" r="5.5"/>
<circle class="st0" cx="25.7" cy="27.5" r="3.5"/>
<circle class="st0" cx="34.2" cy="22" r="1"/>
<path class="st0" d="M17.2,35V24c0-0.6-0.4-1-1-1h-2"/>
</g>
</svg>
Open it in a browser like Safari, and the image shows correctly:
Now I'm creating an SVG containing two SVGs using defs and symbol - one of them is the one I've shown here above, the other is a similar one:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px">
<defs>
<symbol viewBox="0 0 50 50" id="videographer">
<style type="text/css">
.st0{fill:none;stroke:#010101;stroke-width:0.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
</style>
<g>
<circle class="st0" cx="21" cy="19.5" r="2.5"/>
<circle class="st0" cx="26" cy="19.5" r="2.5"/>
<polyline class="st0" points="33.5,27.8 37.5,31 37.5,23 33.5,26.2 "/>
<rect x="30.5" y="25" class="st0" width="3" height="4"/>
<rect x="14.5" y="24" class="st0" width="2" height="6"/>
<rect x="16.5" y="22" class="st0" width="14" height="10"/>
</g>
</symbol>
<symbol viewBox="0 0 50 50" id="photographer">
<style type="text/css">
.st0{fill:none;stroke:#010101;stroke-width:0.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
</style>
<g>
<path class="st0" d="M34.2,19h-4l-0.6-2.5c-0.2-0.9-1-1.5-1.9-1.5h-3.9c-0.9,0-1.7,0.6-1.9,1.5L21.3,19h-4c-1.7,0-3,1.3-3,3v12
c0,0.6,0.4,1,1,1h21c0.6,0,1-0.4,1-1V22C37.2,20.3,35.9,19,34.2,19z"/>
<rect x="17.2" y="17" class="st0" width="2" height="2"/>
<path class="st0" d="M27.2,19h-3c-0.6,0-1-0.4-1-1l0,0c0-0.6,0.4-1,1-1h3c0.6,0,1,0.4,1,1l0,0C28.2,18.6,27.8,19,27.2,19z"/>
<circle class="st0" cx="25.7" cy="27.5" r="5.5"/>
<circle class="st0" cx="25.7" cy="27.5" r="3.5"/>
<circle class="st0" cx="34.2" cy="22" r="1"/>
<path class="st0" d="M17.2,35V24c0-0.6-0.4-1-1-1h-2"/>
</g>
</symbol>
</defs>
</svg>
My React code to import that SVG and render it...
import VendorIcons from '../../images/vendor_icons.svg';
const VendorIcon = props => {
return (
<svg version="1.1" id="Layer_1" x="0px" y="0px"
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink">
<use xlinkHref={`${VendorIcons}#${props.icon}`} />
</svg>
)
};
Code to render it:
<VendorIcon icon={"videographer"}/>
And this is how it renders, without style:
Why is this happening? why is the style lost? Can this be fixed?
It should work fine if you have change the styles in vendor_icons.svg to presentation attributes rather than CSS in a style tag. It looks like you are using Adobe Illustrator, so you should be able to change the SVG export settings to do this for you. The result will be something like:
<symbol viewBox="0 0 50 50" id="videographer">
<g>
<circle fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" cx="21" cy="19.5" r="2.5"/>
<circle fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" cx="26" cy="19.5" r="2.5"/>
<polyline fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" points="33.5,27.8 37.5,31 37.5,23 33.5,26.2 "/>
<rect x="30.5" y="25" fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" width="3" height="4"/>
<rect x="14.5" y="24" fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" width="2" height="6"/>
<rect x="16.5" y="22" fill="none" stroke="#010101" stroke-linecap="round" stroke-width="0.75" width="14" height="10"/>
</g>
</symbol>
Result using the component as you’ve written it:

Text won`t render using path in react rendered SVG. How can I use this textPath correctly?

Can anyone help me identify why my text is not rendering in the defined path #textPath03 ? The path is rendered as supposed, but when I put it inside a <defs> and try to use it, it doesn`t work.
<svg version="1.1" height="700" width="700" viewBox="-5 -5 110 110" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-reactid="13">
<defs data-reactid="14">
<path id="#textPath03" d="M95,50 A45,45 0 0 0 50,5" data-reactid="15"></path>
</defs>
<line x1="50" y1="50" x2="100" y2="50" stroke="black" data-reactid="16"></line>
<line x1="50" y1="50" x2="50" y2="0" stroke="black" data-reactid="17"></line>
<path d="M90,50 A40,40 0 0 0 50,10" fill="none" stroke="green" data-reactid="18"></path>
<use xlink:href="#textPath03" fill="none" stroke="red" data-reactid="19"></use>
<text x="2" y="40%" font-size="20" data-reactid="20">
<textPath xlink:href="#textPath03" data-reactid="21">Teste</textPath>
</text>
</svg>
This is a REACT rendered SVG.
Thanks!!
The path id contains an extraneous # character. Only the reference to the path should have that. Removing it fixes your example...
<svg version="1.1" height="700" width="700" viewBox="-5 -5 110 110" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-reactid="13">
<defs data-reactid="14">
<path id="textPath03" d="M95,50 A45,45 0 0 0 50,5" data-reactid="15"></path>
</defs>
<line x1="50" y1="50" x2="100" y2="50" stroke="black" data-reactid="16"></line>
<line x1="50" y1="50" x2="50" y2="0" stroke="black" data-reactid="17"></line>
<path d="M90,50 A40,40 0 0 0 50,10" fill="none" stroke="green" data-reactid="18"></path>
<use xlink:href="#textPath03" fill="none" stroke="red" data-reactid="19"></use>
<text x="2" y="40%" font-size="20" data-reactid="20">
<textPath xlink:href="#textPath03" data-reactid="21">Teste</textPath>
</text>
</svg>

<svg>Code</svg> doesn't work on Safari mobile

I've got a problem with an SVG image.
The following code works in all browser but not in safari mobile.
(I use IOS Simulator).
Do you know why? I've not find a solution because all other images I've seen works with the same syntax.
<svg version="1.1" id="logo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="150px" height="125px" viewBox="0 0 150 125" enable-background="new 0 0 150 125" xml:space="preserve">
<path fill="none" stroke="#E26161" d="M1.295,1"/>
<path fill="none" stroke="#E26161" d="M1.295,125"/>
<path fill="none" stroke="#000000" d="M-9.681-16.75"/>
<path fill="none" stroke="#000000" d="M93.751,21.722"/>
<path fill="none" stroke="#000000" d="M151.887,123.541"/>
<path fill="none" stroke="#000000" d="M77.072,62.437"/>
<path fill="none" stroke="#000000" d="M2.328,1.391"/>
<path fill="none" stroke="#E26161" d="M77.072,62.437"/>
<path fill="none" stroke="#E26161" d="M1.295,0.667"/>
<line fill="none" stroke="#E26161" x1="149.043" y1="125" x2="149.043" y2="0"/>
<line fill="none" stroke="#E26161" x1="75.022" y1="62.5" x2="149.043" y2="0"/>
<line fill="none" stroke="#111111" x1="1" y1="124.938" x2="75.022" y2="62.437"/>
<line fill="none" stroke="#E26161" x1="1" y1="0" x2="75.28" y2="62.906"/>
<line fill="none" stroke="#E26161" x1="1" y1="125" x2="1" y2="0"/>
</svg>
It looks like you have an invisible U+200B character at the end of your closing </svg>. Remove it and it should work.

Resources