兩種水印效果如圖:css
原理解析:html
代碼分析:jquery
這裏我只簡略分析圖一斜紋類,事實上這兩種方式都較爲簡單,不須要特別強的canvas基礎,只需大概瞭解就行,直接上完整代碼吧canvas
<!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>
<style>
.water {
width: 100vw;
height: 2000px;
position: absolute;
top: 0;
left: 0;
background-repeat: no-repeat;
}
.content {
width: 800px;
height: 2000px;
margin-left: auto;
margin-right: auto;
background: cadetblue;
overflow: hidden;
}
</style>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>
<div class="content">
<div class="water"></div>
</div>
<script>
function addWaterMarker(str) {
// 這裏限制了不超過15個字,實際按需求來
var cpyName = str;
if (str.length > 16) {
cpyName = str.substring(0, 16);
}
// 建立 canvas 元素
var can = document.createElement('canvas');
// 獲取 content 元素
var report = $('.content')[0]
// 將 canvas 元素添加到 content 中
report.appendChild(can);
// 設置 canvas頁面寬度,這裏的 800 是由於我司水印文件大小固定,可按需求更改
can.width = 800;
// 獲取整個body高度
can.height = document.body.offsetHeight;
// 隱藏 canvas 元素
can.style.display = 'none';
can.style.zIndex = '999'
// 獲取 canvas 元素工具箱
var cans = can.getContext('2d');
// 設置文字傾斜角度爲 -25 度以及樣式
cans.rotate(-25 * Math.PI / 180);
cans.font = "800 30px Microsoft JhengHei";
cans.fillStyle = "#000";
cans.textAlign = 'center';
cans.textBaseline = 'Middle';
// 動態改變字體大小
if(cans.measureText(cpyName).width > 180) {
var size = 180 / cpyName.length
cans.font = '800 ' + size +'px '+ ' Microsoft JhengHei'
}
/*
雙重遍歷,
當 寬度小於頁面寬度時,
當 高度小於頁面高度時,
這裏的寬高能夠適當寫大,目的是爲了讓水印文字鋪滿
*/
for(let i = (document.body.offsetHeight*0.5)*-1; i<800; i+=160) {
for(let j = 0; j<document.body.offsetHeight*1.5; j+=60) {
// 填充文字,x 間距, y 間距
cans.fillText(cpyName, i, j)
}
}
// 將 canvas 轉化爲圖片而且設置爲背景
report.style.backgroundImage = "url(" + can.toDataURL("image/png") + ")";
}
addWaterMarker('測試水印');
</script>
</body>
</html>
複製代碼
<!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>
<style>
.water {
width: 100vw;
height: 2000px;
position: absolute;
top: 0;
left: 0;
background-repeat: no-repeat;
}
.content {
width: 800px;
height: 2000px;
margin-left: auto;
margin-right: auto;
background: cadetblue;
overflow: hidden;
}
</style>
</head>
<body>
<div class="content">
<div class="water"></div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
// 添加水印方法
function addWaterMarker(str) {
var cpyName = str;
if (str.length > 16) {
cpyName = str.substring(0, 16);
}
var can = document.createElement('canvas');
var report = $('.content')[0];
report.appendChild(can);
can.width = 180;
can.height = 110;
can.style.display = 'none';
can.style.zIndex = '999'
var cans = can.getContext('2d');
cans.rotate(-25 * Math.PI / 180);
cans.font = "800 30px Microsoft JhengHei";
cans.fillStyle = "#000";
cans.textAlign = 'center';
cans.textBaseline = 'Middle';
if(cans.measureText(cpyName).width > 180) {
var size = 180 / cpyName.length
cans.font = '800 ' + size +'px '+ ' Microsoft JhengHei'
}
cans.fillText(cpyName, 60, 100);
report.style.backgroundImage = "url(" + can.toDataURL("image/png") + ")";
}
addWaterMarker('測試水印');
</script>
</body>
</html>
複製代碼