SVG的bBox为零

时间:2017-10-09 14:56:45

标签: d3.js svg

为什么bBox.widthbBox.height的结果都是0?我希望它们都是500px,如何纠正它?

<!DOCTYPE html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<!--script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.9/d3.js" type="text/JavaScript"></script-->
<style>
    rect {
        stroke: #9A8B7A;
        stroke-width: 1px;
        fill: #CF7D1C;

    }

    svg{
        width: 500px;
        height: 500px;
        background:blue;
    }
</style>
<body>


</body>
<script>

    var svg = d3.select("body").append("svg");
    var bBox = svg.node().getBBox();
    console.log(bBox.width + 'x' + bBox.height);






</script>

1 个答案:

答案 0 :(得分:1)

尝试使用getBoundingClientRect代替getBBox