【发布时间】:2018-01-15 10:18:08
【问题描述】:
我想为我正在使用 d3 和 SVG 制作的绘图设置阴影,但我遇到了阴影与相邻元素重叠的问题。请参阅下图了解它当前的外观。请注意,中间的六边形似乎具有不同的高度,因为阴影正在其中一些顶部渲染。我想做的是设置阴影,使其仅在背景上呈现,而不是在其他相邻的六角形之上。
这是当前如何定义阴影的代码:
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", 1)
.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", 1)
.attr("dy", 1)
.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");
然后将这些样式应用于六边形:
d3.select(this).style("filter", "url(#drop-shadow)")
【问题讨论】:
标签: javascript css d3.js svg