Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 410

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:37:07+00:00 2024-11-25T10:37:07+00:00

360 landing page using three.js and nuxt.js

  • 62k

In this post, I like to share how to create basic 360 landing page using three.js and nuxt.js.

Step 1: install new nuxt.js project

npx nuxi@latest init simple-app cd simple-app npm i 
Enter fullscreen mode Exit fullscreen mode

Step 2: install three.js

npm i three -D 
Enter fullscreen mode Exit fullscreen mode

now we are ready to go let’s create new component for our 360 landing page

Step 3: create new component

npx nuxi add component Landing 
Enter fullscreen mode Exit fullscreen mode

now we will create the component content like this

<script setup lang="ts"> import { PerspectiveCamera, Scene, SphereGeometry, TextureLoader, MeshBasicMaterial, Mesh, WebGLRenderer, MathUtils, } from "three";  const container = ref()  const props = defineProps({     panoramaUrl:{         type: String,         default: "img/hdri.webp"     }, });  let camera, scene, renderer; let isUserInteracting = false,     onPointerDownMouseX = 0, onPointerDownMouseY = 0,     lon = 0, onPointerDownLon = 0,     lat = 0, onPointerDownLat = 0,     phi = 0, theta = 0;  camera = new PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1100); scene = new Scene(); const geometry = new SphereGeometry(1000, 100, 100); const reader = new FileReader();  const onWindowResize= () => {     camera.aspect = window.innerWidth / window.innerHeight;     camera.updateProjectionMatrix();     renderer.setSize(window.innerWidth, window.innerHeight); }  const onPointerDown = (event) => {     if (event.isPrimary === false) return;     isUserInteracting = true;     onPointerDownMouseX = event.clientX;     onPointerDownMouseY = event.clientY;     onPointerDownLon = lon;     onPointerDownLat = lat;     document.addEventListener('pointermove', onPointerMove);     document.addEventListener('pointerup', onPointerUp); }  const onPointerMove = (event) => {     if (event.isPrimary === false) return;     lon = (onPointerDownMouseX - event.clientX) * 0.1 + onPointerDownLon;     lat = (event.clientY - onPointerDownMouseY) * 0.1 + onPointerDownLat; }  const onPointerUp = () => {     if (event.isPrimary === false) return;     isUserInteracting = false;     document.removeEventListener('pointermove', onPointerMove);     document.removeEventListener('pointerup', onPointerUp); }  const onDocumentMouseWheel = (event) => {     const fov = camera.fov + event.deltaY * 0.05;     camera.fov = MathUtils.clamp(fov, 10, 75);     camera.updateProjectionMatrix(); }  const animate = () => {     requestAnimationFrame(animate);     update(); }  const update = () => {     if (isUserInteracting === false) {         lon += 0.1;     }      lat = Math.max(- 85, Math.min(85, lat));     phi = MathUtils.degToRad(90 - lat);     theta = MathUtils.degToRad(lon);     const x = 500 * Math.sin(phi) * Math.cos(theta);     const y = 500 * Math.cos(phi);     const z = 500 * Math.sin(phi) * Math.sin(theta);     camera.lookAt(x, y, z);     renderer.render(scene, camera); }  const dragover = (event) => {     event.preventDefault();     event.dataTransfer.dropEffect = 'copy'; }  const dragenter = () => {     document.body.style.opacity = 0.5; }  const dragleave = () => {     document.body.style.opacity = 1; }  const load1 = (event) => {     material.map.image.src = event.target.result;     material.map.needsUpdate = true; }  const drop = () => {     event.preventDefault();     reader.addEventListener('load', load1);     reader.readAsDataURL(event.dataTransfer.files[0]);     document.body.style.opacity = 1; }  onMounted(() => {         init();         animate();          function init() {             // invert the geometry on the x-axis so that all of the faces point inward             geometry.scale(- 1, 1, 1);             const texture = new TextureLoader().load(props.panoramaUrl);             const material = new MeshBasicMaterial({ map: texture });             const mesh = new Mesh(geometry, material);             scene.add(mesh);             renderer = new WebGLRenderer();             renderer.setPixelRatio(window.devicePixelRatio);             renderer.setSize(window.innerWidth, window.innerHeight);             container.value.appendChild(renderer.domElement);             container.value.style.touchAction = 'none';             container.value.addEventListener('pointerdown', onPointerDown);             document.addEventListener('wheel', onDocumentMouseWheel);             document.addEventListener('dragover', dragover);             document.addEventListener('dragenter', dragenter);             document.addEventListener('dragleave', dragleave);             document.addEventListener('drop', drop);             window.addEventListener('resize', onWindowResize);         } });  onUnmounted(() => {     reader.removeEventListener('load', load1);     container.value.replaceWith(container.value.cloneNode(true));     document.removeEventListener('wheel', onDocumentMouseWheel);     document.removeEventListener('dragover', dragover);     document.removeEventListener('dragenter', dragenter);     document.removeEventListener('dragleave', dragleave);     document.removeEventListener('drop', drop);     window.removeEventListener('resize', onWindowResize); }); </script>  <template>     <div class="max-w-full h-screen w-full overflow-hidden">         <div ref="container"></div>     </div> </template> 
Enter fullscreen mode Exit fullscreen mode

Note that: you can find info about each part of the code in the official documentation of three.js in the link below.

three.js documentation

now we need to create folder inside the public with the name img then we will create our image that use hdri mode.
You can create your own image or you can find one from any resource in this part i basically use greater website called HDRi Haven then i convert the image to webp just to minify the total size of the image also in my case i don’t care about the shadow reflex and that other powerful things.
Now we have the image img/hdri.webp in our public folder we are ready to use our component in the index.vue page.

Note that: we may have some rendering problem if we use SSR with our app so basically we will use wen we call our component like bellow.

<ClientOnly>     <Landing/>     <template #fallback>       <!-- this will be rendered on server side -->       <p>Landing is loading...</p>     </template>   </ClientOnly> 
Enter fullscreen mode Exit fullscreen mode

That’s it, we have now a basic 360 landing page

my showcase in api.daily.dev

nuxtthreejswebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.