【问题标题】:How do you draw an arrow at the end of a bezier curve in SVG / raphael?如何在 SVG / raphael 中的贝塞尔曲线末端绘制箭头?
【发布时间】:2011-09-23 14:35:54
【问题描述】:

我有一条由此生成的曲线:

    var path = ["M", x1.toFixed(3), y1.toFixed(3), "L", arrow_left_x, arrow_left_y, "L", arrow_right_x, arrow_right_y, "L", x1.toFixed(3), y1.toFixed(3), "C", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(",");

但我的箭头没有正确完成。
- 它只指向右边,并不指向与贝塞尔曲线末端的曲线斜率相同的方向。 - 没有填满

现在,我知道我在这里的数学运算不正确,但主要是,我只想知道如何填充行尾的三角形。谢谢!

出于演示目的:

        arrow_left_x = (x1 - 8);
        arrow_left_y = (y1 - 8);
        arrow_right_x = (x1 - 8);
        arrow_right_y = (y1 + 8);

这是获取我使用的坐标的代码。

【问题讨论】:

标签: javascript svg raphael


【解决方案1】:

我猜你应该使用标记来达到你的目的。请参阅example here

编辑:

您需要在defs 部分创建一个标记:

var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
marker.setAttribute('id', 'Triangle');
marker.setAttribute('viewBox', '0 0 16 16');
marker.setAttribute('refX', '0');
marker.setAttribute('refY', '6');
marker.setAttribute('markerUnits', 'strokeWidth');
marker.setAttribute('markerWidth', '16');
marker.setAttribute('markerHeight', '12');
marker.setAttribute('orient', 'auto');
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
marker.appendChild(path); 
path.setAttribute('d', 'M 0 0 L 16 8 L 0 16 z');   
path.setAttribute('stroke', '#000');   
path.setAttribute('stroke-width', '1'); 
path.setAttribute('style', '  marker-start :none;   marker-end :none; ');      

document.getElementById( id_of_svg_placeholder ).getElementsByTagName('svg')[0].appendChild(defs);
defs.appendChild(marker);

并在 CSS 中引用它:

path {marker-start:url("#Triangle")}

或作为属性:

<path d="..." marker-start="url(#Triangle)" />

Here's the resulting jsfiddle

【讨论】:

  • 好的,我知道它现在是如何工作的了。但是,它与 Raphael 不兼容,因为它使用标签而不是属性,如果您以某种方式编辑答案,我会将反对票更改为赞成票。或者,如果您能弄清楚如何在此处获取标记:jsfiddle.net/G5mTx/10 那会很棒
  • 我希望这是您所需要的 jsfiddle.net/G5mTx/29 。不要害怕使用原生 DOM 方法。并且可能是 Raphael 与创建 SVG 标记不兼容,而不是相反。另外,我想对提供帮助并为您的问题提供关键的人投反对票并不是太友好。
  • 除非您编辑答案,否则我无法投票。 =\这正是我需要的!你太棒了!
  • 谢谢!这也很容易在我的新代码中实现:jsfiddle.net/G5mTx/30
  • “对提供帮助的人投反对票不太友好”#TrueStory
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 2013-08-21
相关资源
最近更新 更多