为什么将HTML5画布放在后台不起作用
Why putting HTML5 canvas into the background doesn't work?
我正在尝试将HTML5画布作为我的网页的背景。无论我做什么,我都无法让它工作。有谁知道怎么做?这是我目前的尝试:
<!DOCTYPE html>
<html>
<head> <title> Test </title> </head>
<style>
#bgcanvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index = -1;
}
</style>
<script type="text/javascript">
function fillCanvas(){
var bg = document.getElementById("bgcanvas");
var ctx = bg.getContext("2d");
ctx.fillStyle = "#00FF00";
ctx.fillRect(0, 0, 50, 50);
ctx = document.getElementById("screen").getContext("2d");
ctx.fillStyle = "#0000FF";
ctx.fillRect(0, 0, 500, 400);
}
</script>
<body onload="fillCanvas();">
<center>
<h1>Test Page</h1>
<canvas id="screen" width=720 height=480 style="background: black;">
Your browser sucks. Please upgrade.
</canvas>
</center>
</body>
<canvas id="bgcanvas" width=100 height=100> </canvas>
</html>
谢谢!
这里有一些简单的错误,您可以调整以使其工作,如各种注释中所述:
#bgcanvas {
/* ... rest not shown ... */
/* remove these unless you want the content to scale like an image:
width: 100%;
height: 100%;
*/
z-index: -1; /* change = to :. = is not supported in CSS for properties */
}
将画布元素移动到正文标签内:
...
<canvas id="bgcanvas" width=100 height=100> </canvas>
</body>
提示:增加大小以获得更好的质量(您当前将 100x100 像素的"图像"缩放到窗口大小左右,这可能会产生较差的结果)。
您也可以删除 CSS 大小,并在脚本中以这种方式设置画布大小,然后再为其绘制任何内容:
var bg = document.getElementById("bgcanvas");
bg.width = window.innerWidth;
bg.height = window.innerHeight;
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- 为什么chrome.sockets.tcp.create()在应用程序后台脚本中不起作用
- Jquery 在后台位置上动画不起作用
- SoundCloud widget .play() 方法在选项卡在后台时不起作用 (Chrome)
- Jquery - css 后台修改不起作用
- Chrome:同时使用内容和后台脚本不起作用
- 为什么将HTML5画布放在后台不起作用
- 我想先使用jquery加载后台,但我的代码没有;似乎不起作用
- 在实际的项目环境(Django)中,鼠标在后台移动的示例不起作用