vue使用svg-pan-zoom

该博客介绍了如何在前端项目中使用`svg-pan-zoom`库来实现SVG图像的平移和缩放交互功能。通过引入库、监听加载事件以及创建和销毁embed元素,实现了动态加载SVG并添加控制图标和手势操作。同时,提到了一个备选方案——`html5-svg-viewer`。
摘要由CSDN通过智能技术生成

1、引入

yarn add svg-pan-zoom

2、组件使用

import svgPanZoom from ‘svg-pan-zoom’

<template>
  <div class='svg-model-main'>
    <div style='height: 100%; overflow: hidden; position: relative;font-size: 20px;font-weight: bolder'  ref='svg'>
          <div style='position: absolute;top:50%;left:50%;color: red' v-show='spinning'>加载中...</div>
          <div class='no-data' v-if='!svgId'>
            <div>
              <div class='iconfont icon-zanwushuju' style='font-size: 25px;text-align: center'></div>
              <div style='text-align: center'>未选择图纸</div>
            </div>
          </div>
    </div>
  </div>
</template>

<script>
import svgPanZoom from 'svg-pan-zoom'
import { getAction } from '../../../api/manage'

export default {
  name: 'SvgModel',
  props: {
    docId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      svgId: '',
      spinning: false,
      lastEmbed: ''
    }
  },
  watch: {
    docId: {
      immediate: true,
      async handler(val, oldV) {
        if (val && val != oldV) {
          this.spinning = true
          this.removeEmbed()
          let docRs = await getAction(`/doc/getById/${this.docId}`)
          if (docRs.success) {
            this.svgId = docRs.result.fileId
            this.spinning = false
            this.createNewEmbed(`/dtwins-boot/file/getFileContent/` + this.svgId)
          }
        } else {
          if (!val) {
            this.svgId = ''
          }
        }
      }
    }
  },
  methods: {
    lastEventListener() {
      svgPanZoom(this.lastEmbed, {
        zoomEnabled: true,
        controlIconsEnabled: true
      });
      this.spinning = false
    },
    removeEmbed(){
      if (this.lastEmbed) {
        // Destroy svgpanzoom
        svgPanZoom(this.lastEmbed).destroy()
        // Remove event listener
        this.lastEmbed.removeEventListener('load', () => this.lastEventListener())
        // Remove embed element
        this.$refs.svg.removeChild(this.lastEmbed)
        // Null reference to embed
        this.lastEmbed = null
      }
    },
    createNewEmbed(src) {
      this.spinning = true
      let embed = document.createElement('embed');
      embed.setAttribute('style', 'width: 100%; height: 100%;');
      embed.setAttribute('type', 'image/svg+xml');
      embed.setAttribute('src', src);
      this.$refs.svg.appendChild(embed);
      this.lastEmbed = embed
      embed.addEventListener('load', () => this.lastEventListener())
    }
  }
}
</script>

<style scoped lang='less'>
.svg-model-main {
  height: 100%;

  .no-data {
    display: flex;
    height: 100%;
    justify-content: center;
    align-items: center;
  }

  .svg-model {
    height: 100%;
    /deep/ .ant-spin-container {
      height: 100%;
      overflow: hidden;
    }
  }
}

</style>

后台的下载svg逻辑

content-type为image/svg+xml
@GetMapping("getFileContent/{fileId}")
    public void getFileContent(@PathVariable("fileId") String fileId,
                               HttpServletResponse response) {
        InputStream inputStream = fileModelService.getFile(fileId);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/svg+xml");
            IoUtil.copy(inputStream, outputStream);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            IoUtil.close(inputStream);
        }
    }

另外一个法子

https://github.com/kimguo1993/html5-svg-viewer

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值