I've been looking at using three.js for a fun experiment on a site. I would like to use a current experiment (for which I already have the code for) and use it as a background for my site.
Anybody know how to do this?
I saw it done here: http://janjorissen.be/
Three JS API: https://github.com/mrdoob/three.js/wiki/API-Reference
Following the very basic example on threejs.org (here), I only had to change the canvas style section to:
That moved the canvas to the background.
I'm going to add yet another answer. I'd use
Here's why:
Many people use
canvas { width: 100%; height: 100% }
but that arguably doesn't make a lot of sense. You don't want the canvas to be 100% of the body. You want it to 100% of the screen/window. That's whatcanvas { width: 100vw; height: 100vh; }
does. It's 100% of the viewport width and viewport height.This means you don't need to set the body to height: 100% which also would not make sense, especially if the page is taller than the window/screen
display: block;
fixes some issues with scrollbars on certain browsers. Some pages usehtml, body { overflow: none; }
but again that doesn't make sense if your page ends up needing to be taller than the screen/window.position: fixed;
makes the canvas position relative to the top of window so it won't scroll with the page. If you useposition: absolute
then the canvas will scroll off the top if the page is taller than the screen/window. For example this page.top: 0; left 0;
puts it at the top left. Without that it would default to it's default position which is inside the body's margins. Often this is solved by settingbody { margin: 0; }
but generally that means you end up needing some other container to add a margin back in otherwise your normal content gets positioned at the edge of the window.z-index: -9999;
is there to try to force it further back than anything else just in case the page itself is using some negative values forz-index
Here's an example as a snippet
And here's an example outside SO so you can view it easier full size.
iframe
s work as wellNote that there's the issue that if your canvas animation is interactive the elements in front of the canvas will eat the mouse/touch events. There's no easy solution I know of for that. You can mark everything but that canvas/iframe as
pointer-events: none
and mark the canvas/iframe aspointer-events: auto
but then you run into the issue that no text on your page can be selected and no links can be clicked. You could then say set<a>
tags to havepointer-events: auto
so links work but I'm sure there will be issues here and there depending on what info is on your page (trying to copy an email address, or a location address, etc...)One note: most three.js examples are structured different (less flexible) by referencing
window.innerWidth
andwindow.innerHeight
and putting the canvas inside a div with anid="canvas"
for some reason.Here's a snippet using that structure. There's several more lines of code, redundant calls to
renderer.setSize
and setting the camera aspect in 2 places (not very D.R.Y.) but as far as this Q&A is concerned the only difference is#canvas
instead ofcanvas
as the CSS to size the div instead of the canvas.usually i use iframe for that. Thus you dont have conflict with the base page.
an example of it https://github.com/jeromeetienne/www.jetienne.com/blob/master/index-webgl.html#L128 for the source http://jetienne.com/index-webgl.html for the living code
This is not an actual background, but a 100% width/height element that is displaying the animation, with the rest of the content "elevated" using
z-index
or similar above that fake background.