I want to make a image fill inside svg circle, but avoid the "pattern" mode like in this Stackoverflow Link because of the circular positioning.
So I tried this:
var defs = group
.append('svg:defs')
defs
.append("svg:image")
.attr("id", "chara_avatar")
.attr("xlink:href", imglink)
.attr("width", 3*rad)
.attr("height", 2*rad)
.attr('x', (d,i)=>{ //same x position as the circle
let x = (r * Math.sin(Math.PI * 2 * (i * 36) / 360)) + cx
return x-rad;
})
.attr('y',(d,i)=>{ //same y position as the circle
let y = (r * Math.cos(Math.PI * 2 * (i * 36) / 360)) + cy
return y-rad;
});
And directly use with "url('#chara_avatar')"
:
var graphic = group
.append('circle')
.attr('cx', (d,i)=>{
let x = (r * Math.sin(Math.PI * 2 * (i * 36) / 360)) + cx
return x;
})
.attr('cy',(d,i)=>{
let y = (r * Math.cos(Math.PI * 2 * (i * 36) / 360)) + cy
return y;
})
.attr('r',rad)
.style('fill', (d)=> {
let init = d.val;
if(init==1) {
return "url(#chara_avatar)" // <------- called the url for fill
} else if (init==0) {
return 'black'
}
})
.style('stroke', 'black')
.style('stroke-width', '3px')
.style('cursor',(d)=> {
let init = d.val;
if (init==1) {
return 'pointer'
}
})
- I got the position right, the element are there, but it doesn't show up.
- The image tag AFAIK was properly put, but it shows only
href
notxlink:href
even I write'xlink:href'
If I change defs
to group
(append the image directly to group), it works and place the image above the circle, & I dont know how to clip it. I already used ClipPath but not working.
Full code example: Codepen Link
UPDATE:
Didn't find any good answer here, and I choose appending divs than svgs, it works with a bit more lines