gpt4 book ai didi

javascript - 当光标位于内部 svg 元素上时,d3-zoom 中断

转载 作者:行者123 更新时间:2023-11-30 14:10:51 25 4
gpt4 key购买 nike

我已经按照这个简介实现了 d3-zoom tutorial .

我正在使用 https://d3js.org/d3.v5.min.js .这是我使用 d3 的第一个项目。

我的目标是制作一种显示 field 展位的平面图。与教程类似,我从数组中绘制了形状元素。在我的例子中,我将一系列展位信息输入到元素网格中。

缩放功能工作得很好,除了当我的光标在我的一个矩形的边框或填充上,或者在元素的文本上时。如果我的光标点接触到这些元素中的任何一个,缩放行为将停止工作。

尝试在光标处于空白处而不是触摸形状或文本时使用鼠标滚轮进行缩放。

我曾尝试在某处安装 console.log 以查看事件中未通过的内容,但我什至无法找到可以获取事件参数的位置。

非常感谢任何帮助!这是我的代码:

var svg = d3.select("#venue-svg"); // this is my svg element

// the zoom rectangle. from the tutorial: 'The zoom behavior is applied
// to an invisible rect overlaying the SVG element; this ensures that it
// receives input, and that the pointer coordinates are not affected by
// the zoom behavior’s transform.'

svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.style("fill", "none")
.style("pointer-events", "all")
.call(
d3
.zoom()
.scaleExtent([1 / 2, 4])
.on("zoom", zoomed)
);

function zoomed() {
g.attr("transform", d3.event.transform);
}

// a parent <g> that holds everything else and is targeted
// for the transform (from the tutorial).
var g = svg.append("g");

// the groups that hold each booth table, associated org name, etc.
var tables = g
.selectAll("g")
.data(venueBooths)
.enter()
.append("g")
.attr("transform", function(d) {
return "translate(" + d.x + " " + d.y + ")";
});

var tableRects = tables
.append("rect")
.attr("stroke", "steelblue")
.attr("stroke-width", "2px")
.attr("width", function(d) {
return d.w;
})
.attr("height", function(d) {
return d.h;
})
.attr("fill", "none")
.attr("fill", function(d) {
return $.isEmptyObject(d.reservation) ? "none" : "#FF5733";
})
.attr("id", function(d) {
return "table-" + d.id;
});

tables
.append("text")
.text(function(d) {
return "Booth " + d.id;
})
.attr("dx", 5)
.attr("dy", 60)
.attr("font-size", "8px");

tables
.append("text")
.text(function(d) {
return d.reservation.orgName ? d.reservation.orgName : "Available";
})
.attr("dy", 15)
.attr("dx", 5)
.attr("font-size", "9px")
.attr("font-weight", "bold");

最佳答案

尝试在最后创建 rect,使 DOM 看起来像这样:

<svg>
<g></g>
<rect></rect>
</svg>

由于缩放功能附加到大矩形,因此在其上方创建较小的框可以防止缩放事件传播到它们下方的大矩形。它适用于带有 fill: none; 的框,因为它的行为类似于空心框。

尝试将代码修改为:

var svg = d3.select("#venue-svg"); // this is my svg element

// the zoom rectangle. from the tutorial: 'The zoom behavior is applied
// to an invisible rect overlaying the SVG element; this ensures that it
// receives input, and that the pointer coordinates are not affected by
// the zoom behavior’s transform.'

function zoomed() {
g.attr("transform", d3.event.transform);
}

// a parent <g> that holds everything else and is targeted
// for the transform (from the tutorial).
var g = svg.append("g");

// the groups that hold each booth table, associated org name, etc.
var tables = g
.selectAll("g")
.data(venueBooths)
.enter()
.append("g")
.attr("transform", function(d) {
return "translate(" + d.x + " " + d.y + ")";
});

var tableRects = tables
.append("rect")
.attr("stroke", "steelblue")
.attr("stroke-width", "2px")
.attr("width", function(d) {
return d.w;
})
.attr("height", function(d) {
return d.h;
})
.attr("fill", "none")
.attr("fill", function(d) {
return $.isEmptyObject(d.reservation) ? "none" : "#FF5733";
})
.attr("id", function(d) {
return "table-" + d.id;
});

tables
.append("text")
.text(function(d) {
return "Booth " + d.id;
})
.attr("dx", 5)
.attr("dy", 60)
.attr("font-size", "8px");

tables
.append("text")
.text(function(d) {
return d.reservation.orgName ? d.reservation.orgName : "Available";
})
.attr("dy", 15)
.attr("dx", 5)
.attr("font-size", "9px")
.attr("font-weight", "bold");

svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.style("fill", "none")
.style("pointer-events", "all")
.call(
d3
.zoom()
.scaleExtent([1 / 2, 4])
.on("zoom", zoomed)
);

关于javascript - 当光标位于内部 svg 元素上时,d3-zoom 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54498279/

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