gpt4 book ai didi

javascript - 是否可以将一 block Canvas 放在另一 block Canvas 旁边,而底部没有空间?

转载 作者:行者123 更新时间:2023-12-01 00:40:15 26 4
gpt4 key购买 nike

我希望有两个 Canvas ,用于从左到右的两个图。看来第二个 Canvas 会自动创建在第一个 Canvas 下方,并且指定位置后,它们将在 Canvas 下方有一个很大的空间。

由于 Canvas 是在我的项目中的循环内创建的,因此网络上有太多空白,我希望用 javascript 来消除它们。

以下是我所拥有的。

<script type="text/javascript">             
var canv = document.createElement("canvas");
canv.setAttribute('width', 200);
canv.setAttribute('height', 200);
canv.setAttribute('id', 'canv'+{{res.2}});
document.body.appendChild(canv);
var C = document.getElementById(canv.getAttribute('id'));
if (C.getContext) {
if (C.getContext) {
new Chart(document.getElementById(canv.getAttribute('id')), {
type: 'line',
data: {
labels: ["a1","a2","a3"],
datasets: [
{
label: ["AAA"],
backgroundColor: "rgba(62, 149, 205, 0.5)",
borderColor : "rgba(62, 149, 205, 1)",
pointBackgroundColor: "rgba(62, 149, 205, 1)",
data: [{{res.3}},{{res.4}},{{res.5}}] //numeric value
},
]
},

options: {
responsive: false,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
}

var canv2 = document.createElement("canvas");
canv2.setAttribute('width', 200);
canv2.setAttribute('height', 200);
canv2.setAttribute('id', 'canv2_'+{{res.2}});
document.body.appendChild(canv2);
var C2 = document.getElementById(canv2.getAttribute('id'));
C2.style.left='200px';
C2.style.top='-200px';
C2.style.position="relative";
if (C2.getContext) {
if (C2.getContext) {
new Chart(document.getElementById(canv2.getAttribute('id')), {
type: 'line',
data: {
labels: ["b1","b2","b3"],
datasets: [
{
label: ["BBB"],
backgroundColor: "rgba(62, 149, 205, 0.5)",
borderColor : "rgba(62, 149, 205, 1)",
pointBackgroundColor: "rgba(62, 149, 205, 1)",
data: [{{res.12}},{{res.13}},{{res.14}}] //numeric value
},
]
},

options: {
responsive: false,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
}

</script>

上面在创建的每个 canvcanv2 下面留下了高度为 200 的空间。

我怎样才能让它们消失?提前致谢。

最佳答案

像下面这样吗?如果您需要在页面上进行更具体的放置,可以将它们包装在一个可以更轻松移动的容器中。

编辑:更新为包括居中 CSS。片段预览可能有点紧张。

<html>
<head>
<style>
html, body{
width:100%;
height:100%;
margin:0px;
border:0px;
padding:0px;
box-sizing:border-box;
}
#canvas_container{
position: absolute;
left:50%; /* put 50% of containing div's width to my left */
transform: translate(-50%); /* now move me back half of my own width */
}
</style>
</head>
<body>
<div id="canvas_container">
</div>
<script type="text/javascript">
function createCanvas() {
let canv = document.createElement('canvas')
canv.height = 150;
canv.width = 150;
canv.style.backgroundColor = 'red';
canv.style.border = '1px solid black'
canv.style.position = 'relative';
canv.style.float = 'left';
document.getElementById('canvas_container').appendChild(canv);
}
createCanvas();
createCanvas();
</script>
</body>
</html>

关于javascript - 是否可以将一 block Canvas 放在另一 block Canvas 旁边,而底部没有空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com