I would like to draw a line to connect two rectangle from border to border and pass through both rectangle center. currently, I can draw the line from center to center. I know if I fill the rectangle with white, then the line connection will looks like from border to border. but I do wish to keep rectangle transparent!
What's proper way to implement it?
var svg = d3.select('body').append('svg')
.attr('width',800)
.attr('height',600)
.style('border','1px solid red')
.style('background-color','#e0e0e0')
svg.append("defs")
.append("marker")
.attr("id","pointer")
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("orient","auto")
.attr("refY", 5)
.append("path")
.attr("d", "M 0 0 L 10 5 L 0 10 z")
var data = [
{
id:0,
x:100,
y:100,
width:100,
height:50,
},{
id:1,
x:200,
y:200,
width:100,
height:50,
}
]
var links = [
{
src:0,
dest:1,
}
]
svg.selectAll(null)
.data(data)
.enter()
.append('rect')
.attr('x', d => d.x - d.width/2)
.attr('y', d => d.y - d.height/2)
.attr('width',d => d.width)
.attr('height', d => d.height)
.attr('fill','none')
.attr('stroke','black')
.attr('stroke-width',2)
svg.selectAll(null)
.data(links)
.enter()
.append('line')
.attr('x1',d => data.find(e => e.id === d.src).x)
.attr('y1',d => data.find(e => e.id === d.src).y)
.attr('x2',d => data.find(e => e.id === d.dest).x)
.attr('y2',d => data.find(e => e.id === d.dest).y)
.attr('stroke','black')
.attr("marker-end","url(#pointer)");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.7.0/d3.min.js"></script>