[英]Using window.innerWidth and window.innerHeight makes scrollbars appear
I'm trying to make a simple canvas that stretches itself to fully fill the viewport, but I don't want scrollbars to appear, a thing that happens if I try to resize the canvas in JS using window.innerWidth
and window.innerHeight
.我正在尝试制作一个简单的画布,它可以自行拉伸以完全填充视口,但我不希望出现滚动条,如果我尝试使用
window.innerWidth
和window.innerHeight
在 JS 中调整画布的大小,就会发生这种情况。
Note: I don't want to resize the canvas in CSS because the elements inside get all stretched out since from what I've understood the CSS treats the canvas as an img, and doesn't actually stretch the resolution of the canvas.注意:我不想在 CSS 中调整画布的大小,因为里面的元素都被拉伸了,因为据我所知,CSS 将画布视为 img,并且实际上并没有拉伸画布的分辨率。
This is what happens:这是发生了什么:
This is the code: css:
这是代码:CSS:
html,
body {
height: 100%;
width: 100%;
margin:0;
padding:0;
}
/*canvas*/
#bg {
background: #171629;
}
js: js:
ctx.beginPath()
ctx.canvas.width = window.innerWidth
ctx.canvas.height = window.innerHeight
ctx.rect(20, 40, 50, 50)
ctx.fillStyle = "#e5e5e5"
ctx.fill()
ctx.closePath()
After a bunch of trying I found this as a solution:经过一系列尝试,我发现这是一个解决方案:
css: CSS:
html,
body {
display:flex;
margin:0;
padding:0;
}
js: js:
canvasElement.height = window.innerHeight;
canvasElement.width = document.body.clientWidth;
You can also use:您还可以使用:
css: CSS:
body {
margin: 0;
overflow: hidden;
}
In fact, My problem was when I typed事实上,我的问题是当我输入
<style type="text/csss">
body{
margin:0;
overflow:hidden;
}
canvas{
background-color:lightblue;
}</style>
instead of:代替:
<style type="text/css">
body{
margin:0;
overflow:hidden;
}
canvas{
background-color:lightblue;
} </style>
a tiny grammatical mistake can make you suffer a lot一个微小的语法错误会让你遭受很多痛苦
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.