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 : "申继林"});
        };
  • 相关阅读:
    子级必须浮动,父级如何自动适应高度
    如何应用JS来改变CSS样式
    php对多维数组按某值排序的例子
    php socket编程
    HLSL 高级着色语言基础
    谈谈.NET提供的各种记时器
    获取中文字符串的Unicode值的方法!
    Tab键和KeyDown,KeyUp事件
    公有字段和属性的选择!
    C#中汉字的繁体和简体的相互转换的两个方法!
  • 原文地址:https://www.cnblogs.com/shenjilin/p/9856574.html
Copyright © 2011-2022 走看看