vue 项目中使用photo-sphere-viewer实现vr720度全景图片预览

最近项目中要求vr功能,可以进行图片的全景查看,在此,用到了vue里的一个实现全景预览的插件:photo-sphere-viewer。移动端和PC端都可以使用:下面给大家介绍一下使用步骤及方法。
1、安装 photo-sphere-viewer依赖

 npm install  photo-sphere-viewer --save-dev

2、引入:在你需要使用的页面引入插件

  import {Viewer} from 'photo-sphere-viewer'
  import MarkersPlugin from 'photo-sphere-viewer/dist/plugins/markers'
  import 'photo-sphere-viewer/dist/photo-sphere-viewer.css'
  import 'photo-sphere-viewer/dist/plugins/markers.css';

3、接下来就可以正常使用

<div id="viewer"></div>
  import {getBizSectionVrList} from "../../../api/api";//调用接口
  import {Toast} from "vant";//消息提示
  import {baseUrlMobile} from '../../../api/http'//调用后台接口
  export default {
    data(){
      return{
        profileName:'',
        profileId:'',
        viewer:'',
        imageList:[]


      }
    },
    mounted(){
      this.getProfileVRInfoList()
      // this.initPhotoSphere(this.imageList)

    },
    beforeMount() {
      this.profileName=this.$route.query.profileName
      this.profileId=this.$route.query.profileId
    },
    methods:{
      initPhotoSphere(imageList){
        let _this=this
        let i=0
        _this.viewer = new Viewer({
          container:document.querySelector('#viewer'),
          panorama:baseUrlMobile+imageList[i].filePath,
          caption: _this.profileName,
          navbar:true,
          size: {
            width: '100%',
            height: _this.isMobile()? screen.availHeight : '100%'
          },
          navbar: [
            'autorotate',
            'zoom',
            'markers',
            'caption',
            'fullscreen'
          ],
          plugins: [
            [MarkersPlugin, {
              markers: [
                {
                  id:'circle',
                  image:require('../../../../static/icon/location.png'),
                  width:42,
                  height:42,
                  longitude: 20,
                  latitude:-0.15,
                  anchor:'bottom center',
                  className:'markers',
                  tooltip:{
                    content : '欢迎进入下一场景',
                    position: 'bottom left'
                  },
                  visible:true
                }
              ],
            }],
          ],
        });
        const markersPlugin = _this.viewer.getPlugin(MarkersPlugin);

        markersPlugin.on('select-marker', (e, marker) => {
          this.viewer.animate({
            longitude: marker.config.longitude,
            latitude: marker.config.latitude,
            zoom: 100,
            speed: '-2rpm',
          }).then(() =>{
            i=i+1 ===baseUrlMobile+imageList.length?0:i+1;
            this.viewer.setPanorama(
              baseUrlMobile+imageList[i].filePath
            ).then(() =>{
              markersPlugin.updateMarker({
                id: marker.id,
                longitude: -1.8,
                latitude: -0.28,
              }),
                this.viewer.animate({
                  zoom: 50,
                  speed: '2rpm',
                }).then(() =>{
                  imageList[i].filePath = baseUrlMobile+imageList[i].filePath;
                })
            })
          })
        });
      },

      isMobile() {

        let ua = navigator.userAgent;

        let ipad = ua.match(/(iPad).*OS\s([\d_]+)/),

          isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
          isAndroid = ua.match(/(Android)\s+([\d.]+)/),
          isMobile = isIphone || isAndroid;
        return isMobile;

      },
      onClickLeft(){
        this.$router.push({path: '/Navigation',
          query:{
            profileId:this.profileId,
            profileName:this.profileName
          }})
      },
      getProfileVRInfoList(){
        getBizSectionVrList(this.profileId).then((res) => {
          if (res.code == 200) {
                    this.imageList = res.rows;
                    if (res.rows.length == 0) {
                        this.isData = false;
                    } else {
                        this.isData = true;
                        this.$nextTick(() => {
                            this.initPhotoSphere(this.imageList);
                        });
                    }
                } else {
                    Toast.fail(res.msg);
                }
        });
      }
    },

  }
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 `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` 库本身。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间的情敌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值