简体   繁体   English

Three.js作为网站的背景可能吗?

[英]Three.js as background of website possible?

I've been looking at using three.js for a fun experiment on a site. 我一直在寻找使用Three.js在网站上进行有趣的实验。 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/ 我在这里看到它完成了: http : //janjorissen.be/

Three JS API: https://github.com/mrdoob/three.js/wiki/API-Reference 三种JS API: https//github.com/mrdoob/three.js/wiki/API-Reference

I'm going to add yet another answer. 我将添加另一个答案。 I'd use 我会用

canvas {
  width: 100vw;
  height: 100vh;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -9999;
}

Here's why: 原因如下:

Many people use canvas { width: 100%; height: 100% } 许多人使用canvas { width: 100%; height: 100% } canvas { width: 100%; height: 100% } but that arguably doesn't make a lot of sense. canvas { width: 100%; height: 100% }但这毫无意义。 You don't want the canvas to be 100% of the body. 您不希望画布占主体的100%。 You want it to 100% of the screen/window. 您希望它到屏幕/窗口的100%。 That's what canvas { width: 100vw; height: 100vh; } 这就是canvas { width: 100vw; height: 100vh; } canvas { width: 100vw; height: 100vh; } canvas { width: 100vw; height: 100vh; } does. canvas { width: 100vw; height: 100vh; } It's 100% of the viewport width and viewport height. 它是视口宽度和视口高度的100%。

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 这意味着您无需将正文设置为高度:100%也没有意义,尤其是当页面比窗口/屏幕高时

display: block; fixes some issues with scrollbars on certain browsers. 修复了某些浏览器上滚动条的一些问题。 Some pages use html, body { overflow: none; } 有些页面使用html, body { overflow: none; } html, body { overflow: none; } but again that doesn't make sense if your page ends up needing to be taller than the screen/window. html, body { overflow: none; }但是如果您的页面最终需要比屏幕/窗口高,那也是没有意义的。

position: fixed; makes the canvas position relative to the top of window so it won't scroll with the page. 使画布相对于窗口顶部的位置,使其不会随页面滚动。 If you use position: absolute then the canvas will scroll off the top if the page is taller than the screen/window. 如果您使用position: absolute那么如果页面比屏幕/窗口高,则画布将从顶部滚动。 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 setting body { margin: 0; } 通常,这可以通过设置body { margin: 0; } body { 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. body { margin: 0; }但通常这意味着您最终需要使用其他容器来添加边距,否则常规内容将位于窗口的边缘。

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 for z-index 是否有尝试将其推回最远的位置,以防万一页面本身对z-index使用了一些负值

Here's an example as a snippet 这是一个示例片段

 var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000); var canvas = document.querySelector("canvas"); var renderer = new THREE.WebGLRenderer({canvas: canvas}); renderer.setClearColor(0xF0F0F0); var geometry = new THREE.BoxGeometry(1, 1, 1); var material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true, }); var cube = new THREE.Mesh(geometry, material); scene.add(cube); camera.position.z = 1; function resize() { var width = canvas.clientWidth; var height = canvas.clientHeight; if (width != canvas.width || height != canvas.height) { renderer.setSize(width, height, false); camera.aspect = width / height; camera.updateProjectionMatrix(); } } function render(time) { time *= 0.001; resize(); cube.rotation.x = time; cube.rotation.y = time * 0.31; renderer.render(scene, camera); requestAnimationFrame(render); } render(); 
 canvas { width: 100vw; height: 100vh; display: block; position: fixed; top: 0; left: 0; z-index: -9999; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r77/three.min.js"></script> <canvas></canvas> <div> some content that is in front of the canvas </div> 

And here's an example outside SO so you can view it easier full size. 这是SO之外的示例,因此您可以更轻松地查看它的完整尺寸。

iframe s work as well iframe的工作也是如此

Note 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 as pointer-events: auto but then you run into the issue that no text on your page can be selected and no links can be clicked. 您可以将除canvas / iframe以外的所有内容都标记为pointer-events: none并且将canvas / iframe标记为pointer-events: auto但随后遇到的问题是无法选择页面上的文本,也无法单击任何链接。 You could then say set <a> tags to have pointer-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...) 然后,您可以说将<a>标记设置为具有pointer-events: auto使链接起作用,但是我确定这里和那里都会有问题,具体取决于页面上的信息(尝试复制电子邮件地址或位置)地址等)。

One note: most three.js examples are structured different (less flexible) by referencing window.innerWidth and window.innerHeight and putting the canvas inside a div with an id="canvas" for some reason. 需要注意的是:通过引用window.innerWidthwindow.innerHeight并出于某种原因将画布放在具有id="canvas"的div内,大多数Three.js示例的结构不同(灵活性较差)。

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 DRY) but as far as this Q&A is concerned the only difference is #canvas instead of canvas as the CSS to size the div instead of the canvas. 还有更多的代码行,对renderer.setSize冗余调用,以及将相机#canvas设置在2个位置(不是很干),但就此问与答而言,唯一的区别是#canvas而不是canvas作为CSS来调整div的大小而不是画布。

 var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); document.getElementById("canvas").appendChild(renderer.domElement); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setClearColor(0xF0F0F0); var geometry = new THREE.BoxGeometry(1, 1, 1); var material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true, }); var cube = new THREE.Mesh(geometry, material); scene.add(cube); camera.position.z = 1; function onResize() { renderer.setSize(window.innerWidth, window.innerHeight); camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); } window.addEventListener('resize', onResize); function render(time) { time *= 0.001; cube.rotation.x = time; cube.rotation.y = time * 0.31; renderer.render(scene, camera); requestAnimationFrame(render); } render(); 
 #canvas { width: 100vw; height: 100vh; display: block; position: fixed; top: 0; left: 0; z-index: -9999; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r77/three.min.js"></script> <div id="canvas"></div> <div> some content that is in front of the canvas </div> 

usually i use iframe for that. 通常,我为此使用iframe。 Thus you dont have conflict with the base page. 因此,您与基本页面没有冲突。

<style>
iframe {
    z-index : -9999;
    position: absolute;
    top : 0;
    left    : 0;
    width   : 100%;
    height  : 100%;
    margin  : 0;
    padding : 0;
}
</style>
<iframe src="http://example.com/"></iframe> 

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 https://github.com/jeromeetienne/www.jetienne.com/blob/master/index-webgl.html#L128的示例,以http://jetienne.com/index-webgl.html为源码

Following the very basic example on threejs.org ( here ), I only had to change the canvas style section to: 遵循threejs.org上非常基本的示例( 此处 ),我只需要将canvas样式部分更改为:

canvas {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -9999;
}

That moved the canvas to the background. 这样就将画布移到了背景上。

这不是实际的背景,而是显示动画的100%宽度/高度元素,其余内容使用z-index或类似元素在该假背景之上“升高”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM