js添加圖片水印
不好用你抽我、
如果需要後臺(因爲後臺有個引包的問題)添加水印加Q931216004拿代碼,給個關注唄~
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8"/>
<script src="test.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/**獲取當前日期*/
function getDate() {
var date = new Date();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentDate = date.getFullYear() + '年' + month + '月' + strDate + '日';
return currentDate;
}
/*水印*/
function shuiyin(canvasid,imgurl,addtext){
var img = new Image ;
img.src = imgurl;
img.onload = function(){
var canvas = document.getElementById(canvasid);
var ctx = canvas.getContext("2d");
ctx.drawImage(img,0,0);
ctx.font = "14px 微軟雅黑";
ctx.fillStyle = "rgba(252,255,255,0.8)";
document.getElementById("shuiyinBtn").onclick = function(){
var date=getDate();
var addtext =date;
ctx.fillText(addtext,10,50); //選擇位置
}
}
}
/*canvas的id,以及圖片路徑*/
shuiyin("shuiyinTest","img/test.jpg")
function getDate() {
var date = new Date();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentDate = date.getFullYear() + '年' + month + '月' + strDate + '日';
return currentDate;
}
</script>
<link rel="stylesheet" type="text/css" href="test.css"/>
</head>
<body>
<canvas id="shuiyinTest">
</canvas>
<div>
<input id="shuiyinText" value="" type="text"/>
<button id="shuiyinBtn" class="mui-btn mui-btn-primary" type="button"> 點擊添加水印</button><br />
</div>
</body>
</html>