javascript - 发光的球体 - d3 正交投影无法让 css 工作

标签 javascript css svg d3.js

我正在尝试结合这两个 d3 示例:

http://bl.ocks.org/mbostock/4183330

http://bl.ocks.org/mbostock/2206590

我有一个球体,投影显示正确,缩放功能正常。我现在要做的就是设计它的样式。

我之前得到了世界巡回演出的例子,它使用 Canvas ,我能够给它一个阴影来创造我非常喜欢的发光效果。

合并这两个代码片段后,我现在正在使用 svg 元素,但似乎无法使发光效果发挥作用。

这是我的代码(.globe 类的 fill 属性似乎有效):

<!DOCTYPE html>
<meta charset="utf-8">
 <style>

body {
  background: #000000;
}

.background {
  fill: none;
  pointer-events: all;
}

.feature {
  fill: #ccc;
  cursor: pointer;
}

.feature.active {
  fill: #00FF15;
}

.globe
{
  fill:#fff;
  strokeStyle: #35C441; 
  lineWidth: 5; 
  shadowColor: #35C441;
  shadowBlur: 40;
  shadowOffsetX: 0;
  shadowOffsetY: 0;
}

.mesh {
  fill: none;
  stroke: #fff;
  stroke-linecap: round;
  stroke-linejoin: round;
}

</style>
<body>
<script src="d3/d3.v3.min.js"></script>
<script src="d3/topojson.v1.min.js"></script>
<script>

var width = 960,
    height = 720;
    active = d3.select(null);

var globe = {type: "Sphere"};

var projection = d3.geo.orthographic()
    .scale(height / 2.1)
    .translate([width / 2, height / 2])
    .clipAngle(90)
    .precision(.5);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

//append a rectange to the svg element. give it the background css style class.
//on click do reset?
svg.append("rect")
    .attr("class", "background")
    .attr("width", width)
    .attr("height", height)
    .on("click", reset);

//append "g" to the svg element
var g = svg.append("g")
    .style("stroke-width", "1.5px");

var path = d3.geo.path()
    .projection(projection)

d3.json("./world-110m.json", function(error, world) {
  g.append("path")
        .datum(globe)
        .attr("class", "globe")
        .attr("d", path);

  g.selectAll("path")
    .data(topojson.feature(world, world.objects.countries).features)
    .enter().append("path")
    .attr("d", path)
    .attr("class", "feature")
    .on("click", clicked);



 g.append("path")
      .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
      .attr("class", "mesh")
      .attr("d", path);

});

function clicked(d) {
  if (active.node() === this) return reset();
  active.classed("active", false);
  active = d3.select(this).classed("active", true);

  var bounds = path.bounds(d),
      dx = bounds[1][0] - bounds[0][0],
      dy = bounds[1][1] - bounds[0][1],
      x = (bounds[0][0] + bounds[1][0]) / 2,
      y = (bounds[0][1] + bounds[1][1]) / 2,
      scale = .9 / Math.max(dx / width, dy / height),
      translate = [width / 2 - scale * x, height / 2 - scale * y];

  g.transition()
      .duration(750)
      .style("stroke-width", 1.5 / scale + "px")
      .attr("transform", "translate(" + translate + ")scale(" + scale + ")");
}

function reset() {
  active.classed("active", false);
  active = d3.select(null);

  g.transition()
      .duration(750)
      .style("stroke-width", "1.5px")
      .attr("transform", "");
}

</script>

</body>
</html>

如果有人能提供帮助那就太好了,或者如果答案已经存在于此,请您指出正确的方向

谢谢!

最佳答案

如果您附上一张您想要的效果的图片,将会有所帮助。

也就是说,您的 CSS 对 SVG 元素无效:

enter image description here

前两个有对应的样式:

.globe {
   fill:#fff;
   stroke: #35C441; 
   stroke-width: 5; 
}

阴影,are a bit trickier .

关于javascript - 发光的球体 - d3 正交投影无法让 css 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27569194/

相关文章:

javascript - platform.js 和 analytics.js 被 AdBlocker 拦截

javascript - 如何检测正在拖动哪个div

javascript - 通过 JavaScript 创建 SVG 元素

javascript - 如何获取 Fabric JS 中形状的坐标?

javascript - 我可以使用 Materialise 在 View 中处理多个 .html 吗?

javascript - jsPDF和图像加载

javascript - Jquery不 append html的特定部分

Avenir 的 iOS Safari 字体字距调整/字母间距问题

html - 绝对元素的百分比值是指浏览器的宽度和高度?

css - svg滤镜阴影路径修复