• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


    SVG namespace & preview bug

    error

    This XML file does not appear to have any style information associated with it. The document tree is shown below.

    bad xml close tag

    Ok


    solution & svg namespace

    
    <?xml version="1.0" encoding="UTF-8"?>
    <svg width="976px" height="471px" viewBox="0 0 976 471" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    </svg>
    

    responsive viewbox

        svgAutoConvert(svgStr) {
          const viewport = `<svg width="100%" height="100%" viewBox="0 0 1000 1000" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`;
          // const viewport = `<svg width="100%" height="100%" viewBox="0 0 1000 1000">`;
          const xml = /<?xml[sS]*?>/i;
          let result = ``;
          result = svgStr.replace(xml,``);
          let index = result.indexOf(`>`);
          result = result.substr(index + 1);
          result = viewport + result;
          // log(`new result`, result);
          return result;
        },
        svgAutoScale(svgStr) {
          let result = ``;
          // 匹配任意字符 (s*Ss*)* 或 [sS]* ???
          const reg  = /[sS]*^<svg(s*Ss*)*>$/i;
          result = svgStr.replace(reg, `<svg width="100%" height="100%" viewBox="0 0 1000 1000" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`);
          log(`result`, result);
          return result;
        },
    
    

    auto Svg Download

    const autoSvgDownload = (filename = `live-map.svg`) =>{
      const svg = document.querySelector(`svg`);
      const html = svg.parentElement.innerHTML;
      // xml namespace, support browser open preview
      const xml = `<?xml version="1.0" encoding="UTF-8"?>
    ${html}`;
      console.log(`xml`, xml);
      console.log(`encodeURIComponent(xml)`, encodeURIComponent(xml));
      const element = document.createElement('a');
      element.setAttribute('href', 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(xml));
      element.setAttribute('download', filename);
      element.style.display = 'none';
      document.body.appendChild(element);
      element.click();
      document.body.removeChild(element);
    }
    
    

    demos

    https://cdn.xgqfrms.xyz/svg/namespace/poly.svg

    https://cdn.xgqfrms.xyz/svg/namespace/poly-bug.svg


  • 相关阅读:
    转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
    TYVJ P1081 最近距离 Label:这不是分治!!!
    TYVJ P1086 Elevator Label:dp
    数字图像处理的三个层次
    栅格化是什么意思?
    图像基本知识
    修改了天空盒子但是点play还是没变原因
    地形编辑
    Bmp8位图像亮度如何调整?
    bmp图像作业笔记
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/12307035.html
Copyright © 2020-2023  润新知