javascript-放大/缩小时在地图上保持D3对象大小不变
作者:互联网
我正在从http://bl.ocks.org/d3noob/raw/5193723/开始
放大时,示例中绘制的线圈尺寸增大.我无法找出可以使圆圈大小保持不变的方法.
有任何想法吗?
编辑1:关于如何保持圆弧半径不变的任何想法.我想出了使圆保持恒定半径的方法,如下所示:
g1.append("circle")
.attr("cx", 200)
.attr("cy", 200)
.attr("r", 10)
.style("fill", "red");
var zoom = d3.behavior.zoom().on("zoom", function () {
g1.selectAll("circle")
.attr("transform", "translate(" + d3.event.translate.join(",") + ")scale(" + d3.event.scale + ")")
.attr("r", function(d) {
return 10/d3.event.scale;
});
});
同样,我也有饼形图的弧形,希望将其大小保持在缩放状态:
var r = 4;
var p = Math.PI * 2;
var arc = d3.svg.arc()
.innerRadius(r - 2)
.outerRadius(r - 1)
.startAngle(0)
.endAngle(p * d.value1);
var arc2 = d3.svg.arc()
.innerRadius(r - 3)
.outerRadius(r - 2)
.startAngle(0)
.endAngle(p * d.value2);
var projection = d3.geo.mercator()
.center([0, 5])
.scale(200)
var translate = "translate(" + projection([77, 13])[0] + ", " + projection([77, 13])[1] + ")";
g.append("path")
.attr("d", arc)
.attr("fill", "maroon")
.attr("transform", translate);
g.append("path")
.attr("d", arc2)
.attr("fill", "green")
.attr("transform", translate);
关于如何保持弧形路径保持相同大小的任何建议?
解决方法:
您必须在zoom事件处理程序中添加一些其他处理.将圆的半径设置为其标称值(5)除以缩放系数(zoom.scale()).这样,当应用缩放比例时,结果将是恒定的视在大小.就像是:
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+
d3.event.translate.join(",")+")scale("+d3.event.scale+")");
g.selectAll("circle")
.attr("d", path.projection(projection))
.attr("r", 5/zoom.scale());
g.selectAll("path")
.attr("d", path.projection(projection));
});
标签:topojson,javascript,d3-js 来源: https://codeday.me/bug/20191011/1895040.html