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

Logo

前往低代码交流专区

更多推荐