SVG marker-mid on specific point on path

浪子不回头ぞ 提交于 2019-12-28 16:06:44

问题


I got some code that generates pathes on canvas. the path objects looks similar to this :

<path class="link" d="M450,215.L265,236L225,236" style="stroke-width: 1;"></path>

and on view (a,b,c letters are just for explaining the problem):

My problem is that I want to draw some arrow (marker) on the middle of the line, between "a" to "b", but when I create a marker and do a marker-mid attribute, its generates on b point.

I've tried to do some point between a and b, but then marker-mid did the arrows both there and both on b point.

from WEB API documentation :

The marker-mid defines the arrowhead or polymarker that shall be drawn at every vertex other than the first and last vertex of the given element or basic shape.

How can I disable the marker on point b? Or how can I make something like arrow between a-b ? Thanks !


回答1:


sometimes its not so easy to split the path at any point you like. Then you can use text on a path with startOffset to position an "arrow" at any point on a path...

path {
  fill: none;
  stroke: red;
  stroke-width: 3
}
text {
  font-size: 35px;
  fill: red;
  dominant-baseline: central
}
<svg viewBox="0 0 500 500" width="300px" height="300px">
  <path class="link" id="path1" d="M0 0 L200 400A300 300 0 0 1 490 150"></path>
  <text >
    <textPath xlink:href="#path1" startOffset="10%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="20%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="30%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="40%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="50%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="60%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="70%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="80%">➤</textPath>
    <textPath xlink:href="#path1" startOffset="90%">➤</textPath>
  </text>
</svg>



回答2:


The marker-mid defines the arrowhead or polymarker that shall be drawn at every vertex other than the first and last vertex of the given element or basic shape.

You are drawing a line with vertices A, B, and C, so by definition the marker will be drawn at B.

If you want a marker to be drawn between A and B you will need to draw a path from A to the midpoint AB to B.

<svg width="200" height="150">
  <defs>
    <marker id="markerArrow" markerWidth="13" markerHeight="13" refX="2" refY="6" orient="auto">
      <path d="M2,2 L2,11 L10,6 L2,2" style="fill: #000;" />
    </marker>
  </defs>
  <path d='M0,0 L50,50 L100, 100' style='marker-mid:url(#markerArrow); stroke: #000'/>
  <path d='M100,100 L125, 100 L150, 100' style='marker-mid:url(#markerArrow); stroke: #000' />
</svg>


来源:https://stackoverflow.com/questions/41105713/svg-marker-mid-on-specific-point-on-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!