I'm trying to create directional arrows on an svg path that's being rendered using d3. I obtain the start points using the path string and the svg-path-properties npm package. I then push these points to an array, and in another function, I map these points onto the original svg path with the d3 logic to create triangles at each point.
public mapArrowPoints(): void {
this.arrowPoints.forEach((point: ArrowPointObject) => {
const path = d3.path();
path.moveTo(parseInt(point.x, 10), parseInt(point.y, 10));
path.lineTo(parseInt(point.x, 10) + 8, parseInt(point.y, 10) + 2);
path.lineTo(parseInt(point.x, 10) + 0, parseInt(point.y, 10) + 8);
path.closePath();
const svg = d3.select('svg#topLevel');
const defs = svg.insert('defs', '#curve');
const marker = defs.append('marker');
marker
.attr('id', 'triangle')
.attr('viewBox', '0 -5 10 10')
.attr('markerWidth', 4)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('path')
.attr('d', path.toString())
.attr('fill', 'red')
.attr('stroke', 'black')
.attr('stroke-width', '3px');
svg
.select('#curve')
.attr('marker-start', 'url(#triangle)')
.attr('marker-mid', 'url(#triangle)')
.attr('marker-end', 'url(#triangle)');
});
}
Now, when I check the browser, I'm able to see my main path, but no triangles. I can inspect the DOM, and see the svg
, the defs
, marker
, and the path
they're supposed to be appended onto. I can also select the marker path itself, and see where it should be rendering (a blue box shows where the triangle should be). I can't quite figure out why the triangles aren't showing. Any help will be much appreciated. I've created a stackblitz example for experimenting with solutions.