Vue实现3D全景图,photo-sphere-viewer

文档里面讲的很详细很清楚 ===>>> 传送门

安装依赖

npm install photo-sphere-viewer

新建vue组件

<template>
  <div id="viewer"></div>
</template>

引用

import {Viewer} from 'photo-sphere-viewer' // 引入组件

初始化实例

import MapImage from '../assets/images/a.jpeg' // 引入图片
import TentImage from '../assets/images/tent.png' // 引入图片

export default {
	mounted() {
		this.viewer = new Viewer({
			container:document.querySelector('#viewer'),
			// 全景图片,也支持多种适配器,默认 全景图
			panorama: panorama,
			// 这里定义展示视图的大小,也可以通过CSS在样式中定义
	        size:{
		        width: '90vw',
		        height: '90vh',
	        },
	        touchmoveTwoFingers: true
		})
	}
}


添加标记

  • 在全景图上添加标记进行交互

import { MarkersPlugin } from 'photo-sphere-viewer/dist/plugins/markers';
import 'photo-sphere-viewer/dist/photo-sphere-viewer.css' // 引入样式

this.viewer = new Viewer({
      // (必需的) 将包含全景图或元素标识符的 HTML 元素
      container:document.querySelector('#viewer'),
      // (必需的) 全景路径。必须是默认 equirectangular 适配器的单个 URL。其他适配器支持其他值。
      // photo-sphere-viewer支持多种适配器,这里讲到的是默认的 equirectangular 适配器,使用起来最为简单,但是要求图片必须为全景图
      panorama: PanoramaImage,
      // 这里定义展示视图的大小,也可以通过CSS在样式中定义
      size:{
        width: '90vw',
        height: '90vh',
      },
      touchmoveTwoFingers: true,
      plugins: [
        [MarkersPlugin, {
          markers: [
            {
              id        : '1',
              imageLayer: TentImage,
              width     : 120,
              height    : 94,
              longitude : -0.45,
              latitude  : -0.1,
              tooltip   : 'test-1',
              content   : '<div class="tooltip-content"><h3>test-1</h3><p>描述</p></div>',
            },
            {
              id        : '2',
              imageLayer: TentImage,
              width     : 120,
              height    : 94,
              longitude : 1,
              latitude  : 0,
              tooltip   : 'test-2',
              content: '<div class="tooltip-content"><h3>test-2</h3><p>描述</p></div>',
            },

            {
              // polyline marker
              id: 'polyline',
              polylinePx: [2478, 1635, 2184, 1747, 1674, 1953, 1166, 1852, 709, 1669, 301, 1519, 94, 1399, 34, 1356],
              svgStyle: {
                stroke: 'rgba(140, 190, 10, 0.8)',
                strokeLinecap: 'round',
                strokeLinejoin: 'round',
                strokeWidth: '10px'
              },
              tooltip: 'A dynamic polyline marker',
              content: '<div class="tooltip-content"><h3>旅游路线</h3><p>描述</p></div>'
            },
            {
              // polygon marker
              id: 'polygon',
              polylineRad: [
                [5.998, 0.232],
                [6.582, 0.232],
                [6.582, -0.182],
                [5.998, -0.182],
                [5.998, 0.232],
              ],
              svgStyle: {
                fill: 'rgba(200, 0, 0, 0.2)',
                stroke: 'rgba(200, 0, 50, 0.8)',
                strokeWidth: '4px'
              },
              tooltip: {
                content: '电视机',
                position: 'right bottom'
              },
              content: '<div class="tooltip-content"><h3>电视机</h3><p>描述</p></div>',
            },

          ]
        }]
      ]
    });

给标记添加选择事件

  const markersPlugin = this.viewer.getPlugin(MarkersPlugin);

  markersPlugin.on('select-marker', (e, marker) => {
     console.log("select-marker", marker);
     // markersPlugin.updateMarker({
     //   id: marker.id,
     //   content: '123123123123123123'
     // });
   });
   console.log("this.viewer", this.viewer)

版本

nuxt: ^2.15.8
photo-sphere-viewer: ^4.6.4
vue: ^2.6.14
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 `vue-photo-sphere-viewer` 库来集成 `photo-sphere-viewer` 到 Vue.js 4 中,然后在组件中使用这个库的 API 来实现片切换。具体步骤如下: 1. 安装 `vue-photo-sphere-viewer` 库: ```bash npm install vue-photo-sphere-viewer --save ``` 2. 在组件中引入 `vue-photo-sphere-viewer` 库: ```javascript import PhotoSphereViewer from 'vue-photo-sphere-viewer'; import 'photo-sphere-viewer/dist/photo-sphere-viewer.css'; ``` 3. 在模板中使用 `PhotoSphereViewer` 组件,并绑定片路径和其他属性: ```html <template> <div> <photo-sphere-viewer :panoramaUrl="panoramaUrl" :defaultLongitud="defaultLongitud" :defaultLatitud="defaultLatitud" :defaultZoom="defaultZoom" :size="size" :minZoom="minZoom" :maxZoom="maxZoom" @ready="onReady" ></photo-sphere-viewer> </div> </template> ``` 其中,`panoramaUrl` 是片路径,`defaultLongitud`、`defaultLatitud` 和 `defaultZoom` 是初始位置和缩放级别,`size` 是全景像素大小,`minZoom` 和 `maxZoom` 是缩放级别的最小值和最大值。 4. 在组件的 `methods` 中定义 `onReady` 方法来获取 `PhotoSphereViewer` 实例,并调用 `setPanorama` 方法来切换片: ```javascript methods: { onReady(viewer) { this.viewer = viewer; }, switchImage(imagePath) { this.viewer.setPanorama(imagePath); }, } ``` 然后你可以在组件的其他方法中调用 `switchImage` 方法来切换片。 ```javascript this.switchImage('/path/to/another/image.jpg'); ``` 注意,这里的 `vue-photo-sphere-viewer` 库只是 `photo-sphere-viewer` 的 Vue.js 封装,你还需要在项目中引入 `photo-sphere-viewer` 库本身。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值