I create my image pattern like this:
mainSVG
.append('defs')
.append('pattern')
.attr('id', `pic${vertex.id}`)
.attr('patternUnits', 'userSpaceOnUse')
.attr('x', -vertex.radius)
.attr('y', -vertex.radius)
.attr('width', vertex.radius * 2)
.attr('height', vertex.radius * 2)
.append('image')
.attr('fill', 'red')
.attr('xlink:href', vertex.picture)
.attr('width', vertex.radius * 2)
.attr('height', vertex.radius * 2);
And I create my circle like this:
node
.enter()
.append('g')
.attr('class', 'node')
.call(force.drag)
.append('svg:circle')
.attr('r', x => x.radius)
.attr('id', x => `node${x.id}`)
.attr('class', 'nodeStrokeClass')
.attr('stroke', 'gray')
.attr('stroke-width', '2')
.attr('fill', x => `url(#pic${x.id})`)
.on('mouseover', function(v) {
d3.select(`#node${v.id}`)
.transition()
.attr('r', v.radius * 1.5);
})
.on('mouseout', function(v) {
d3.select(`#node${v.id}`)
.transition()
.attr('r', v.radius);
});
How can I have the image scale with the circle?