【问题标题】:How "d3.translateExtent" coordinate works?“d3.translateExtent”坐标如何工作?
【发布时间】:2019-05-15 05:04:11
【问题描述】:

jsfiddle DEMO

我正在尝试向圆圈添加拖动并尝试应用 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 工作原理的详细说明吗?如何根据坐标计算边界。

【问题讨论】:

    标签: javascript d3.js zooming


    【解决方案1】:

    这里有一些注意事项,鉴于我过去肯定被它们绊倒,我希望我能在这里清楚地解释它们。

    缩放范围

    让我们看看缩放范围 (zoom.extent) - 不是平移范围。默认范围是“[[0, 0], [width, height]],其中宽度是元素的客户端宽度,高度是其客户端高度”(d3-zoom docs)。由于您在 svg 上调用缩放,因此默认范围应为 [0,0],[width,height],在您的情况下,宽度和高度各为 500。

    您的翻译范围,[100,100],[400,400] 比您的缩放范围,这不起作用,来自 Mike Bostock 的类似问题:“问题是您指定的 translateExtent 是小于缩放范围。因此无法满足请求的约束。 (d3-zoom issue tracker)。

    翻译范围

    那么问题是您错误地使用了翻译范围。您指定的平移范围是您希望将圆约束到的范围。但这不等于平移范围,它是在给定缩放范围的情况下要显示的坐标空间的边界(圆所在的世界的边界)。

    让我们考虑[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]])
    

    我们不必扩展 translateExtent 来解决仍在 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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2020-08-17
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多