在d3.js圆中添加阴影效果
Adding drop shadow effects in d3.js circle
我想在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)");
但阴影变成了矩形,我的圆圈不断移动,里面有一个文本标签。
这是工作代码片段。希望这能有所帮助。
从这里转介。
var svg = d3.select("svg");
var defs = svg.append("defs");
var dropShadowFilter = defs.append('svg:filter')
.attr('id', 'drop-shadow')
.attr('filterUnits', "userSpaceOnUse")
.attr('width', '250%')
.attr('height', '250%');
dropShadowFilter.append('svg:feGaussianBlur')
.attr('in', 'SourceGraphic')
.attr('stdDeviation', 2)
.attr('result', 'blur-out');
dropShadowFilter.append('svg:feColorMatrix')
.attr('in', 'blur-out')
.attr('type', 'hueRotate')
.attr('values', 180)
.attr('result', 'color-out');
dropShadowFilter.append('svg:feOffset')
.attr('in', 'color-out')
.attr('dx', 3)
.attr('dy', 3)
.attr('result', 'the-shadow');
dropShadowFilter.append('svg:feBlend')
.attr('in', 'SourceGraphic')
.attr('in2', 'the-shadow')
.attr('mode', 'normal');
var data = [{
sentiment: 5,
x: 100,
y: 200,
radius: 5
}, {
sentiment: 10,
x: 250,
y: 250,
radius: 15
}];
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', "blue")
.style("filter", "url(#drop-shadow)");
svg {
background: white;
border: 2px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg width=500 height=500 />
这可能是你的过滤器尺寸需要大得多(记住默认尺寸是120%宽/高。)
var filter = defs.append("filter")
.attr("id", "drop-shadow")
.attr("width", "300%")
.attr("height", "300%");
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在d3.js饼图或圆环图中添加阴影
- 在d3.js圆中添加阴影效果
- 向阴影框图像添加说明
- 在JSSOR滑块中为图像添加框阴影的任何方法
- 框阴影在由 JavaScript 添加时不会生效
- 添加阴影效果作为innerHTML
- 谷歌地图api,如何为多个自定义标记添加标记阴影
- 猫头鹰旋转木马的阴影添加了白色边框
- 如何为圆形图像添加鼠标悬停阴影效果
- 如何使用.style JavaScript属性添加css框阴影
- svg投影问题在Chrome:而不是添加阴影仅图形的阴影被添加到实际的容器
- 滚动时添加文本阴影(jquery或javascript)
- 如何在D3折线图中添加背景阴影