根据谷歌的材料设计,阴影应该接近现实世界的条件,因为人类具有巨大的感知能力。因此,阴影应该由环境分量和投射分量组成。在此处查看 Google 的规范。
http://www.google.com/design/spec/what-is-material/environment.html#environment-light-shadow
以上都是很好的阴影例子,但我找不到任何复合阴影的例子,所以我想分享这个定制的过滤器。它似乎确实使最终结果看起来更现实。
var id = "md-shadow";
var deviation = 2;
var offset = 2;
var slope = 0.25;
var svg = d3.select("#yoursvg");
var defs = svg.select("defs");
// create filter and assign provided id
var filter = defs.append("filter")
.attr("height", "125%") // adjust this if shadow is clipped
.attr("id", id);
// ambient shadow into ambientBlur
// may be able to offset and reuse this for cast, unless modified
filter.append("feGaussianBlur")
.attr("in", "SourceAlpha")
.attr("stdDeviation", deviation)
.attr("result", "ambientBlur");
// cast shadow into castBlur
filter.append("feGaussianBlur")
.attr("in", "SourceAlpha")
.attr("stdDeviation", deviation)
.attr("result", "castBlur");
// offsetting cast shadow into offsetBlur
filter.append("feOffset")
.attr("in", "castBlur")
.attr("dx", offset - 1)
.attr("dy", offset)
.attr("result", "offsetBlur");
// combining ambient and cast shadows
filter.append("feComposite")
.attr("in", "ambientBlur")
.attr("in2", "offsetBlur")
.attr("result", "compositeShadow");
// applying alpha and transferring shadow
filter.append("feComponentTransfer")
.append("feFuncA")
.attr("type", "linear")
.attr("slope", slope);
// merging and outputting results
var feMerge = filter.append("feMerge");
feMerge.append('feMergeNode')
feMerge.append("feMergeNode")
.attr("in", "SourceGraphic");
这可以应用于对象,就像这样..
var r = element.append("rect")
.attr("class", "element-frame")
: // other settings
.style("filter", "url(#md-shadow)");