我正在尝试将缩放和缩放级别保存到数据库。我取得了进步,但我有一些问题需要解决。
这是怎么回事。在我的力图初始渲染中,我的缩放级别是准确的。换句话说,这就是我所做的:
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元素上设置的初始值....但它没有。
感谢任何见解。
答案 0 :(得分:0)
制作初始缩放变量时,它必须具有预定义的值,以便它可以继续,如下所示:
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]);
像魅力一样工作:)