<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
/*
添加水印函数
str:水印显示的文本内容
*/
function addWaterMarker(str) {
var can = document.createElement('canvas');
//水印显示的背景容器
var body = document.body;
body.appendChild(can);
can.width = 400;
can.height = 200;
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.fillStyle = '#cc0000';
cans.textAlign = 'left';
cans.textBaseline = 'Middle';
cans.fillText(str, can.width / 3, can.height / 2);
body.style.backgroundImage = "url(" + can.toDataURL("image/png") + ")";
}
window.onload = function () {
addWaterMarker('显示水印')
}
</script>
</head>
<body>
</body>
</html>
显示的效果