I cannot get SVG use to work internal to itself.
I can get SVG use to work for external references.
This works:
<svg>
<defs>
<symbol id = "foo">
<some svg stuff>
</symbol>
</defs>
</svg>
HTML
<svg><use xlink:href="my.svg#foo"/></svg>
However I want to re-use symbols internally in the SVG. So this doesn't work:
<svg>
<defs>
<symbol id = "foo">
<some svg stuff>
</symbol>
</defs>
<g id = "foo-bar">
<use xlink:href="#foo"/>
</g>
</svg>
HTML
<svg><use xlink:href="#my.svgfoo-bar"/></svg>
use does work internally. Look at this sample: http://jsfiddle.net/0o4jtntL/
As you can see, there is only one caveat: the defined symbol can be croped before being used. So be sure that this symbol is drawn at the center of your screen when you define it.
<svg>
<defs>
<symbol id = "foo">
<circle cx="0" cy="0" r="10" fill="red" stroke="black"/>
</symbol>
</defs>
<g id = "foo-bar">
<use x="20" y="20" xlink:href="#foo"/>
<use x="0" y="50" xlink:href="#foo"/>
<use x="40" y="10" xlink:href="#foo"/>
</g>
</svg>
Related
I'm absolutely beginner with SVG, and I need to put text around this moon...
I tried to make text around a path and could not get the right sizes and match it with the moon.
<svg viewBox="-6 -6 30 40">
<defs>
<mask id="earth">
<rect fill="white" x="-5" y="-5" width="10" height="10"></rect>
<circle fill="black" cx="3.141592654" r="5" />
</mask>
</defs>
<circle r="5" fill="currentColor" mask="url(#earth)" transform="rotate(-25)"/>
</svg>
Draw the moon with a path. (and edit in: https://yqnn.github.io/svg-path-editor/)
The path is drawn counter-clockwise, if you want to draw the innermoon text like your design, it is easier to add a 2nd path drawn clockwise.
Setting pathLength helps in positioning with startoffset
Look up all attributes you don't know in the docs.
<svg viewBox="0 0 80 60">
<rect width="100%" height="100%" fill="skyblue"/>
<path id="Moon" pathLength="10" d="m16 2a12 12 0 1018 13 1 1 0 01-18-13z"/>
<text>
<textPath href="#Moon"
startoffset="1" text-anchor="left" dominant-baseline="hanging"
fill="blue" font-size="3px">Outside moon</textPath>
</text>
<text>
<textPath href="#Moon"
startoffset="6" text-anchor="right" dominant-baseline="hanging"
fill="rebeccapurple" font-size="4">Inside moon</textPath>
</text>
</svg>
In circle tag I have to pass stroke-dashoffset value dynamically
<svg>
<circle class="donut-segment" data-per="100" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#b1c94e" stroke-width="5" stroke-dasharray="100 100" stroke-dashoffset="50"></circle>
</svg>
I am using angular5
How Can I pass it?
Try this:
<svg>
<circle class="donut-segment" data-per="100" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#b1c94e" stroke-width="5" stroke-dasharray="100 100" [stroke-dashoffset]="yourPropNameofComponent"></circle>
</svg>
Got the solution
[attr.stroke-dashoffset]="percentage"
We can pass percentage value dynamically
I have an SVG that I want to set as a background image for an input tag so that I could use an image as a radio button. I've been following many guides but nothing seems to work. I'm suspecting that it has to be the SVG content itself.
Here's the SVG:
<svg width="80px" height="80px" viewBox="473 123 80 80" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="face_sad" opacity="0.8" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" transform="translate(473.000000, 123.000000)">
<path d="M40,0 C17.90861,-1.3527075e-15 2.705415e-15,17.90861 0,40 C-2.705415e-15,62.09139 17.90861,80 40,80 C62.09139,80 80,62.09139 80,40 C80,29.3913404 75.7857264,19.2171839 68.2842712,11.7157288 C60.7828161,4.21427361 50.6086596,6.49593051e-16 40,0 Z M40,77.6 C19.2340934,77.6 2.4,60.7659066 2.4,40 C2.4,19.2340934 19.2340934,2.4 40,2.4 C60.7659066,2.4 77.6,19.2340934 77.6,40 C77.5779535,60.7567678 60.7567678,77.5779535 40,77.6 L40,77.6 Z" id="Shape" fill="#FFFFFF" fill-rule="nonzero"></path>
<path d="M40,49.2 C27.2,49.2 20.16,59.008 19.408,60.128 C19.1388619,60.481731 19.0872921,60.9552533 19.2739762,61.3586265 C19.4606604,61.7619997 19.8550164,62.0291441 20.2988593,62.0529021 C20.7427021,62.0766601 21.1633224,61.8531397 21.392,61.472 C22.08,60.464 28.408,51.6 40,51.6 C51.592,51.6 57.92,60.464 58.608,61.472 C58.9937796,61.9790342 59.7085433,62.096207 60.2360188,61.7388849 C60.7634943,61.3815628 60.9197839,60.6743215 60.592,60.128 C59.84,59.008 52.8,49.2 40,49.2 Z" id="Shape" fill="#FFFFFF" fill-rule="nonzero"></path>
<ellipse id="Oval" fill="#FFFFFF" fill-rule="nonzero" cx="29.616" cy="28.8" rx="4" ry="4"></ellipse>
<ellipse id="Oval" fill="#FFFFFF" fill-rule="nonzero" cx="50.384" cy="28.8" rx="4" ry="4"></ellipse>
<path d="M54.76,35.04 C54.6,34.88 54.44,34.8 54.2,34.8 C53.96,34.8 53.8,34.88 53.72,35.04 C53.64,35.12 52.44,36.56 51.16,38.32 C49.48,40.8 48.6,42.72 48.6,43.92 C48.6,46.88 51.08,49.2 54.2,49.2 C57.24,49.2 59.8,46.8 59.8,43.92 C59.8,42.72 58.92,40.88 57.24,38.4 C56.04,36.56 54.76,35.12 54.76,35.04 Z" id="Shape" fill="#FFFFFF" fill-rule="nonzero"></path>
</g>
</svg>
and my CSS for the input:
input#bad-choice {
background: url(../assets/img/svgs/emoji-bad.svg);
}
I've tried adding thing like no-repeat left top in background or even changing it to background-image, but nothing seems to work. I even tried converting it to data URI but that didn't seem to work neither. Any tips would be greatly appreciated. Thank you.
I see IE display different with firefox, chrome, safari...
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="700" width="1300">
<defs>
<path id="defsPath0" transform="translate(0, 0)" d="M-25,12 L75,12 M-25,32 L75,32 M-25,52 L75,52 M-25,72 L75,72" />
<path id="defsPath1" transform="translate(0, 20)" d="M-25,12 L75,12 M-25,32 L75,32 M-25,52 L75,52 M-25,72 L75,72" />
<path id="defsPath2" transform="translate(0, 40)" d="M-25,12 L75,12 M-25,32 L75,32 M-25,52 L75,52 M-25,72 L75,72" />
<path id="defsPath3" transform="translate(0, 60)" d="M-25,12 L75,12 M-25,32 L75,32 M-25,52 L75,52 M-25,72 L75,72" />
</defs>
<g>
<text x="0" y="0" font-family="MS UI Gothic" font-size="15" style="" fill="rgb(0, 0, 0)" fill-rule="nonzero" transform="matrix(1,0,0,1,200,20)">
<textPath id="textPath_0_0" xlink:href="#defsPath0">1</textPath>
<textPath id="textPath_0_1" xlink:href="#defsPath1">2</textPath>
<textPath id="textPath_0_2" xlink:href="#defsPath2">3</textPath>
<textPath id="textPath_0_3" xlink:href="#defsPath3">4</textPath>
</text>
</g>
with Firefox, chrome:
1
2
3
4
But IE:
1
234
Please help me ! thank you so much !
I use javascript make to svg !
Then you should have shown us your Javascript also.
However I suspect I know what the problem is. I am guessing you are using createElement() to create your SVG elements. You need to use createElementNS():
document.createElementNS(""http://www.w3.org/2000/svg", "textPath");
Trying to render a Hebrew text along a path in SVG causes a bug in Chrome - the glyphs are rendered backwards (left-to-right), making the text unreadable.
<svg height="220" width="190">
<defs>
<path id="MyPath2" d="M0,100 L200,100" />
</defs>
<use xlink:href="#MyPath2" fill="none" stroke="red" />
<text text-anchor="middle" dx="100" dy="0" writing-mode="rl" direction="rtl">
<textPath xlink:href="#MyPath2">
הטקסט הזה ייראה הפוך
</textPath>
</text>
</svg>
Is there a way to get around this? Is this a known bug or is there an attribute I should've used?
JSFIddle: http://jsfiddle.net/j9RnL/
After not finding an elegant solution myself, I just reversed the characters.
function reverse(s, languageCode) {
if (['he', 'ar'].indexOf(languageCode) === -1)
return s;
return s.split("").reverse().join("");
}
I modified your SVG and it seems to render correctly on Chrome 100 and Firefox 100:
<svg viewBox="0 0 190 220" xmlns="http://www.w3.org/2000/svg">
<defs>
<path id="MyPath2" d="M0,100 L200,100"/>
</defs>
<use href="#MyPath2" fill="none" stroke="red"/>
<text text-anchor="middle" dx="100" dy="0" writing-mode="rl" direction="rtl">
<textPath href="#MyPath2">
הטקסט הזה ייראה הפוך
</textPath>
</text>
</svg>