3d生成的SVG没有响应

d3-generated SVG is not responsive

本文关键字:响应 SVG 3d      更新时间:2023-09-26

我正在尝试创建一个响应式SVG。我已经成功地创建了一个示例SVG:

<html xmlns:xlink="http://www.w3.org/1999/xlink"><head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  </head>
  <body>
    <div id="container">
        <svg viewBox="0 0 600 200" preserveAspectRatio="xMidYMid" id="chart">
            <circle fill="red" cy="100" cx="100" r="100"></circle>
            <circle fill="blue" cy="100" cx="300" r="100"></circle>
            <circle fill="green" cy="100" cx="500" r="100"></circle>
        </svg>
    </div>
</body></html>
https://jsfiddle.net/andrewsu/kombdqL2/4/

https://gist.github.com/andrewsu/d3ed340495a2f21a25f8f69dedb2096a

如果您在jsfiddle版本中调整面板边界,您可以看到圆圈的大小适当缩放。

我想使用D3创建完全相同的SVG。

<html xmlns:xlink="http://www.w3.org/1999/xlink"><head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
  </head>
  <body>
    <div id="container">
    </div>
    <script type="text/javascript">
        var width=600, height=200;
        var svg = d3.select("#container").append("svg")
        .attr("id","chart")
        .attr("preserveAspectRatio", "xMidYMid")
        .attr("viewbox", "0 0 "+ width + " " + height);
        svg.append("circle")
            .attr("r","100")
            .attr("cx","100")
            .attr("cy","100")
            .attr("fill","red");
        svg.append("circle")
            .attr("r","100")
            .attr("cx","300")
            .attr("cy","100")
            .attr("fill","blue");
        svg.append("circle")
            .attr("r","100")
            .attr("cx","500")
            .attr("cy","100")
            .attr("fill","green");
    </script>
</body></html>
https://jsfiddle.net/andrewsu/g1x3s2ny/5/

https://gist.github.com/andrewsu/bf0e7549934f93ac40a416dc17bb7b1e

据我所知,呈现的HTML是完全相同的,但是第二个示例不能正确工作(参见jsfiddle)。

对我来说奇怪的是,如果我使用浏览器的检查器来改变viewBox属性中的四个数字中的任何一个,行为会立即按预期工作。

想法吗?

viewBox有一个大写的B:

.attr("viewBox", "0 0 "+ width + " " + height);

现在检查你的小提琴:https://jsfiddle.net/on17ga9u/

文档如下:https://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute