gpt4 book ai didi

javascript - 为什么我在 Canvas 下看到 div 的背景

转载 作者:行者123 更新时间:2023-11-28 16:56:54 25 4
gpt4 key购买 nike

我正在尝试在 div 内滚动 Canvas 。问题是,当我尝试滚动时,我看到下面 div 的红色背景的红线部分,而不是我希望看到的一切都是黑色的。

HTML:

   <div id="Div1" style=" float: left; width: 150px; height: 200px; background: red; overflow:scroll; ">
<canvas id="myCanvas1" width="200px" height="500px" style="border:0px solid #ff0000;">asdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdf</canvas>
</div>

JavaScript:

 var c = document.getElementById("myCanvas1");
var ctx = c.getContext("2d");
ctx.fillRect(0, 0, 200, 500);
ctx.fillStyle = "#00ff00"; //<======= here
ctx.font = '35px serif';
ctx.fillText('Hello world', 0, 40);

CSS:

canvas { border: 0px solid black; }

这是一个最小的例子: https://jsfiddle.net/alessio211734/vL4whdc0/17/

最佳答案

添加display: block;canvas

var c = document.getElementById("myCanvas1");
var ctx = c.getContext("2d");
ctx.fillRect(0, 0, 200, 500);
ctx.fillStyle = "#00ff00"; //<======= here
ctx.font = '35px serif';
ctx.fillText('Hello world', 0, 40);
canvas {
border: 0px solid black;
display: block;
}
<div id="Div1" style=" float: left; width: 150px; height: 200px; background: red; overflow:scroll; ">
<canvas id="myCanvas1" width="200px" height="500px" style="border:0px solid #ff0000;">asdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdf</canvas>
</div>

关于javascript - 为什么我在 Canvas 下看到 div 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183280/

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