zoukankan      html  css  js  c++  java
  • 网页加水印 svg 方式

        
        /**
     *网页加水印 svg 方式
     *
     * @export
     * @param {*} [{
     *   container = document.body,
     *   content = '请勿外传',
     *   width = '300px',
     *   height = '200px',
     *   opacity = '0.2',
     *   fontSize = '20px',
     *   zIndex = 1000
     * }={}]
     */
     function __waterDocumentSvg({
      container = document.body,
      content = '请勿外传',
      width = '120px',
      height = '200px',
      opacity = '0.2',
      fontSize = '20px',
      zIndex = 0
    } = {}) {
      const args = arguments[0];
      const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${width}">
    <text x="20%" y="20%" dy="12px"
    text-anchor="middle"
    stroke="#000000"
    stroke-width="1"
    stroke-opacity="${opacity}"
    fill="none"
    transform="rotate(-45, 100 10)"
    style="font-size: ${fontSize};">
    ${content}
    </text>
    </svg>`;
      const base64Url = `data:image/svg+xml;base64,${window.btoa(unescape(encodeURIComponent(svgStr)))}`;
      const __wm = document.querySelector('.__wm');
      const watermarkDiv = __wm || document.createElement("div");
      const styleStr = `
        position:absolute;
        top:0;
        left:0;
        100%;
        height:100%;
        z-index:${zIndex};
        pointer-events:none;
        background-repeat:repeat;
        background-image:url('${base64Url}')`;
      watermarkDiv.setAttribute('style', styleStr);
      watermarkDiv.classList.add('__wm');
      if (!__wm) {
        container.style.position = 'relative';
        container.insertBefore(watermarkDiv, container.firstChild);
      }
      const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
      if (MutationObserver) {
        let mo = new MutationObserver(function () {
          const __wm = document.querySelector('.__wm');
          // 只在__wm元素变动才重新调用 __canvasWM
          if ((__wm && __wm.getAttribute('style') !== styleStr) || !__wm) {
            // 避免一直触发
            mo.disconnect();
            mo = null;
            __waterDocumentSvg(JSON.parse(JSON.stringify(args)));
          }
        });
        mo.observe(container, {
          attributes: true,
          subtree: true,
          childList: true
        })
      }
    }
        
        //onload时触发水印绘制
        window.onload=function(){
            __waterDocumentSvg({content : "申继林"});
        };
  • 相关阅读:
    005 Python的IDE之Pycharm的使用
    006 Python的IDE之Jupyter的使用
    004 pip的使用
    003 Python解释器源修改
    002 Python解释器安装
    BZOJ 4567 [SCOI2016]背单词 (Trie树、贪心)
    BZOJ 2085 luogu P3502 [POI2010]Hamsters (KMP、Floyd、倍增)
    UOJ #219 BZOJ 4650 luogu P1117 [NOI2016]优秀的拆分 (后缀数组、ST表)
    UOJ #214 [UNR #1]合唱队形 (概率期望计数、DP、Min-Max容斥)
    LOJ #2542 [PKUWC2018]随机游走 (概率期望、组合数学、子集和变换、Min-Max容斥)
  • 原文地址:https://www.cnblogs.com/shenjilin/p/9856574.html
Copyright © 2011-2022 走看看