• 用JS给H5页面增加水印


    用JS给H5页面增加水印(watermark),代码示例如下:

    import store from "../store";
    export function waterMark() {
      (function waterMarkFulfill() {
        function textBecomeImg(text, fontsize, fontcolor) {
          var canvas = document.createElement("canvas");
          let buHeight = 0;
          if (fontsize <= 32) {
            buHeight = 99;
          } else if (fontsize > 32 && fontsize <= 60) {
            buHeight = 2;
          } else if (fontsize > 60 && fontsize <= 80) {
            buHeight = 4;
          } else if (fontsize > 80 && fontsize <= 100) {
            buHeight = 6;
          } else if (fontsize > 100) {
            buHeight = 10;
          }
          canvas.height = fontsize + buHeight;
          canvas.padding = 30;
          var context = canvas.getContext("2d");
          context.clearRect(0, 0, canvas.width * 2, canvas.height);
          context.textAlign = "center";
          canvas.width = 160;
          canvas.height = 130;
          context.fillStyle = fontcolor;
          context.font = fontsize + "px Arial";
          context.textBaseline = "middle";
          var lineHeight = 22;
          var lines = text.split("\n");
          context.fillText(lines[0], 40, fontsize / 2);
          context.fillText(lines[1], 28, fontsize / 2 + lineHeight);
          var dataUrl = canvas.toDataURL("image/png");
          return dataUrl;
        }
        var date = new Date();
        var time =
          date.getFullYear() +
          "-" +
          (date.getMonth() + 1).toString().padStart(2, "0") +
          "-" +
          date.getDate().toString().padStart(2, "0");
        var text = store.getters.realName + "\n" + time;
        var shuiyinDiv = document.createElement("div");
        var style = shuiyinDiv.style;
        style.position = "fixed";
        style.left = "-30%";
        style.top = "-60%";
        style.width = "200%";
        style.height = "200%";
        style.opacity = "0.1";
        style.background = "url(" + textBecomeImg(text, 16, "gray") + ")";
        style.zIndex = 9999999991;
        style.transform = "rotate(-30deg)";
        style.pointerEvents = "none";
        document.body.appendChild(shuiyinDiv);
      })();
    }

    效果如下:

  • 相关阅读:
    作业
    作业
    [转]C语言指针 之 函数指针
    iOS 隐私政策
    微信小程序成长记录(一)
    ios 用信号量控制多个异步网络请求
    ios 利用cocapods创建私有库
    ios 整理获取设备相关信息
    ios_scrollView顶部图片下拉放大
    ios 在项目中使用文字ttf文件
  • 原文地址:https://www.cnblogs.com/luoyihao/p/16249128.html
Copyright © 2020-2023  润新知