[英]Draw arrow on svg path without using textPath
我會將箭頭放置在路徑的其他區域。 實際上我正在使用textPath
。
textPath
的問題在於它與路徑鏈接,並且我不能在它們之間放置元素(例如,路徑上方的圓圈但箭頭下方)。 另外,我僅限於文字字符來繪制箭頭。
是否可以在沒有textPath
的情況下執行相同的textPath
我會使用自己繪制的箭頭,並遵循其方向將其放置在直線上?
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/textPath --> <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <!-- to hide the path, it is usually wrapped in a <defs> element --> <!-- <defs> --> <circle cx="75" cy="45" r="5" fill="blue"/> <path id="MyPath" fill="none" stroke="red" d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 Q70,70 75,50" /> <!-- </defs> --> <text dominant-baseline ='central'> <textPath href="#MyPath" startOffset='100%' text-anchor='middle'>⯈</textPath> </text> </svg>
這是js解決方案:
let len = MyPath.getTotalLength() requestAnimationFrame(draw); function draw(t){ requestAnimationFrame(draw); let p1 = MyPath.getPointAtLength((t/33)%len); let p2 = MyPath.getPointAtLength((t/33)%len + 10); let a = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI triangle.setAttribute('transform', `translate(${p1.x},${p1.y})rotate(${a})`) }
<body style="overflow:hidden;margin:0"> <svg viewBox="0 0 100 100" width=100vw height=100vh> <path id="MyPath" fill="none" stroke="red" d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 Q70,70 75,50" /> <circle cx="75" cy="45" r="5" fill="blue"/> <path id=triangle d="m0,0 v-5l10,5l-10,5z"/> </svg> </body>
textPath方法允許您使用文本字符的路徑
可以使用unicode三角形符號: ⏵
⏵
<svg width="50%" height="50%" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <circle cx="75" cy="45" r="5" fill="blue"/> <path id="MyPath" fill="none" stroke="red" d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 Q70,70 75,50" /> <text font-size="20" font-family="Times New Roman" fill="black" > <textPath xlink:href="#MyPath" startOffset='0%' > <tspan dx="0" dy="6"> ⏵ </tspan> <animate dur="10s"repeatCount="1" attributeName="startOffset" values="0%;95.6%" fill="freeze"/> </textPath> </text> </svg>
<svg width="50%" height="50%" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <circle cx="75" cy="45" r="5" fill="blue"/> <path id="MyPath" fill="none" stroke="red" d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 Q70,70 75,50" /> <text font-size="20" font-family="Times New Roman" fill="black" > <textPath xlink:href="#MyPath" startOffset='0%' > <tspan dx="0" dy="6"> ⏵ </tspan> <tspan dx="-19" fill="red"> ⏵ </tspan> <tspan dx="-19" fill="gold" > ⏵ </tspan> <tspan dx="-19" fill="green" > ⏵ </tspan> <animate dur="10s" repeatCount="1" attributeName="startOffset" values="0%;89%" fill="freeze"/> </textPath> </text> </svg>
<svg width="600" height="400" viewBox="100 100 400 300"> <path id="pathTrain" d="M100,200 C100,100 250,100 250,200 S 400,300 400,200" stroke="grey" fill="none"/> <text font-size="32" font-family="Times New Roman" fill="black" > <textPath id="result" xlink:href="#pathTrain"> <tspan dx="0" > 🚅 </tspan> <tspan dx="-15"> 🚃 </tspan> <tspan dx="-15"> 🚃</tspan> <tspan dx="-15"> 🚃</tspan> <tspan dx="-15">🚃</tspan> <tspan dx="-15" > 🚅 </tspan> <animate dur="7s" repeatCount="indefinite" attributeName="startOffset" values="55%;0%;55%"/> </textPath> </text> </svg>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.