SVG textpath repeat text - svg

Is it possible to easily define a short text which is repeated in an defined offset along a curve in an svg grafik? Did i have to use patterns for that purpose or is there a attribute for the textpath element (I couldn't find something at google)

If you want to have specific predefined offsets along the path, you will need to have multiple copies of the <textPath> element with different startOffset values.
<svg width="12cm" height="3.6cm" viewBox="0 0 1000 300" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path id="MyPath"
d="M 100 200
C 200 100 300 0 400 100
C 500 200 600 300 700 200
C 800 100 900 100 900 100" />
</defs>
<use xlink:href="#MyPath" fill="none" stroke="red" />
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath" startOffset="0">Text</textPath>
</text>
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath" startOffset="200">Text</textPath>
</text>
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath" startOffset="400">Text</textPath>
</text>
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath" startOffset="600">Text</textPath>
</text>
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath" startOffset="800">Text</textPath>
</text>
</svg>
Or if instead you wanted to have specific gaps between the words, rather than placing them at specific offsets, you can use dx on <tspan> to set the word spacings.
<svg width="12cm" height="3.6cm" viewBox="0 0 1000 300" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path id="MyPath"
d="M 100 200
C 200 100 300 0 400 100
C 500 200 600 300 700 200
C 800 100 900 100 900 100" />
</defs>
<use xlink:href="#MyPath" fill="none" stroke="red" />
<text font-size="42.5" fill="blue" >
<textPath xlink:href="#MyPath">Text
<tspan dx="200">Text</tspan>
<tspan dx="200">Text</tspan>
<tspan dx="200">Text</tspan>
<tspan dx="200">Text</tspan>
</textPath>
</text>
</svg>

Related

How to rotate svg component around its axis

I want to rotate the pointer around its axis of the following SVG component.
How could I achieve that. I have tried it with following method but it doesn't rotate around it.
<g id="Group 1" transform = "translate(100, 100) rotate(45 0 0)">
<path id="Point" fill-rule="evenodd" clip-rule="evenodd" d="M302.248 291.371L230.862 209.521L212.309 230.492L302.248 291.371Z" fill="#72A1E7"/>
</g>
It is easier to:
rotate something if it can be centered around 0,0.
calculate the angle of the needle if you know the angle at the starting point.
Therefor the you could draw the needle like this, with the middle of the short line at 0,0 (yes, it is off-canvas) and then pointing at 6 o'clock:
svg {
background-color: silver;
}
<svg width="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<g id="Group 1">
<path id="Point" fill-rule="evenodd" clip-rule="evenodd"
d="M -10 0 L 10 0 L 0 75 Z" fill="#72A1E7"/>
</g>
</svg>
Now the needle can be moved to the center (or where ever) of the image:
svg {
background-color: silver;
}
<svg width="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<g id="Group 1" transform="translate(100 100)">
<path id="Point" fill-rule="evenodd" clip-rule="evenodd"
d="M -10 0 L 10 0 L 0 75 Z" fill="#72A1E7"/>
</g>
</svg>
The rotation can be done in different ways, but here I rotate the needle/path <path> itself to the imagined zero point of the meter and then use the group element <g> to give the meter a "value". So, here the calculation is that the meter takes up 300 deg, the zero value is at 30 deg (from 6 o'clock) and the max value is then at 330 deg. Here the value is 1/3 = 100 deg.
svg {
background-color: silver;
}
<svg width="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<circle transform="rotate(120 100 100)" cx="100" cy="100" r="85"
stroke="blue" stroke-width="5" stroke-dasharray="300 360" pathLength="360"
fill="none" />
<g id="Group 1" transform="translate(100 100) rotate(100)">
<path id="Point" transform="rotate(30)" fill-rule="evenodd"
clip-rule="evenodd" d="M -10 0 L 10 0 L 0 75 Z" fill="#72A1E7"/>
</g>
</svg>

Why is <textpath> not showing up?

Expected behavior:
I'm trying to write ABC in polygon with the help of but nothing is showing up. Is this the right way to do?
<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 84 96">
<g id="ABC" transform="translate(-8.000000, -2.000000)">
<g transform="translate(11.000000, 5.000000)">
<text x="10" y="100" style={{ fill: '#64FFDA' }}>
<textPath href="#Shape" fill="#64FFDA">
ABC
</textPath>
</text>
<polygon
id="Shape"
stroke="#64FFDA"
strokeWidth="5"
strokeLinecap="round"
strokeLinejoin="round"
fillOpacity="transparent"
points="39 0 0 22 0 67 39 90 78 68 78 23"></polygon>
</g>
</g>
</svg>
If you need to place letters inside the polygon, you need to place the <text> command below the <polygon> command
Pay attention to the syntax of SVG command writing. Instead of strokeWidth ="5" you need stroke-width ="5"
<svg xmlns="http://www.w3.org/2000/svg" role="img" width="20%" height="20%" viewBox="0 0 84 96">
<g id="ABC" transform="translate(-8.000000, -2.000000)">
<g transform="translate(11.000000, 5.000000)">
<polygon
id="Shape"
stroke="#64FFDA"
stroke-width="4"
fill="#151515"
points="39 0 0 22 0 67 39 90 78 68 78 23"></polygon>
</g>
<text x="50" y="55" font-size="22px" font-weight="500" font-family="sans-serif" fill="#64FFDA" text-anchor="middle" >ABC</text>
</g>
</svg>

How to draw a circle with stripped border in SVG?

How to draw a circle with stripped border in SVG. Something like attached image ?
<svg width="25%" height="25%" viewBox="0 0 120 120">
<circle class="default" cx="25" cy="25" r="24" fill="none" stroke="black" stroke-width="1"/>
<circle class="default" cx="25" cy="25" r="16" fill="none" stroke="black" stroke-width="1"/>
</svg>
It is necessary to divide the circle with the use stroke-dasharray of into 8 parts
The circumference at a radius of 20 is 2 * 3.14 * 20 = 125.6
Divide into 8 parts to get 4 sectors of the circle, each of which
will have a line and a space - 125.6 / 8 = 15.7
stroke-dasharray="15.7, 15.7", where the first value is the length of the dash the second value is a space
<svg width="30%" height="30%" viewBox="0 0 120 120">
<!-- Outer circle -->
<circle class="default" cx="25" cy="25" r="24" fill="none" stroke="black" stroke-width="1"/>
<!-- The circle is divided into 4 sections -->
<circle class="default" cx="25" cy="25" r="20" fill="none" stroke="black"
stroke-width="8" stroke-dasharray="15.7, 15.7 "/>
<!--Inner circle -->
<circle class="default" cx="25" cy="25" r="16" fill="none" stroke="black" stroke-width="1"/>
</svg>
Update
8 sectors
<svg width="30%" height="30%" viewBox="0 0 120 120">
<!-- Outer circle -->
<circle class="default" cx="25" cy="25" r="24" fill="none" stroke="black" stroke-width="1"/>
<!-- The circle is divided into 4 sections -->
<circle class="default" cx="25" cy="25" r="20" fill="none" stroke="black"
stroke-width="8" stroke-dasharray="7.85"/>
<!--Inner circle -->
<circle class="default" cx="25" cy="25" r="16" fill="none" stroke="black" stroke-width="1"/>
</svg>
As commented #enxaneta
the dash height would be the stroke-width. So you may set the
stroke-width = radius / 5
stroke-width="4"
How to make the gaps between dashes to be filled with white instead of
being transparent: you draw another circle with a white border and the
same stroke-width beneath the dashed one
Add a second circle
<circle class="gold" cx="25" cy="25" r="20" fill="none" stroke="gold"
stroke-width="4" stroke-dasharray="7.85" />
I added yellow sectors you can add any other color
<svg width="360" height="360" viewBox="0 0 120 120">
<circle class="gold" cx="25" cy="25" r="20" fill="none" stroke="gold"
stroke-width="4" stroke-dasharray="7.85" />
<circle class="black" cx="25" cy="25" r="20" fill="none" stroke="black"
stroke-width="4" stroke-dasharray="7.85" stroke-dashoffset="7.85"/>
</svg>
<svg width="360" height="360" viewBox="0 0 120 120">
<circle class="gold" cx="25" cy="25" r="20" fill="none" stroke="gold"
stroke-width="4" troke-dasharray="7.85" />
<circle class="black" cx="25" cy="25" r="20" fill="none" stroke="black"
stroke-width="4" stroke-dasharray="7.85" stroke-dashoffset="7.85"/>
</svg>

Why is my SVG using center-origin coordinates?

Why are my circles not starting in the upper left corner? What am I missing here?
I'm not sure why 0 of the X axis is in the center of the drawing.
Am I going crazy? :)
<svg xmlns="http://www.w3.org/2000/svg" width="150" height="20" viewBox="0 0 100 20">
<svg x="0" y="0" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="20" y="0" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="40" y="0" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="60" y="0" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="80" y="0" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
</svg>
You are using viewBox="0 0 100 20" This means that the width in user units is 100 and the height is 20. Meanwhile you are using width="150" height="20"
You may delete the height attribute. In this case the height will be calculated so that the rapport w/h stays the same.
If you need width="150" height="20" you may try using a different viewBox="0 0 150 20".
Read more about the viewBox attribute
svg{background:#d9d9d9}
<svg xmlns="http://www.w3.org/2000/svg" width="150" viewBox="0 0 100 20">
<svg x="0" y="0" viewBox="0 0 200 200" width="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="20" y="0" viewBox="0 0 200 200" width="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="40" y="0" viewBox="0 0 200 200" width="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="60" y="0" viewBox="0 0 200 200" width="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
<svg x="80" y="0" viewBox="0 0 200 200" width="20" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100" />
</svg>
</svg>

svg Text Path renders weired in firefox

In Chrome, this fiddle renders just fine, however in firefox, the curved text is a complete mess:
https://jsfiddle.net/a1khn0cs/1/
<path id="curveBelow" d="M 25,75 A 40 40 0 0 0 95,75" stroke="none" fill="none"></path>
<text class="badge-circle-text" x="42" y="40">
<textPath alignment-baseline="middle" text-anchor="middle" href="#curveBelow">Text Belog</textPath>
</text>
I cannot find a reason why this happens in Firefox.
Could someone give a hint, how I could resolve or find the problem?
dominant-baseline="middle" and text-anchor="middle" are attributes of the text. Also in order to center text on a path you need to use startOffset = "50%" for the textPath and text-anchor="middle" for the text. I hope it helps.
<svg viewBox="0 0 120 120" width=120 height=120 version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="81512f4465f92d314502bc64-alpha">
<feColorMatrix type="saturate" values="0" result="desat"></feColorMatrix>
<feComponentTransfer>
<feFuncR type="discrete" tableValues="0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1"></feFuncR>
<feFuncG type="discrete" tableValues="0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1"></feFuncG>
<feFuncB type="discrete" tableValues="0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1"></feFuncB>
</feComponentTransfer>
</filter>
<mask id="81512f4465f92d314502bc64-grungeMask" x="0" y="0" width="180" height="180" maskUnits="objectBoundingBox">
<image x="0" y="0" width="180" height="180" filter="url(#81512f4465f92d314502bc64-alpha)" href="/grunge-mask.png"></image>
</mask>
<clipPath id="81512f4465f92d314502bc64-cutMiddle">
<rect x="0" y="0" width="120" height="45"></rect>
<rect x="0" y="75" width="120" height="45"></rect>
</clipPath>
<path id="81512f4465f92d314502bc64-curveAbove" d="M 25,45 A 40 40 0 0 1 95,45" stroke="none" fill="none"></path>
<path id="81512f4465f92d314502bc64-curveBelow" d="M 25,75 A 40 40 0 0 0 95,75" stroke="none" fill="none"></path>
<mask id="81512f4465f92d314502bc64-mainText">
<rect width="100%" height="100%" fill="#fff" x="0" y="0"></rect>
<text x="50%" y="50%" fill="#000" dominant-baseline="middle" text-anchor="middle">Text Center</text>
</mask>
</defs>
<g mask="url(#grungeMask)">
<circle class="badge-circle-outer-big" cx="60" cy="60" r="50" stroke="black" stroke-width="3" fill="none" clip-path="url(#81512f4465f92d314502bc64-cutMiddle)"></circle>
<circle class="badge-circle-outer-small" cx="60" cy="60" r="47" stroke="black" stroke-width="1" fill="none" clip-path="url(#81512f4465f92d314502bc64-cutMiddle)"></circle>
<circle class="badge-circle-inner-small" cx="60" cy="60" r="26" stroke="black" stroke-width=".5" fill="none" clip-path="url(#81512f4465f92d314502bc64-cutMiddle)"></circle>
<circle class="badge-circle-inner-big" cx="60" cy="60" r="24" stroke="black" stroke-width="1.5" fill="none" clip-path="url(#81512f4465f92d314502bc64-cutMiddle)"></circle>
<text dominant-baseline="middle" text-anchor="middle" class="badge-circle-text">
<textPath startOffset="50%" href="#81512f4465f92d314502bc64-curveAbove">Text Above</textPath>
</text>
<text dominant-baseline="middle" text-anchor="middle" class="badge-circle-text">
<textPath startOffset="50%" href="#81512f4465f92d314502bc64-curveBelow">Text Belog</textPath>
</text>
<path d="M 0,45 L 120,45 L 110,60 L 120,75 L 0,75 L 10,60" fill="black" class="badge-main" mask="url(#81512f4465f92d314502bc64-mainText)"></path>
</g>
</svg>

Resources