admin管理员组文章数量:1278822
Is there a way I can make an HTML5 canvas the background of a web page I am making and place all the elements on top of it.
So it acts like the <body>
?
I tried doing this with z-index and positioning the elements on top, but then they were click-able or focus-able. I need them to still be functioning, but the canvas to also be clickable just in the background and not clickable where there are elements over it.
Is there a way I can make an HTML5 canvas the background of a web page I am making and place all the elements on top of it.
So it acts like the <body>
?
I tried doing this with z-index and positioning the elements on top, but then they were click-able or focus-able. I need them to still be functioning, but the canvas to also be clickable just in the background and not clickable where there are elements over it.
Share Improve this question edited Jun 9, 2013 at 5:46 alex 491k204 gold badges889 silver badges991 bronze badges asked Jun 9, 2013 at 5:41 JayGatzJayGatz 2711 gold badge9 silver badges18 bronze badges 1- See also: stackoverflow./questions/3397334/… – Jonathan Commented Mar 29, 2017 at 23:10
2 Answers
Reset to default 9Just set the <canvas>
's z-index
to -1
. If your canvas is covered by containers on top, simulate custom events using createEvent
.[1]
Demo: http://jsfiddle/DerekL/uw5XU/
var canvas = $("canvas"), //jQuery selector, similar to querySelectorAll()
//...
function simulate(e) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mousemove", true, true, window,
0, e.screenX, e.screenY, e.clientX, e.clientY, false, false, false, false, 0, null);
canvas[0].dispatchEvent(evt);
}
$("body > *").each(function () {
this.addEventListener("mousemove", simulate);
});
Sure that would be possible with z-index
, most probably the canvas will not be clickable because you're working with a container that's on top of it. So even if there appears to be no element the container is still there which prevents you from clicking on the canvas.
本文标签: javascriptMake ltcanvasgt the background of an html documentStack Overflow
版权声明:本文标题:javascript - Make <canvas> the background of an html document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222078a2361154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论