简体   繁体   English

将站点更改为连续 animation 和 Three.js

[英]Changing sites as continous animation with Three.js

I was looking at this website called Garden-Eight and they have this very beautiful transition when you change from "Home" to "About Us".我正在查看这个名为Garden-Eight的网站,当您从“家”更改为“关于我们”时,它们会有一个非常漂亮的过渡。 I am really interested in how they made it so smooth and continuous without loading screens or any loading time at all.我真的很感兴趣他们是如何在没有加载屏幕或任何加载时间的情况下让它如此流畅和连续的。

It looks like it has to be one scene, but watching the URL one can watch a change in the address so there must be some kind of change in the site location.看起来它必须是一个场景,但是观看 URL 可以看到地址的变化,因此站点位置必须有某种变化。

I am not necessarily looking at this kind of highly complex transitions.我不一定要看这种高度复杂的过渡。 It would be great if I could create some kind of seemingly continuous camera movement along the x axis that changes as a starting point.如果我可以沿着 x 轴创建某种看似连续的摄像机移动,并作为起点发生变化,那就太好了。

For example, I made this very short animation with this JavaScript:例如,我用这个 JavaScript 制作了这个非常短的 animation:

 var camera, scene, renderer, geometry, material, mesh, geometryNew, materialNew, meshNew; init(); animate(); function init() { scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000); camera.position.z = 500; scene.add(camera); geometry = new THREE.CubeGeometry(200, 200, 200); material = new THREE.MeshNormalMaterial(); mesh = new THREE.Mesh(geometry, material); mesh.position.set(0,0,0); scene.add(mesh); geometryNew = new THREE.CubeGeometry(200, 200, 200); materialNew = new THREE.MeshNormalMaterial(); meshNew = new THREE.Mesh( geometryNew, materialNew); meshNew.position.set(800,0,0); scene.add(meshNew); renderer = new THREE.CanvasRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); } function animate() { requestAnimationFrame(animate); render(); } function render() { mesh.rotation.x += 0.01; mesh.rotation.y += 0.02; meshNew.rotation.x -= 0.01; meshNew.rotation.y -= 0.02; if(camera.position.x < 800) camera.position.x += 3 renderer.render(scene, camera); }

You can watch the animation in this JSFiddle .您可以在此 JSFiddle 中观看animation

What I want to achieve is that the first cube is on the landing page and the second cube is on another page.我想要实现的是第一个立方体在登录页面上,第二个立方体在另一个页面上。 Can this be done by some kind of asynchronous loading or is there a built-in method, that helps to switch from one canvas to the other?这可以通过某种异步加载来完成,还是有一种内置方法可以帮助从一个 canvas 切换到另一个?

This is a single-page-app, (SPA), which means it's all one HTML page, regardless what the URL updates to.这是一个单页应用程序 (SPA),这意味着它都是一个 HTML 页面,不管 URL 更新到什么。 Some frameworks, like React, have a very complex methodology to implement this, and it would take an entire tutorial to teach.一些框架,比如 React,有一个非常复杂的方法来实现这一点,并且需要一个完整的教程来教授。

For simplicity's sake, I recommend you look into the window.onhashchange event to do a fake "change address" without loading a new page.为简单起见,我建议您查看window.onhashchange事件以在不加载新页面的情况下进行虚假的“更改地址”。 For instance, going from site.com/ to site.com/#about would trigger the event, and then you can start your animation on the same canvas:例如,从site.com/site.com/#about将触发该事件,然后您可以在同一 canvas 上启动 animation:

window.onhashchange = function() {
  if (location.hash === '#about') {
    // animate camera to about section
  } else if (location.hash === '#work') {
    // animate camera to work section
  } else if (location.hash === '') {
    // animate camera to home section
  }
}

Then you can set the hash to change the URL and trigger the event above:然后可以设置 hash 更改 URL 并触发上述事件:

// Adds #about hash to URL, and triggers event listener
location.hash = "#about";

// Removes any hash, and triggers event listener
location.hash = "";

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

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