javascript - 更改D3 geoCircle的大小和 Angular

标签 javascript d3.js

如何更改两个重叠的圆圈的大小,例如固定为 50px 半径?

另一个问题是如何旋转黄色圆圈,例如 30 度?

我用当前进度创建了一个 fiddle :jsFiddle

var svg = d3.select("svg"),
    circle = d3.geoCircle()
    projection = d3.geoOrthographic()

    path = d3.geoPath()
      .projection(projection)

    moonBackground = svg.append("path")
      .attr("fill", "#2b281b")
      .attr("d", path(circle())),

    moon = svg.append("path")
      .attr("fill", "yellow")
      .attr("d", path(circle.center([140, 0])()));

最佳答案

我看到你正在尝试画月亮。嗯,我相信 d3.geoCircle 是执行该任务的错误工具。话虽这么说,你所做的只是一种黑客行为。

无论如何,这是我的 2 美分:

您可以使用 circle.radius() 设置圆的半径:

If radius is specified, sets the circle radius to the specified angle in degrees, and returns this circle generator.

但不要这样做,因为这会扰乱定位黄色路径时的“阴影”效果。相反,使用 projection.scale()缩小月球的大小。

要旋转月球的发光部分,您可以调整 center圆的(这不是正确的方法,但无论如何这整个代码都是一个黑客!):

.attr("d", path(circle.center([140, -30])()));

这是你的月亮(我在背景上放了一些星星,只是因为):

var svg = d3.select("svg"),
  circle = d3.geoCircle(),
  projection = d3.geoOrthographic().translate([150, 150]).scale(100);

path = d3.geoPath()
  .projection(projection),

  circles = svg.selectAll(null)
  .data(d3.range(100))
  .enter()
  .append("circle")
  .attr("r", 1)
  .attr("fill", "white")
  .attr("cx", () => Math.random() * 300)
  .attr("cy", () => Math.random() * 300),

  moonBackground = svg.append("path")
  .attr("fill", "#2f2c1f")
  .attr("d", path(circle())),

  moon = svg.append("path")
  .attr("fill", "yellow")
  .attr("d", path(circle.center([140, -30])()));
svg {
  background-color: black;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="300" height="300"></svg>

关于javascript - 更改D3 geoCircle的大小和 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566340/

相关文章:

javascript - 如何在圆环图中的扇区之间缩进?

javascript - 将 DOM 元素传递给 jQuery 自定义插件

javascript - 在不注入(inject) $rootScope 的情况下对指令中的 Angular 事件使用react

javascript - 选择视频/音频应用程序的信令协议(protocol)(WebRTC API)

javascript - Controller 属性更改不会影响某些指令的整个绑定(bind)属性

d3.js - 如何保持 SVG 标记的宽度和高度?

javascript - Svg D3.js 如何使用条件选择全部

javascript - d3 序数尺度映射

d3.js - D3 树布局可视化 - 继承具有多个父级的子级

javascript - svg 和 D3 中的 cx、cy vs 变换,有什么区别?