开发者

How do you draw an arrow at the end of a bezier curve in SVG / raphael?

开发者 https://www.devze.com 2023-04-07 05:06 出处:网络
I have a curve generated by this: 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\",

I have a curve generated by this:

    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(",");

How do you draw an arrow at the end of a bezier curve in SVG / raphael?

but my arrow is not correctly done.

- it only points to the right, and doesn't point the same direction as the slope of the curve at the end of the bezier. - it is not filled

now, I know I'm not doing the math correctly here, but mainly, I just want to know how to fill the triangle at the end of the line. thanks!

For demo purposes:

        arrow_left_x = (x1 - 8);
        arrow_left_y = (y1 - 8);
        arrow_right_x = (x1 - 8);
        arrow_ri开发者_运维百科ght_y = (y1 + 8);

that is the code for getting the the coordinates I use.


I guess you should use markers for your purpose. See an example here.

Edit:

You need to create a marker in the defs section:

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);

and referense it in CSS:

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

or as an attribute:

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

Here's the resulting jsfiddle

0

精彩评论

暂无评论...
验证码 换一张
取 消