[英]SVG lines with text on top
每次线条移动时,文本应自动随之移动
我认为 go 最直接的方法是:
<g>
将<line>
和<text>
组合在一起; 接着<g>
元素。注意,我在下面的示例中使用了 CSS animation 而不是 javascript,但原则成立。
工作示例:
svg { width: 500px; height: 210px; } g { transform-origin: 100%; animation: rotateGroup 3s linear infinite; } line { stroke: rgb(255,0,0); stroke-width: 4px; } text { fill: rgb(255, 0, 0); font-size: 36px; } @keyframes rotateGroup { 0% {transform: rotate(0deg);} 100% {transform: rotate(360deg);} }
<svg viewBox="0 0 105 250"> <g> <line x1="0" y1="185" x2="200" y2="185" /> <text x="22" y="176">Hello there</text> </g> </svg>
附加说明:
你提到:
无论长度如何,始终保持在线条的中心
为此,您可以在每次重新计算<line>
的x1
和x2
值时重新计算<text>
的x
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.