通过canvas 实现

1.canvas 画 文字
2.获取 图文字的base64
3.给div 做背景,注意 div 设置 pointer-events = 'none'

const watermark = {}

const setWatermark = str => {
  const id = '1.23452384164.123412415'

  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }

  const can = document.createElement('canvas')
  can.width = 150
  can.height = 120

  const cans = can.getContext('2d')
  cans.rotate((-20 * Math.PI) / 180)
  cans.font = '20px Vedana'
  cans.fillStyle = 'rgba(200, 200, 200, 0.20)'
  cans.textAlign = 'left'
  cans.textBaseline = 'Middle'
  cans.fillText(str, can.width / 3, can.height / 2)

  const div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '70px'
  div.style.left = '0px'
  div.style.position = 'fixed'
  div.style.zIndex = '100000'
  div.style.width = document.documentElement.clientWidth - 100 + 'px'
  div.style.height = document.documentElement.clientHeight - 100 + 'px'
  div.style.background =
    'url(' + can.toDataURL('image/png') + ') left top repeat'
  document.body.appendChild(div)
  return id
}

// 该方法只允许调用一次
watermark.set = str => {
  let id = setWatermark(str)
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str)
    }
  }, 500)
  window.onresize = () => {
    setWatermark(str)
  }
}

watermark.set('你好')

通过svg 实现

function getSVGTextBase64(text, svgStyle) {
      var svgNS = 'http://www.w3.org/2000/svg';
      function createTag(tag, objAttr) {
        var oTag = document.createElementNS(svgNS, tag);
        for (var attr in objAttr) {
          oTag.setAttribute(attr, objAttr[attr]);
        }
        return oTag;
      }

      function encode(input) {

        function utf8_encode(string) {
          string = string.replace(/\r\n/g, "\n");
          var utftext = "";

          for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
              utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
              utftext += String.fromCharCode((c >> 6) | 192);
              utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
              utftext += String.fromCharCode((c >> 12) | 224);
              utftext += String.fromCharCode(((c >> 6) & 63) | 128);
              utftext += String.fromCharCode((c & 63) | 128);
            }

          }

          return utftext;
        }
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        input = utf8_encode(input);


        while (i < input.length) {

          chr1 = input.charCodeAt(i++);
          chr2 = input.charCodeAt(i++);
          chr3 = input.charCodeAt(i++);

          enc1 = chr1 >> 2;
          enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
          enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
          enc4 = chr3 & 63;

          if (isNaN(chr2)) {
            enc3 = enc4 = 64;
          } else if (isNaN(chr3)) {
            enc4 = 64;
          }

          output = output +
            _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
            _keyStr.charAt(enc3) + _keyStr.charAt(enc4);

        }

        return output;
      }

      svgStyle = Object.assign({
        'width': '120px',
        'height': '80px',
        'text-anchor': 'left',
        'font-size': '12px',
        'transform': 'translate(0 50) rotate(-30)',
        'opacity':'0.5',
        'x': '1em',
        'y': '1em',
      }, svgStyle);

      var oSvg = createTag('svg', { 'xmlns': svgNS, 'width': svgStyle.width, 'height': svgStyle.height, });
      var oText = createTag('text', svgStyle);
      oText.innerHTML = text;
      oSvg.appendChild(oText);

      var svgStr = new XMLSerializer().serializeToString(oSvg);
      var bgUrl = 'data:image/svg+xml;base64,' + encode(svgStr);
      return bgUrl;
    }

调用

var watermarkImg = getSVGTextBase64(userInfo.userName)
        $('.obsoleteMark').css({
            'background-image': 'url(' + watermarkImg + ')'
        });

标签: none

添加新评论