d3js 在使用 d3.zoom() 时设置背景颜色



我正在尝试用一些颜色在背景上绘制一些图像,当我缩小时,这种方法无法按预期工作(边框上的屏幕某些部分变为白色(。那么设置背景颜色的正确方法是什么?

这是代码:

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>
<body>
  <script>
    var svg = d3.select("body")
      .append("svg")
      .attr("width", "100%")
      .attr("height", "100%")
      .call(d3.zoom().on("zoom", function () {
              svg.attr("transform", d3.event.transform)
      }))
      .append("g")
    svg.append("rect")
        .attr("width", "100%")
        .attr("height", "100%")
        .attr("fill", "pink");
    svg.append("image")
        .attr("xlink:href", "./image/" + "item_1" + ".png")
        .attr("x", document.body.clientWidth / 2)
        .attr("y", document.body.clientHeight / 2)
        .attr("width", 32)
        .attr("height", 32)
    svg.append("image")
        .attr("xlink:href", "./image/" + "item_2" + ".png")
        .attr("x", 3 * document.body.clientWidth / 4)
        .attr("y", 3 * document.body.clientHeight / 4)
        .attr("width", 32)
        .attr("height", 32)
  </script>
</body>

更新:看起来添加background-color解决了问题:

...
  </style>
</head>
<style>
   body{
    background-color: #E59400;
   } 
</style>
<body>
  <script>
...
<!DOCTYPE html>
   <head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>
<body>
  <script>
var svg = d3.select("body")
  .append("svg")
  .attr("width", "100%")
  .attr("height", "100%").style("background-color", "pink")
  .call(d3.zoom().on("zoom", function () {
          svg.attr("transform", d3.event.transform)
  }))
  .append("g")
svg.append("rect")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("fill", "pink")
    ;
svg.append("image")
    .attr("xlink:href", "./image/" + "item_1" + ".png")
    .attr("x", document.body.clientWidth / 2)
    .attr("y", document.body.clientHeight / 2)
    .attr("width", 32)
    .attr("height", 32)
svg.append("image")
    .attr("xlink:href", "./image/" + "item_2" + ".png")
    .attr("x", 3 * document.body.clientWidth / 4)
    .attr("y", 3 * document.body.clientHeight / 4)
    .attr("width", 32)
    .attr("height", 32)
  </script>
</body>

最新更新