前端實(shí)現(xiàn)添加水印功能的四種方法
實(shí)現(xiàn)添加水印功能常見的方法包括使用 CSS、Canvas 和 SVG 等。
方法 1: 使用 CSS 實(shí)現(xiàn)水印
通過 CSS 可以簡(jiǎn)單地實(shí)現(xiàn)全局水印,即給需要添加水印的元素添加背景圖。這種方式比較簡(jiǎn)單方便,適合靜態(tài)頁(yè)面,但靈活性和防篡改能力較差。
具體的實(shí)現(xiàn)示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS水印示例</title>
<style>
.watermark {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
pointer-events: none;
opacity: 0.2;
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200"><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="30" fill="rgba(0,0,0,0.1)">水印文本</text></svg>') repeat;
}
</style>
</head>
<body>
<div class="watermark"></div>
<div>
<h1>內(nèi)容區(qū)域</h1>
<p>這是需要加水印的內(nèi)容</p>
</div>
</body>
</html>
方法 2: 使用 Canvas 實(shí)現(xiàn)水印
使用 Canvas 可以生成更復(fù)雜的水印圖案,適合動(dòng)態(tài)內(nèi)容,并且相對(duì)難以篡改。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas水印示例</title>
<style>
.watermark {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
pointer-events: none;
opacity: 0.2;
}
</style>
</head>
<body>
<canvas id="watermarkCanvas" class="watermark"></canvas>
<div>
<h1>內(nèi)容區(qū)域</h1>
<p>這是需要加水印的內(nèi)容</p>
</div>
<script>
window.onload = function() {
var canvas = document.getElementById('watermarkCanvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var text = '水印文本';
var width = canvas.width;
var height = canvas.height;
ctx.font = '30px Arial';
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// 旋轉(zhuǎn)并重復(fù)繪制水印文本
for (var x = 0; x < width; x += 200) {
for (var y = 0; y < height; y += 200) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(- Math.PI / 4);
ctx.fillText(text, 0, 0);
ctx.restore();
}
}
};
</script>
</body>
</html>
方法 3: 使用 SVG 實(shí)現(xiàn)水印
SVG 也是一種實(shí)現(xiàn)水印的有效方式,容易實(shí)現(xiàn)和修改,具備良好的圖形渲染能力。性能不如 canvas 好,但是dom更友好
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG水印示例</title>
<style>
.watermark {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
pointer-events: none;
opacity: 0.2;
}
</style>
</head>
<body>
<div class="watermark">
<svg width="100%" height="100%">
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="30" fill="rgba(0,0,0,0.1)" transform="rotate(-45, 50, 50)">水印文本</text>
</svg>
</div>
<div>
<h1>內(nèi)容區(qū)域</h1>
<p>這是需要加水印的內(nèi)容</p>
</div>
</body>
</html>
方法 4: 動(dòng)態(tài)生成水印圖像
更靈活的方式是通過 JavaScript 動(dòng)態(tài)生成水印圖像。相對(duì)其他方式,最高的靈活性,可以在運(yùn)行時(shí)生成任意圖案的水印。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>動(dòng)態(tài)水印示例</title>
<style>
.watermark {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
pointer-events: none;
background-repeat: repeat;
opacity: 0.2;
}
</style>
</head>
<body>
<div id="watermarkContainer"></div>
<div>
<h1>內(nèi)容區(qū)域</h1>
<p>這是需要加水印的內(nèi)容...</p>
</div>
<script>
function createWatermark(text) {
var canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
var ctx = canvas.getContext('2d');
ctx.font = '30px Arial';
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.translate(100, 100);
ctx.rotate(- Math.PI / 4);
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, 0, 0);
return canvas.toDataURL('image/png');
}
window.onload = function() {
var watermarkContainer = document.getElementById('watermarkContainer');
watermarkContainer.classList.add('watermark');
watermarkContainer.style.backgroundImage = 'url(' + createWatermark('水印文本') + ')';
};
</script>
</body>
</html>總結(jié)
到此這篇關(guān)于前端實(shí)現(xiàn)添加水印功能的四種方法的文章就介紹到這了,更多相關(guān)前端添加水印功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào)
這篇文章主要介紹了信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
JS實(shí)現(xiàn)表單中checkbox對(duì)勾選中增加邊框顯示效果
這篇文章主要介紹了JS實(shí)現(xiàn)表單中checkbox對(duì)勾選中增加邊框顯示效果,可實(shí)現(xiàn)點(diǎn)擊后圖片增加邊框及勾選的功能,非常簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2015-08-08
詳解如何構(gòu)建Promise隊(duì)列實(shí)現(xiàn)異步函數(shù)順序執(zhí)行
這篇文章主要介紹了詳解如何構(gòu)建Promise隊(duì)列實(shí)現(xiàn)異步函數(shù)順序執(zhí)行,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-10-10
JavaScript 就地編輯HTML節(jié)點(diǎn)實(shí)現(xiàn)代碼
JavaScript 就地編輯HTML節(jié)點(diǎn)實(shí)現(xiàn)代碼2009-07-07
PHP+jQuery+Ajax+Mysql如何實(shí)現(xiàn)發(fā)表心情功能
這篇文章通過php+jquery+ajax+mysql相結(jié)合,實(shí)現(xiàn)當(dāng)用戶瀏覽網(wǎng)站文章或者是論壇帖子后,想表達(dá)自己瀏覽后的心情,發(fā)表自己的感受,很多網(wǎng)站都提供了用戶發(fā)表心情的功能,通過此功能可以很直觀的分析文章或者是論壇對(duì)瀏覽者的用戶體驗(yàn)度2015-08-08

