html - 更改 CSS SVG 中文本的方向
问题描述
我有以下 SVG 代码:https ://jsfiddle.net/danpan/m3ofzrc1/生成打击图像。
有两行文字——围绕一个圆圈:
HELLO_WORLD_1
HELLO_WORLD_2
如何将其中一条线“HELLO_WORLD_2”的方向更改为顺时针方向?
<svg width="132" height="132">
<defs>
<path id="text-path" d="M66 126A60 60 0 1 0 66 6a60 60 0 0 0 0 120" fill="none"/>
</defs>
<text>
<textPath xlink:href="#text-path" font-family="Manrope3-ExtraBold, Manrope3" font-size="10" font-weight="600" fill="#001A62" letter-spacing="3.14">HELLO_WORLD_1 * HELLO_WORLD_2</textPath>
</text>
</svg>
解决方案
简短回答:
要解决这个问题,您需要将文本和路径分成两部分。文本的顶部将遵循顶部路径。文字的下部,分别沿着下部路径。
<svg width="150" height="150" viewBox="-10 -10 150 150" >
<!-- sweep-flag="1" Clockwise text direction -->
<path id="top-path" d="M6 66A60 60 0 1 1 126 66" fill="none" stroke="red"/>
<!-- sweep-flag="0" Counterclockwise text direction -->
<path id="bottom-path" d="M6 66A60 60 0 1 0 126 66" fill="none" stroke="blue"/>
</svg>
文本的两半将从具有坐标的同一点开始并在同一点M6 66
结束126 66
但是文本的上部将顺时针方向 文本sweep-flag = "1"
的下部是逆时针方向 sweep-flag = "0"
<svg width="150" height="150" viewBox="-10 -10 150 150" >
<defs>
<!-- sweep-flag="1" Clockwise text direction -->
<path id="top-path" d="M6 66A60 60 0 1 1 126 66" fill="none" />
<!-- sweep-flag="0" Counterclockwise text direction -->
<path id="bottom-path" d="M6 66A60 60 0 1 0 126 66" fill="none" />
</defs>
<text dx="30" dy="-2">
<textPath xlink:href="#top-path" font-family="Manrope3-ExtraBold, Manrope3" font-size="10" font-weight="600" fill="#001A62" letter-spacing="3.14">HELLO_WORLD_1 </textPath>
</text>
<text dx="20" dy="10">
<textPath xlink:href="#bottom-path" font-family="Manrope3-ExtraBold, Manrope3" font-size="10" font-weight="600" fill="#001A62" letter-spacing="3.14">* HELLO_WORLD_2</textPath>
</text>
</svg>
Tl;博士
文本将位于从起点(Arc start)到终点(Arc end)的弧线上但是从起点到终点的路径 - 弧终点,取决于参数
large-arc-flag和扫旗
您的文本排列变体 - 底行红线
<path d="M6 66A60 60 0 1 1 126 66" />
<path d="M mx,my A rx,ry x-axis-rotation large-arc-flag, sweep-flag x,y" /> , where
- M mx,my – 椭圆弧起点的坐标
- A rx, ry - 椭圆弧半径
- x-axis-rotation – 椭圆相对于当前坐标系的 x 轴旋转 x-axis-rotation 度数
- large-arc-flag - 实现大部分弧的输出的参数,如果(= 1)或更少(= 0)
- sweep-flag - 实现从起点到终点绘制圆弧的方向。如果 sweep-flag = 1,则椭圆弧将顺时针绘制。使用扫描标志 = 0 - 逆时针。
- x,y – 椭圆弧终点的坐标
推荐阅读
- python - python将随机时间索引更改为第二个基本索引
- c# - 使用 IEqualityComparer 比较两个列表,顺序无关紧要
- amazon-s3 - CloudFront 不会附加所有标头
- jquery - 在jQuery中使用dragstart和dragend捕获拖动量
- javascript - JavaScript 对象和在其中创建的元素之间有什么关系?
- java - 带有 Java 8 lambda 代码的 HibernateCallback
- angular - 如何在角度 4 中动态创建具有动态 ID 的 div
- php - 提交后输入文本消失了
- authentication - 如何使用 Auth 0 从 JWT 接收电子邮件
- migration - QNX 6.6 到 QNX 7.0 Neutrino 的区别