js给网页加水印

2022-12-29  本文已影响0人  91Shan

也没啥好说的,直接复制下面代码到你网站下的控制台运行就出效果

注意:记得给根元素样式加个opacity:0.9
let addMask = (str)=>{
  var can = document.createElement('canvas');
  var body = document.body;
  body.appendChild(can);
  can.width=300;
  can.height=210;
  can.style.display='none';
  var cans = can.getContext('2d');
  cans.rotate(-20*Math.PI/180);
  cans.font = "16px Microsoft JhengHei";
  cans.fillStyle = "rgba(17, 17, 17, 0.50)";
  cans.textAlign = 'left';
  cans.textBaseline = 'Middle';
  cans.fillText(str,can.width/3,can.height/2);
  body.style.backgroundImage="url("+can.toDataURL("image/png")+")";
}

addMask("http://web.shanpf.cn");
上一篇 下一篇

猜你喜欢

热点阅读