当前位置:首页 > JavaScript

js实现360全景

2026-01-30 14:01:13JavaScript

使用Three.js实现360全景

Three.js是一个基于WebGL的JavaScript库,可以轻松创建3D场景。以下是一个基本实现:

js实现360全景

// 初始化场景、相机和渲染器
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建球体并添加纹理
const geometry = new THREE.SphereGeometry(500, 60, 40);
geometry.scale(-1, 1, 1); // 反转球体使其纹理在内部可见

const material = new THREE.MeshBasicMaterial({
  map: new THREE.TextureLoader().load('panorama.jpg')
});

const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

// 设置相机位置
camera.position.set(0, 0, 0.1);

// 添加鼠标控制
let isUserInteracting = false,
  lon = 0, lat = 0,
  phi = 0, theta = 0;

document.addEventListener('mousedown', () => {
  isUserInteracting = true;
});

document.addEventListener('mouseup', () => {
  isUserInteracting = false;
});

document.addEventListener('mousemove', (e) => {
  if (isUserInteracting) {
    lon = (e.clientX - window.innerWidth / 2) * 0.1;
    lat = (e.clientY - window.innerHeight / 2) * 0.1;
  }
});

// 动画循环
function animate() {
  requestAnimationFrame(animate);

  if (!isUserInteracting) {
    lon += 0.1;
  }

  lat = Math.max(-85, Math.min(85, lat));
  phi = THREE.MathUtils.degToRad(90 - lat);
  theta = THREE.MathUtils.degToRad(lon);

  camera.position.x = 100 * Math.sin(phi) * Math.cos(theta);
  camera.position.y = 100 * Math.cos(phi);
  camera.position.z = 100 * Math.sin(phi) * Math.sin(theta);

  camera.lookAt(scene.position);
  renderer.render(scene, camera);
}

animate();

使用CSS3实现简单360全景

对于性能要求不高的场景,可以使用CSS3变换:

js实现360全景

<div class="panorama-container">
  <div class="panorama-image"></div>
</div>

<style>
.panorama-container {
  width: 100%;
  height: 100vh;
  perspective: 1000px;
  overflow: hidden;
}

.panorama-image {
  width: 200%;
  height: 100%;
  background-image: url('panorama.jpg');
  background-size: cover;
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  transition: transform 0.5s ease-out;
}
</style>

<script>
const panorama = document.querySelector('.panorama-image');
let rotation = 0;

document.addEventListener('mousemove', (e) => {
  const x = e.clientX / window.innerWidth;
  rotation = x * 180;
  panorama.style.transform = `rotateY(${rotation}deg)`;
});
</script>

使用PhotoSphereViewer库

PhotoSphereViewer是一个专门用于展示360度全景图像的库:

// 引入库
import { Viewer } from 'photo-sphere-viewer';

// 初始化查看器
const viewer = new Viewer({
  container: document.getElementById('viewer'),
  panorama: 'panorama.jpg',
  size: {
    width: '100%',
    height: '100vh'
  },
  navbar: [
    'autorotate',
    'zoom',
    'fullscreen'
  ],
  defaultZoomLvl: 50,
  autorotateDelay: 3000,
  autorotateSpeed: '0.5rpm'
});

全景图像准备注意事项

全景图像应为等距柱状投影格式(equirectangular projection) 推荐分辨率为2:1比例(如6000×3000像素) 确保图像无缝连接,避免明显接缝 可使用Photoshop或PTGui等软件处理原始图像

性能优化建议

压缩全景图像以减少加载时间 考虑使用多分辨率切片技术 移动设备上考虑降低渲染质量 添加加载进度指示器 实现自适应分辨率以匹配设备性能

标签: 全景js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

vue实现js休眠

vue实现js休眠

实现 JavaScript 休眠的方法 在 Vue 中实现 JavaScript 休眠(延迟执行)可以通过以下方式实现。由于 JavaScript 本身没有内置的 sleep 函数,通常使用 Prom…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…