我正在研究世界交互式地图(缩放+拖动)上的Orthogonal和Mercator投影之间的过渡。我的尝试是基于this answer @mbostock。我的过渡效果很好,但控制台会返回:
错误:属性d =&#34的值无效; M399.97994119370463,631.2543931983346L401.90905516286807,634.6157013775801L402.49382423943246,639.8924592362391L397.1999166487309,......., InfinityL492,InfinityL298.23496934976544 ,InfinityZ "
也
错误:属性d的值无效=" M407.5277462873383,448.4656515493841L407.3237573717514,448.5198670656254L408.0599991645186,448.54332648718287L408.0215749188842,448.4385574971469ZM410.40575858016456,448.5990104765492L410.8017911794915,448.571665201377L411.4470970198755 .. 。,NaNL415,NaNL415,的 NaNZM415,NaNL415,NaNL415,NaNZM415,NaNL415 下,449.0295187376797L415,449.0295187376797L414.9999381778805,449.02952802059315L415,449.02952802059315L414.41200773961845,.."
我正在使用Natural Earth shapefile中的topoJson,每次返回补间函数时,我只会收到其中一个路径错误。我相信这意味着只有一个路径元素出现此问题。
以下是我在投影类中的转换方法:
animation(timeout, newProjection) {
let self = this;
let projection = this.interpolatedProjection(this.prjFnc, newProjection);
let path = d3.geo.path().projection(projection);
this.svg.transition()
.duration(timeout / 2)
.tween('projection', function() {
return function(_) {
self.alpha(_);
self.svg.selectAll('path').attr('d', path);
};
});
}
interpolatedProjection(a, b) {
let self = this;
this.a = a;
this.b = b;
this.α = null;
function raw(λ, φ) {
let pa = self.a([λ *= 180 / Math.PI, φ *= 180 / Math.PI]), pb = self.b([λ, φ]);
return [(1 - self.α) * pa[0] + self.α * pb[0], (self.α - 1) * pa[1] - self.α * pb[1]];
}
this.intProjection = d3.geo.projection(raw).scale(1);
this.center = this.intProjection.center;
this.translate = this.intProjection.translate;
delete this.intProjection.scale;
delete this.intProjection.translate;
delete this.intProjection.center;
return this.alpha(0.1);
}
alpha(_) {
if(!arguments.length) {
return this.α;
}
this.α = +_;
let ca = this.a.center(),
cb = this.b.center(),
ta = this.a.translate(),
tb = this.b.translate();
this.center(
[(1 - this.α) * ca[0] + this.α * cb[0],
(1 - this.α) * ca[1] + this.α * cb[1]]
);
this.translate(
[Math.round((1 - this.α) * ta[0] + this.α * tb[0]),
Math.round((1 - this.α) * ta[1] + this.α * tb[1])]
);
return this.intProjection;
}
有什么想法吗?
答案 0 :(得分:0)
无穷大错误是由尝试在svg之外绘制的路径引起的。我可以通过向插值投影添加剪辑范围来删除这些错误。
interpolatedProjection(a, b) {
let self = this;
this.a = a;
this.b = b;
this.α = null;
function raw(λ, φ) {
let pa = self.a([λ *= 180 / Math.PI, φ *= 180 / Math.PI]), pb = self.b([λ, φ]);
return [(1 - self.α) * pa[0] + self.α * pb[0], (self.α - 1) * pa[1] - self.α * pb[1]];
}
this.intProjection = d3.geo.projection(raw).scale(1)
.clipExtent(this.extent); // this addition
this.center = this.intProjection.center;
this.translate = this.intProjection.translate;
delete this.intProjection.scale;
delete this.intProjection.translate;
delete this.intProjection.center;
return this.alpha(0.1);
}
我也在d属性中收到NaN错误,我可以通过不让补间索引达到值1来删除。
animation(timeout, newProjection) {
let self = this;
let projection = this.interpolatedProjection(this.prjFnc, newProjection);
let path = d3.geo.path().projection(projection);
this.svg.transition()
.duration(timeout)
.tween('projection', function() {
return function(_) {
if (_ === 1) { return; } // This addition
self.alpha(_);
self.paths.attr('d', path);
};
});
}