【发布时间】:2016-09-06 10:05:36
【问题描述】:
我想为 d3.js 圆圈添加阴影,并且在某种程度上我已经成功地做到了,但是阴影是矩形而不是圆形。
我添加阴影的方式
var defs = svg.append("defs");
// create filter with id #drop-shadow
// height=130% so that the shadow is not clipped
var filter = defs.append("filter")
.attr("id", "drop-shadow")
.attr("height", "130%");
// SourceAlpha refers to opacity of graphic that this filter will be applied to
// convolve that with a Gaussian with standard deviation 3 and store result
// in blur
filter.append("feGaussianBlur")
.attr("in", "SourceAlpha")
.attr("stdDeviation", 5)
.attr("result", "blur");
// translate output of Gaussian blur to the right and downwards with 2px
// store result in offsetBlur
filter.append("feOffset")
.attr("in", "blur")
.attr("dx", 5)
.attr("dy", 5)
.attr("result", "offsetBlur");
// overlay original SourceGraphic over translated blurred opacity by using
// feMerge filter. Order of specifying inputs is important!
var feMerge = filter.append("feMerge");
feMerge.append("feMergeNode")
.attr("in", "offsetBlur")
feMerge.append("feMergeNode")
.attr("in", "SourceGraphic");
var nodes = svg.selectAll("circle")
.data(data);
nodes.enter().append("circle")
.attr("class", function (d, i) { return " circle_"+d.sentiment+" circle_"+i})
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.x; })
.attr("r", function (d) { return d.radius; })
.style('fill', function(d) {
return colors["sentiment"][d["sentiment"]];
})
.style("filter", "url(#drop-shadow)");
但是阴影是矩形的,我的圆圈不断移动,里面有一个文本标签。
【问题讨论】:
-
我似乎找不到一种方法来模糊一个圆圈,因为它最终会调整圆圈周围的空白区域。我知道这不是一个解决方案,但也许一个低不透明度的灰色圆圈会解决这个问题?示例:jsfiddle.net/thatOneGuy/0nv4ck58/1
标签: javascript css d3.js svg