代码之家  ›  专栏  ›  技术社区  ›  PirateApp

为什么这个SVG路径在Firefox上显示的宽度和高度不正确?

  •  0
  • PirateApp  · 技术社区  · 8 年前

    var svg = d3.select("body")
      .append("svg")
      .attr("width", 1800)
      .attr("height", 600)
    
    function drawCandle(x, y, width, upper, body, lower){
      var path = d3.path()
      // path.moveTo(x + width/2, y)
      // path.lineTo(x + width/2, y + upper + body + lower)
      path.moveTo(x, y + upper)
      path.lineTo(x + width, y + upper)
      path.closePath()
      return path
    }
    
    var p = svg.append('path')
    console.log(p.style('stroke-width'))
    
    p.attr('d', drawCandle(200,100,20, 50, 100, 80))
    

    在开发人员工具中检查时生成的路径如下所示

    <svg width="1800" height="600"><path d="M200,150L220,150Z"></path></svg>
    

    但是,如果我在元素上使用检查器悬停,则显示宽度为24 x 4 enter image description here

    我是不是丢了什么东西?这不是20 x 1吗

    path{
      stroke: blue;
      shape-rendering:crispEdges;
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Alex W    8 年前

    看起来这和 stroke-miterlimit 正在用于计算维度。当改变 脑卒中 到 1 它调整高度 4 到 1.45001

    MDN

    var svg = d3.select("body")
      .append("svg")
      .attr("width", 1800)
      .attr("height", 600)
    
    function drawCandle(x, y, width, upper, body, lower){
      var path = d3.path()
      // path.moveTo(x + width/2, y)
      // path.lineTo(x + width/2, y + upper + body + lower)
      path.moveTo(x, y + upper)
      path.lineTo(x + width, y + upper)
      path.closePath()
      return path
    }
    
    var p = svg.append('path')
    console.log(p.style('stroke-width'))
    
    p.attr('d', drawCandle(200,100,20, 50, 100, 80))
    path{
      stroke: blue;
      stroke-width:1px;
      stroke-miterlimit: 1;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.5.0/d3.js"></script>
        2
  •  1
  •   ibrahim tanyalcin    8 年前

    path.moveTo(x, y);
      path.lineTo(x + width, y);
      path.lineTo(x + width, y + upper);
      path.lineTo(x, y + upper);
      path.closePath();
    

    http://jsfiddle.net/ibowankenobi/ozd10hq9/

    另外,由于开发者工具的DOM rect依赖于厂商,Firefox增加了笔划宽度,而chrome则没有。在引擎盖下,客户端rect在这两个方面都是正确的,您可以验证是否这样做:

    console.log(p.node().getBBox());//SVGRect { x: 200, y: 100, width: 20, height: 30 }
    

    http://jsfiddle.net/ibowankenobi/k0t4pfjz/