gpt4 book ai didi

javascript - 3djs,力图。如何保留缩放级别

转载 作者:行者123 更新时间:2023-11-28 06:26:27 27 4
gpt4 key购买 nike

我正在尝试将缩放和比例级别保存到数据库。我取得了进步,但有一些问题需要解决。

事情就是这样。在我的力图初始渲染中,我的缩放级别是准确的。换句话说,这就是我所做的:

 svg.append("defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("marker")
.attr("id", function (d) {
return d;
})
.attr("viewBox", "0 -5 10 10")
.attr("refX", 25)
.attr("refY", 0)
.attr("markerWidth", 4)
.attr("markerHeight", 4)
.attr("orient", "auto")
.append("path")
.attr("d", "M 0,0 m -5,-5 L 5,0 L -5,5 Z")
.style("stroke", "#4679BD")
.style("opacity", "0.6")
.attr("transform", "translate(" + zoomWidth + "," + zoomHeight + ") scale(" +
scale + ")");
//This line restores the zoom
svg.attr("transform", "translate(" + graph.zoomTranslateX + "," +
graph.zoomTranslateY + ") scale(" + graph.scale + ")")

这是设置初始缩放和比例级别的代码行,它工作得很好:

 svg.attr("transform", "translate(" + graph.zoomTranslateX + "," + 
graph.zoomTranslateY + ") scale(" + graph.scale + ")")

加载图表后,如果我检查 html 中的 SVG 元素,我可以看到它已正确设置:

<g transform="translate(-12632.3077320904,-830.9843396749) scale(24.25)"

当我使用鼠标滚轮放大或缩小时出现问题。会发生什么情况我的初始值被覆盖,并且设置了一些(默认?)其他值。

这是我的缩放功能:

function zoomed() {
var zoomLevels = sessionStorage.getItem("graphdata");
var storedGraphJSON = JSON.parse(zoomLevels.replace(/\bNaN\b/g, 'null'));
//Calculation of zoom properties:
//d3.event.translate[0] = d3.event.translate[0] -
storedGraphJSON.zoomTranslateX;
//d3.event.translate[1] = d3.event.translate[1] -
storedGraphJSON.zoomTranslateY;
//d3.event.scale = d3.event.scale - storedGraphJSON.scale;
svg.attr("transform", "translate(" + d3.event.translate + ")scale(" +
d3.event.scale + ")");

graph.zoomTranslateX = storedGraphJSON.zoomTranslateX =
d3.event.translate[0];
graph.zoomTranslateY = storedGraphJSON.zoomTranslateY =
d3.event.translate[1];
graph.scale = storedGraphJSON.scale = d3.event.scale;

sessionStorage.setItem("graphdata", JSON.stringify(storedGraphJSON));
}

据我所知,d3.event.translate 和 d3.event.scale 应该获取在 svg 元素上设置的初始值......但事实并非如此。

任何见解都值得赞赏。

最佳答案

当创建初始缩放变量时,它必须具有预定义值,以便它可以继续,如下所示:

var zoom = d3.behavior.zoom()
.scaleExtent([-40, 100])
.on("zoom", zoomed);
//Make sure zoom picks from the initial value that is set on DOM!!!
zoom.scale(graph.scale);
zoom.translate([graph.zoomTranslateX, graph.zoomTranslateY]);

工作就像魅力:)

关于javascript - 3djs,力图。如何保留缩放级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35085513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com