画布最大化错误?
我肯定已经因为 Firefox、Safari 和 Chrome 中发生的奇怪行为而浪费了一个多小时的时间了。下面是一些代码:
<!doctype html>
<html>
<head>
<title>Watch me grow scrollbars!</title>
</head>
<body onload="resizeCanvas()">
<canvas id="canvas"></canvas>
</body>
</html>
<script type="application/javascript">
function resizeCanvas() {
var canvas = document.getElementById("canvas");
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("width", window.innerWidth);
}
</script>
基本上,画布似乎总是最大化“太多”,并且窗口两侧都会增加滚动条。我尝试过使用各种方法来获取文档尺寸,包括将画布嵌套在 100% 宽和 100% 宽的画布中。 high 绝对定位的 div,以及提取 document.documentElement.clientWidth/Height 属性。
为了确保我不会发疯,我在画布上放置了一个图像,瞧,相同的代码可以完美地将图像拉伸到文档尺寸。
我在这里做错了什么?我太累了,无法理解。必须..喝..东西。
I must've killed over an hour on what seems to me like strange behaviour that happens in Firefox, Safari and Chrome. Here is some code:
<!doctype html>
<html>
<head>
<title>Watch me grow scrollbars!</title>
</head>
<body onload="resizeCanvas()">
<canvas id="canvas"></canvas>
</body>
</html>
<script type="application/javascript">
function resizeCanvas() {
var canvas = document.getElementById("canvas");
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("width", window.innerWidth);
}
</script>
Basically, the canvas always seems to maximize 'too much', and the window grows scrollbars on both sides. I've tried using various ways of obtaining the document dimensions, including nesting canvas in a 100% wide & tall absolutely positioned div, as well as extracting the document.documentElement.clientWidth/Height properties.
Just to be sure I'm not going insane, I've placed an image in place of the canvas, and voila, the same code worked perfectly to stretch the image out to document dimensions.
What am I doing wrong here? I'm too tired to understand. Must.. drink.. something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找一种方法来扩展画布以覆盖整个页面,我发现的最佳解决方案是让 CSS 自动将其大小调整为 100%,然后使用当前画布元素大小来重置画布分辨率。
为了避免出现滚动条,您还必须将画布 CSS 位置设置为绝对位置。
(使用 Chrome 34、Firefox 27、Explorer 11 进行测试)
If you are looking for a way to extend the canvas to cover the whole page, the best solution I found is to let CSS automatically resize it to 100%, then use the current canvas element size to reset the canvas resolution.
In order to avoid having scrollbars you must also set the canvas CSS position to absolute.
(tested with Chrome 34, Firefox 27, Explorer 11)
我还没有找到解决该问题的方法(仍然欢迎反馈),所以现在我在父容器元素上隐藏了溢出,这大致带来了我正在寻找的内容。不过,如果这种不一致的话,那就不好了。除非我错过了什么..
I haven't found a workaround for the issue (still open to feedback) so for now I have hidden overflow on the parent container element which brought about roughly what I was looking for. Still, it can't be good if it's this inconsistent. Unless I'm missing something..
嗯,这在 Firefox 4 中有效,但我没有在其他浏览器中尝试过,因为我的计算机上没有它们。
至于 IE7(无论如何都不支持画布),我必须将脚本的类型属性从
application/javascript
更改为text/javascript
才能使代码正常工作。这就是原因:(在 脚本标记的 type 属性的 javascript MIME 类型是什么?)
我不知道这是否仍然代表更新版本的 IE。
Well, this works in Firefox 4, but I haven't tried in other browsers because I don't have them in this computer.
As for IE7 (which doesn't support canvas anyway), I had to change your script's type attribute from
application/javascript
totext/javascript
to make the code work.This is why: (quoting keparo on What is the javascript MIME type for the type attribute of a script tag?)
I don't know if this still stands for more recent versions of IE.