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

在浏览器中缩放svg文档

  •  -1
  • Baz  · 技术社区  · 6 年前

    我想在浏览器中显示一个svg文档,但是问题是客户端没有放大或选择部分图像的功能。因此,我假设我必须将svg嵌入到html页面中,并使用javascript提供此功能。为了方便地提供这样的接口,您建议使用什么库?

    0 回复  |  直到 6 年前
        1
  •  1
  •   ROOT Krunal    6 年前

    D3 推荐使用lib缩放/平移svg,注意这个使用v3和最新版本d3的代码片段是5。

    <!DOCTYPE html>
    <html>
      <head>
        <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
        <style type="text/css">
          body, html {
            width: 100%;
            height: 100%;
            margin: 0;
          }
          svg {
            position: absolute;
            top: 0;
            left: 0;
          }
          p {
            text-align: center;
          }
        </style>
      </head>
      <body>
        <p>Use the mouse to pan (click and move) / zoom (scrollwheel)</p>
      </body>
      <script type="text/javascript">
        var svg = d3.select("body")
          .append("svg")
          .attr("width", "100%")
          .attr("height", "100%")
          .call(d3.behavior.zoom().on("zoom", function () {
            svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
          }))
          .append("g")
    
        svg.append("circle")
          .attr("cx", document.body.clientWidth / 2)
          .attr("cy", document.body.clientHeight / 2)
          .attr("r", 50)
          .style("fill", "#B8DEE6")
      </script>
    </html>