【发布时间】:2021-01-18 20:58:49
【问题描述】:
我正在尝试旋转地球仪(正交投影)。 我现在所拥有的确实可以旋转地球,尽管它非常不稳定,并且在我拖动它后会破坏地图的外观(标线和海洋填充)
如何改进我的代码以使其变得更好? 以下是相关代码:
const svg = d3.select('svg');
const projection = d3.geoOrthographic()
const graticule = d3.geoGraticule();
let pathGenerator = d3.geoPath().projection(projection);
const g = svg.append('g');
g.append('path')
.attr('class', 'sphere')
.attr('d', pathGenerator({type: 'Sphere'}));
g.append('path')
.datum(graticule)
.attr("class", "graticule")
.attr("d", pathGenerator);
g.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
g.call(d3.zoom().on('zoom', () => {
g.attr('transform', d3.event.transform)
}));
function dragstarted(){
console.log("started");
}
function dragged(){
const rotate = projection.rotate()
const k = 75 / projection.scale()
//console.log(k);
projection.rotate([
rotate[0] + d3.event.dx * k,
rotate[1] - d3.event.dy * k
])
pathGenerator = d3.geoPath().projection(projection)
svg.selectAll("path").attr("d", pathGenerator)
}
function dragended(){
console.log("drag ended");
}
编辑: 现场演示:https://vizhub.com/Glebenator/f44ac266b14f4c92b88113fcc89c389d?edit=files&file=index.html
【问题讨论】:
-
你有现场演示或 JSFiddle 之类的吗?了解您所说的“破坏地图外观”的含义会很有帮助。
-
很公平,我会尝试制作一个。
-
此外,演示网站上的地图实际上比我的本地机器看起来更平滑......我想我只需要在拖动时正确更新所有元素,但我不知道该怎么做。
-
好吧..这是你想要的样子吗? vizhub.com/jgphilpott/…
-
@JacobPhilpott,这周会去看看,有一些建议。
标签: javascript d3.js map-projections