Panolens.js Panorama Viewer
Panolens.js is a lightweight, event-driven, and WebGL-based JavaScript library designed for creating interactive 360-degree panorama viewers in web applications. Built on top of Three.JS, it provides robust capabilities for displaying spherical images and videos, including features like infospots, linking between panoramas, and various control mechanisms. The current stable version is 0.12.1, with releases occurring on an irregular basis, often bundling multiple fixes and new features within minor versions, and occasionally introducing significant breaking changes (e.g., the v0.10.0 ES Module refactor). Its key differentiators include its tight integration with Three.JS, emphasis on performance through WebGL, and an event-driven architecture that allows for highly customizable interactions.
Common errors
-
Uncaught ReferenceError: PANOLENS is not defined
cause Attempting to use `PANOLENS` global object without loading `panolens.min.js` via a script tag or incorrectly importing it in an ES module environment.fixIf using script tags, ensure `<script src="panolens.min.js"></script>` is included after `three.min.js`. If using ES modules and a bundler, ensure `import * as PANOLENS from 'panolens';` (or named imports) is used and 'panolens' is installed. -
TypeError: Cannot read properties of undefined (reading 'Viewer')
cause Attempting to access `PANOLENS.Viewer` after v0.10.0 ES module refactor when not using a global script or when `PANOLENS` is not correctly imported as a namespace.fixFor modern module-based projects, use `import { Viewer } from 'panolens';`. If you strictly require a namespace, ensure `import * as PANOLENS from 'panolens';` is used. For script tag usage, verify the `panolens.min.js` script is loaded. -
THREE is not defined
cause Panolens.js depends on Three.JS, but the `THREE` global object is not available, or the `three` module is not correctly imported.fixEnsure `three.min.js` is loaded before `panolens.min.js` in script tag setups. If using modules, explicitly `import * as THREE from 'three';` in your file. -
Uncaught TypeError: panorama.link is not a function
cause The `panorama` object is not an instance of a Panolens panorama class, or the method name is incorrect.fixVerify that `panorama` is correctly instantiated as `new PANOLENS.ImagePanorama()` or `new PANOLENS.VideoPanorama()`. Check method names against current documentation, especially after major updates.
Warnings
- breaking Version 0.10.0 introduced a major refactor to ES6 modules, deprecating CommonJS `require` usage. It also removed several classes (SpriteText, Tile, TileGroup, BendModifier, Bmfont) and changed constant variable naming conventions (e.g., `PANOLENS.Controls` became `PANOLENS.CONTROLS`). Gulp was replaced by Rollup for builds.
- breaking In version 0.8.0, the library reduced its size by replacing data URI images with remote links. This change removed `PANOLENS.DataImage` and requires users who need locally hosted assets to either provide their own image sources or use the `panolens-offline.min.js` version.
- deprecated Version 0.11.0 deprecated the use of `video` elements with a `src` attribute in favor of `source` elements within the `video` tag for better multimedia handling and flexibility.
- gotcha Panolens.js relies on Three.JS. While Panolens.js exposes `PANOLENS.THREE_VERSION` to check compatibility, developers must ensure they are including a compatible version of `three.min.js` before `panolens.min.js` when using script tags, or correctly installing `three` as a peer dependency in module environments.
Install
-
npm install panolens -
yarn add panolens -
pnpm add panolens
Imports
- Viewer
const Viewer = require('panolens').Viewer;import { Viewer } from 'panolens'; // Or if using global script tags: // const viewer = new PANOLENS.Viewer(); - ImagePanorama
import Panolens from 'panolens'; const panorama = new Panolens.ImagePanorama('...');import { ImagePanorama } from 'panolens'; // Or if using global script tags: // const panorama = new PANOLENS.ImagePanorama('path/to/image.jpg'); - VideoPanorama
const panorama = new PANOLENS.VideoPanorama();
import { VideoPanorama } from 'panolens';
Quickstart
import { Viewer, ImagePanorama } from 'panolens';
import * as THREE from 'three';
// Ensure a DOM element exists for the viewer
const appContainer = document.createElement('div');
appContainer.id = 'panorama-container';
appContainer.style.width = '100vw';
appContainer.style.height = '100vh';
document.body.appendChild(appContainer);
// Create a new panorama from an image
const panorama = new ImagePanorama('https://pchen66.github.io/Panolens/examples/asset/textures/equirectangular/cabin.jpg');
// Create the viewer instance, attaching it to the container
const viewer = new Viewer({
container: appContainer,
autoRotate: true,
autoRotateSpeed: 0.5
});
// Add the panorama to the viewer
viewer.add(panorama);
// Example of linking another panorama (dummy link for demonstration)
const otherPanorama = new ImagePanorama('https://pchen66.github.io/Panolens/examples/asset/textures/equirectangular/field.jpg');
viewer.add(otherPanorama);
// Link the first panorama to the second one at a specific 3D coordinate
panorama.link(otherPanorama, new THREE.Vector3(0, 0, -500), 100);