我正在尝试向圆中添加拖动并尝试应用translateExtent。那么如何将拖动边界限制为矩形呢??
var height = 500;
var width = 500;
//if extent is specified, sets the translate extent to the specified array of points [[x0, y0], [x1, y1]], where [x0, y0] is the top-left corner of the world and [x1, y1] is the bottom-right corner of the world, and returns this zoom behavior.
var zoom = d3.zoom()
.translateExtent([[100, 100], [400, 400]])
.on("zoom", zoomed);
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body")
.append("svg")
.attr("width", height)
.attr("height", width)
.append("g")
svg.append("rect")
.attr("x", 100)
.attr("y", 100)
.attr("height", 300)
.attr("width", 300);
var circle = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20)
.style("fill", "red")
svg.call(zoom);
function zoomed() {
circle.attr("transform", d3.event.transform);
}
关于https://github.com/d3/d3-zoom#zoom_translateExtent的工作方式的详细说明吗?如何从坐标计算边界。
答案 0 :(得分:2)
这里有一些注意事项,并且鉴于我过去肯定会被他们绊倒,我希望我可以在这里清楚地解释它们。
缩放范围
让我们看一下缩放范围(zoom.extent
)-不翻译范围。默认范围是“ [[0, 0], [width, height]]
,其中width是元素的客户端宽度,height是其客户端高度”(d3-zoom docs)。由于您是在svg
上调用缩放功能,因此默认范围应为[0,0],[width,height]
,这里的宽度和高度分别为500。
您的翻译范围[100,100],[400,400]
比缩放范围小 ,这对麦克·波斯托克(Mike Bostock)来说是行不通的:“问题是您的translationExtent指定的尺寸小于缩放范围。因此无法满足要求的约束。” (d3-zoom issue tracker)。
TranslateExtent
然后的问题是,您使用的翻译范围不正确。您指定的平移范围是希望圆约束的范围。但这不等于平移范围,平移范围是在给定缩放范围的情况下要显示的坐标空间的边界(圆所在的世界的边界)。
让我们考虑在[100,100]
处的圆,它在translate(0,0)
处的缩放转换中居中:它位于起始位置。这标记了您希望将圆限制在其中的边界框的左上位置。此时缩放的左上坐标为[0,0]
。缩放范围或视口的右下角是[500,500]
。
如果圆位于其预定移动的右下角[400,400]
处,则它的变换为translate(300,300)
,因为它是向右300像素,从开始位置向下300像素(原始位置与cx / cy)。假设一切都向右下移动300像素,则视口或缩放范围的左上角现在为[-300,-300]
(对于cx,cy为-300的圆,其中心位于SVG的左上角,因为缩放变换)。右下角是[200,200]
。
首先,当圆不能进一步向上或向左移动时,我们显示的范围为[0,0],[500,500]
;当圆位于右下角时,当圆圈不能进一步向下或向右移动时,我们有[-300,-300],[200,200]
的显示范围。
以极端表示,那么我们想要的最大范围是:[-300,-300],[500,500]
,这是我们想要显示的世界范围,以便圆与矩形重叠。
var height = 500;
var width = 500;
var zoom = d3.zoom()
.translateExtent([[-300, -300], [500, 500]])
.on("zoom", zoomed);
var svg = d3.select("body")
.append("svg")
.attr("width", height)
.attr("height", width)
.append("g")
svg.append("rect")
.attr("x", 100)
.attr("y", 100)
.attr("height", 300)
.attr("width", 300);
var circle = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20)
.style("fill", "red")
svg.call(zoom);
function zoomed() {
circle.attr("transform", d3.event.transform);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
可能的优化
如果我们使用的缩放范围的宽度和高度等于矩形的宽度和高度:
.extent([[0,0],[300,300]])
我们不必扩展translationExtent即可解决仍在SVG内的矩形周围的空白空间:
.translateExtent([[-300,-300],[300,300]])
var height = 500;
var width = 500;
//if extent is specified, sets the translate extent to the specified array of points [[x0, y0], [x1, y1]], where [x0, y0] is the top-left corner of the world and [x1, y1] is the bottom-right corner of the world, and returns this zoom behavior.
var zoom = d3.zoom()
.translateExtent([[-300,-300],[300,300]])
.extent([[0,0],[300,300]])
.on("zoom", zoomed);
console.log(zoom.extent());
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body")
.append("svg")
.attr("width", height)
.attr("height", width);
svg.append("rect")
.attr("x", 100)
.attr("y", 100)
.attr("height", 300)
.attr("width", 300);
var circle = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20)
.style("fill", "red")
svg.call(zoom);
function zoomed() {
circle.attr("transform", d3.event.transform);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>