• 使用 js 动态修改在线 svg 背景色


    使用 js 动态修改在线 svg 背景色

    由于网上搜了很多相关资料,都是介绍如何通过 js 修改本地 svg 资源。不过我们要求是要修改在线 svg 资源,因此,在一顿研究之后,封装了如下组件。

    <template>
      <img v-if="url" class="hyl-svg-container" :src="url"/>
    </template>
    
    <script>
      export default {
        name: 'hyl-svg-container',
        props: {
          src: String,
          fill: String,
          options: {
            type: Object,
            default: () => ({})
          }
        },
        data() {
          return {
            content: '',
            url: ''
          };
        },
        watch: {
          fill: {
            handler(v) {
              if (this.content && v) {
                this.fillColor();
              }
            },
            immediate: true
          }
        },
        created() {
          this.fetchSvgSource();
        },
        methods: {
          async fetchSvgSource() {
            if (!this.src) return;
            const response = await fetch(this.src);
            const contentType = response.headers.get('content-type');
            const [fileType] = (contentType || '').split(/ ?; ?/);
    		// 异常状态
            if (response.status > 299) {
              console.error('Not found');
            }
            this.url = '';
    		// 非有效格式
            if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {
              console.error(`Content type isn't valid: ${fileType}`);
              this.url = this.src;
              return;
            }
            // 获取内容
            const content = await response.text();
            this.content = content;
            this.fillColor();
          },
          fillColor() {
            if (this.fill) {
              // 支持自定义填充颜色
              this.content = this.content.replace(/#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/g, this.fill);
            }
            // 转换base64
            const base64Code = btoa(this.content);
            const base64SVG = `data:image/svg+xml;base64,${base64Code}`;
            this.url = base64SVG;
          }
        }
      };
    </script>
    
    <style>
    .hyl-svg-container {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
  • 相关阅读:
    GEM5 Garnet DVFS / NoC DVFS教程:ruby.clk_domain ruby.voltage_domain
    21天学习挑战赛之java多线程
    边坡安全监测系统的功能优势
    chatgpt赋能python:Python文件夹的使用和优化
    Uniapp---快速生成安卓证书keystore文件
    Sping-AOP切面相关操作
    美国签证被拒签后怎么申诉?
    机器学习之朴素贝叶斯分类
    axios Post 数据问题
    Linux·字符设备和杂项设备
  • 原文地址:https://blog.csdn.net/u013243347/article/details/133034185