前端canvas水印快速制作(附完整代碼)
發(fā)布時(shí)間:2019-09-19 16:13:05 作者:森君不能再懶惰
我要評論
這篇文章主要介紹了前端canvas水印快速制作(附完整代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
兩種水印效果如圖:


原理解析:
- 圖一斜紋類:創(chuàng)建一個(gè)和頁面一樣大的畫布,根據(jù)頁面大小以及傾斜角度大致鋪滿水印文字,最后轉(zhuǎn)化為一張圖片設(shè)為背景
- 圖二傾斜類:將文字傾斜后轉(zhuǎn)化為圖片,然后設(shè)置背景圖片repeat填充整個(gè)頁面
代碼分析:
這里我只簡略分析圖一斜紋類,事實(shí)上這兩種方式都較為簡單,不需要特別強(qiáng)的canvas基礎(chǔ),只需大概了解就行,直接上完整代碼吧
圖一
<!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個(gè)字,實(shí)際按需求來
var cpyName = str;
if (str.length > 16) {
cpyName = str.substring(0, 16);
}
// 創(chuàng)建 canvas 元素
var can = document.createElement('canvas');
// 獲取 content 元素
var report = $('.content')[0]
// 將 canvas 元素添加到 content 中
report.appendChild(can);
// 設(shè)置 canvas頁面寬度,這里的 800 是因?yàn)槲宜舅∥募笮」潭ǎ砂葱枨蟾?
can.width = 800;
// 獲取整個(gè)body高度
can.height = document.body.offsetHeight;
// 隱藏 canvas 元素
can.style.display = 'none';
can.style.zIndex = '999'
// 獲取 canvas 元素工具箱
var cans = can.getContext('2d');
// 設(shè)置文字傾斜角度為 -25 度以及樣式
cans.rotate(-25 * Math.PI / 180);
cans.font = "800 30px Microsoft JhengHei";
cans.fillStyle = "#000";
cans.textAlign = 'center';
cans.textBaseline = 'Middle';
// 動態(tài)改變字體大小
if(cans.measureText(cpyName).width > 180) {
var size = 180 / cpyName.length
cans.font = '800 ' + size +'px '+ ' Microsoft JhengHei'
}
/*
雙重遍歷,
當(dāng) 寬度小于頁面寬度時(shí),
當(dāng) 高度小于頁面高度時(shí),
這里的寬高可以適當(dāng)寫大,目的是為了讓水印文字鋪滿
*/
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 轉(zhuǎn)化為圖片并且設(shè)置為背景
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
這篇文章主要介紹了前端水印的簡單實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)2020-12-02
手摸手教你用canvas實(shí)現(xiàn)給圖片添加平鋪水印的實(shí)現(xiàn)
這篇文章主要介紹了手摸手教你用canvas實(shí)現(xiàn)給圖片添加平鋪水印的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨2019-08-20
html5 canvas實(shí)現(xiàn)給圖片添加平鋪水印
這篇文章主要介紹了html5 canvas實(shí)現(xiàn)給圖片添加平鋪水印,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-20- 這篇文章主要介紹了canvas 下載二維碼和圖片加水印的方法的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-21

前端使用canvas生成盲水印的加密解密的實(shí)現(xiàn)
這篇文章主要介紹了前端使用canvas生成盲水印的加密解密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編2020-12-16



