Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CSS/SVG, how to rotate each character of a word?

Here is a Jsfiddle demo

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<svg width="100%" height="100%" viewBox="0 0 1000 300" 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 class="material-icons">
        <textPath xlink:href="#MyPath">&#xe55d; &#xe55d; &#xe55d; &#xe55d;</textPath>
    </text>
    <!-- Show outline of the viewport using 'rect' element -->
    <rect x="1" y="1" width="998" height="298" fill="none" stroke="black" stroke-width="2" />
</svg>

&#xe55d; is a special character which is displayed as an "arrow".

There is a problem in the demo above: The direction of the arrow in the <textPath> is perpendicular to the <path>, while I need to set its direction to be the same (in parallel) as the path.

Therefore, I need to rotate 90 degree for each character in the text &#xe55d; &#xe55d; &#xe55d; &#xe55d; (not the whole sentence)..

I found this post about rotating characters, but it looks not ideal because it needs to "Encapsule each letter in a element with jQuery". Maybe there is a way to do this easier in SVG?

Does anyone have ideas about how to rotate each character of a word in <textPath> node?

like image 632
Hanfei Sun Avatar asked Jun 27 '15 16:06

Hanfei Sun


1 Answers

Just add this style="writing-mode: tb; glyph-orientation-vertical: 180;" to text

I hope this is what you wanted :

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<svg width="100%" height="100%" viewBox="0 0 1000 300" 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 class="material-icons" style="writing-mode: tb; glyph-orientation-vertical: 180;">
        <textPath xlink:href="#MyPath">&#xe55d; &#xe55d; &#xe55d; &#xe55d;</textPath>
    </text>
    <!-- Show outline of the viewport using 'rect' element -->
    <rect x="1" y="1" width="998" height="298" fill="none" stroke="black" stroke-width="2" />
</svg>
like image 137
Siddharth Thevaril Avatar answered Sep 28 '22 10:09

Siddharth Thevaril