1
我想用d3.js circle packing example填充一堆SVG的圖案填充圖像svg圈。我的源圖片爲800x600,但圓圈大小各不相同。我已經將它設置如下:使用url填充模式爲svg圈時圖像模糊
var patterns = defs.selectAll("pattern")
.data(nodes.filter(function(d){ return !d.children }))
.enter()
.append('pattern')
.attr('id',function(d){
return 'id'+d.id
})
.attr('x','0')
.attr('y','0')
.attr('height',function(d){ return d.r*2})
.attr('width',function(d){ return d.r*2*1.333333})
.append('image')
.attr('x','0')
.attr('y','0')
.attr('height',function(d){ return d.r*2})
.attr('width',function(d){ return d.r*2*1.333333})
.attr('xlink:href',function(d){
return 'img/img' + d.image;
})
var circle = svg.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
.style("fill", function(d) {
if (!d.children){
return 'url(#id' + d.id + ')';
} else {
return d.children ? color(d.depth) : null;
}
})
讓自己的DOM呈現這樣的:
<defs id="cdef">
<pattern id="id65" x="0" y="0" height="326.8534904318234" width="435.8045449579344">
<image x="0" y="0" height="326.8534904318234" width="435.8045449579344" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img/img65.png"></image>
</pattern>
...
</defs>
但是當我這樣做,在圈內的圖像是超吹出(見附件圖片)。任何想法發生了什麼?
您是否嘗試過使用'patternUnits'屬性?我做了一些與svg中的圖像類似的以下設置:'.attr(「patternUnits」,「objectBoundingBox」)'和'.attr({「viewBox」:「0 0 1 1」})' – fengshuo
Thankyou,同樣的問題很麻煩。 – user2457956