当前位置:首页 > JavaScript

js全景实现

2026-02-01 15:13:16JavaScript

使用Three.js实现全景效果

Three.js是一个强大的3D库,可以轻松实现全景效果。需要准备一张等距柱状投影的全景图(equirectangular)。

import * as THREE from 'three';

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 texture = new THREE.TextureLoader().load('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);

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

使用PhotoSphereViewer库

PhotoSphereViewer是专门用于全景展示的库,简单易用。

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'
    ]
});

使用A-Frame实现WebVR全景

A-Frame是构建VR体验的Web框架,可以轻松创建全景场景。

<a-scene>
    <a-sky src="panorama.jpg" rotation="0 -90 0"></a-sky>
    <a-camera>
        <a-cursor></a-cursor>
    </a-camera>
</a-scene>

移动端手势控制实现

为全景添加移动端手势控制,增强用户体验。

let isUserInteracting = false;
let lon = 0, lat = 0;
let phi = 0, theta = 0;

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

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

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

function updateCamera() {
    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(0, 0, 0);
}

性能优化技巧

使用低分辨率纹理预加载,然后切换高分辨率纹理。

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

const highResTexture = new THREE.TextureLoader().load('panorama-high.jpg', () => {
    material.map = highResTexture;
    material.needsUpdate = true;
});

添加热点交互

在全景中添加可交互的热点元素。

const hotspotGeometry = new THREE.SphereGeometry(1, 16, 16);
const hotspotMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const hotspot = new THREE.Mesh(hotspotGeometry, hotspotMaterial);
hotspot.position.set(10, 5, -8);
scene.add(hotspot);

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

function onMouseClick(event) {
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);
    const intersects = raycaster.intersectObjects([hotspot]);

    if (intersects.length > 0) {
        console.log('Hotspot clicked!');
    }
}

js全景实现

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

相关文章

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js实现换肤

js实现换肤

使用CSS变量实现换肤 通过CSS变量可以轻松实现主题切换功能。CSS变量在根元素中定义,通过JavaScript动态修改这些变量值。 :root { --primary-color: #34…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现二叉树

js实现二叉树

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