我正在尝试用一些颜色在背景上绘制一些图像,然而 this当我缩小时,方法无法按预期工作(边框上的某些屏幕部分变白)。 那么设置背景颜色的正确方法是什么?
代码如下:
<!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>
关于javascript - d3js 在使用 d3.zoom() 时设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260408/