当前位置:首页 > VUE

vue实现全景图

2026-01-23 02:54:24VUE

Vue 实现全景图的方法

使用 Three.js 和 Vue 结合

安装依赖库:

npm install three

在 Vue 组件中引入 Three.js 并创建全景图场景:

<template>
  <div ref="container" class="panorama-container"></div>
</template>

<script>
import * as THREE from 'three';

export default {
  mounted() {
    this.initPanorama();
  },
  methods: {
    initPanorama() {
      const container = this.$refs.container;
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(container.clientWidth, container.clientHeight);
      container.appendChild(renderer.domElement);

      const geometry = new THREE.SphereGeometry(500, 60, 40);
      geometry.scale(-1, 1, 1);
      const texture = new THREE.TextureLoader().load('path/to/panorama.jpg');
      const material = new THREE.MeshBasicMaterial({ map: texture });
      const sphere = new THREE.Mesh(geometry, material);
      scene.add(sphere);

      camera.position.set(0, 0, 0.1);

      const animate = () => {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
      };
      animate();

      let isDragging = false;
      let previousMousePosition = { x: 0, y: 0 };
      container.addEventListener('mousedown', () => { isDragging = true; });
      container.addEventListener('mouseup', () => { isDragging = false; });
      container.addEventListener('mousemove', (event) => {
        if (!isDragging) return;
        const deltaMove = {
          x: event.clientX - previousMousePosition.x,
          y: event.clientY - previousMousePosition.y
        };
        sphere.rotation.y += deltaMove.x * 0.01;
        sphere.rotation.x += deltaMove.y * 0.01;
        previousMousePosition = { x: event.clientX, y: event.clientY };
      });
    }
  }
};
</script>

<style>
.panorama-container {
  width: 100%;
  height: 100vh;
}
</style>

使用现成的 Vue 全景图组件

安装 vue-panorama 组件:

npm install vue-panorama

在 Vue 项目中使用:

<template>
  <vue-panorama :src="panoramaImage" />
</template>

<script>
import VuePanorama from 'vue-panorama';

export default {
  components: {
    VuePanorama
  },
  data() {
    return {
      panoramaImage: require('@/assets/panorama.jpg')
    }
  }
}
</script>

使用 PhotoSphereViewer 库

安装 PhotoSphereViewer:

npm install photo-sphere-viewer

在 Vue 组件中使用:

<template>
  <div ref="viewer" class="panorama-viewer"></div>
</template>

<script>
import { Viewer } from 'photo-sphere-viewer';
import 'photo-sphere-viewer/dist/photo-sphere-viewer.css';

export default {
  mounted() {
    this.initViewer();
  },
  methods: {
    initViewer() {
      const viewer = new Viewer({
        container: this.$refs.viewer,
        panorama: 'path/to/panorama.jpg',
        size: {
          width: '100%',
          height: '100vh'
        }
      });
    }
  }
};
</script>

<style>
.panorama-viewer {
  width: 100%;
  height: 100vh;
}
</style>

注意事项

确保全景图是等距柱状投影格式(equirectangular projection),这是大多数全景图查看器支持的标准格式。

对于移动设备支持,需要添加触摸事件处理,Three.js 示例中可以添加类似以下代码:

container.addEventListener('touchstart', () => { isDragging = true; });
container.addEventListener('touchend', () => { isDragging = false; });
container.addEventListener('touchmove', (event) => {
  if (!isDragging) return;
  const deltaMove = {
    x: event.touches[0].clientX - previousMousePosition.x,
    y: event.touches[0].clientY - previousMousePosition.y
  };
  sphere.rotation.y += deltaMove.x * 0.01;
  sphere.rotation.x += deltaMove.y * 0.01;
  previousMousePosition = { x: event.touches[0].clientX, y: event.touches[0].clientY };
});

考虑添加加载状态和错误处理,提升用户体验:

vue实现全景图

const textureLoader = new THREE.TextureLoader();
textureLoader.load(
  'path/to/panorama.jpg',
  (texture) => {
    const material = new THREE.MeshBasicMaterial({ map: texture });
    const sphere = new THREE.Mesh(geometry, material);
    scene.add(sphere);
  },
  undefined,
  (error) => {
    console.error('Error loading panorama:', error);
  }
);

标签: 全景图vue
分享给朋友:

相关文章

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…