20 Star 2 Fork 1

UOLab联合开放实验室/Java1020

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
watermark.html 4.70 KB
一键复制 编辑 原始数据 按行查看 历史
杨舒婷 提交于 2022-07-14 09:47 . mark
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="info" onclick="alert(1)">
123
</div>
<script>
(function () {
function __canvasWM({
container = document.body,
width = '100px',
height = '100px',
textAlign = 'left',
textBaseline = 'middle',
font = "25px Microsoft Yahei",
fillStyle = 'rgba(184, 184, 184, 0.6)',
content = '水印',
rotate = '30',
zIndex = 10000
}) {
// 调整间距
let length = 500
let h = window.screen.width
const args = arguments[0];
const canvas = document.createElement('canvas');
canvas.setAttribute('width', h);
canvas.setAttribute('height', 800);
const ctx = canvas.getContext("2d");
ctx.textAlign = textAlign;
ctx.textBaseline = textBaseline;
ctx.font = font;
// 测量长度
let testWidth = ctx.measureText(content).width
let { a } = hypotenuse(testWidth, rotate)
console.log(a)
a = Math.round(a)
let b
if (a < h) {
b = ((1 - a / h) / 0.02).toFixed(2) + '%'
console.log(b);
} else {
b = 0
}
ctx.fillStyle = fillStyle;
ctx.rotate(Math.PI / 180 * rotate);
// ctx.fillText(content, parseFloat(width) / 100, parseFloat(height) / 100);
ctx.fillText(content, 0, 250);
const base64Url = canvas.toDataURL();
const __wm = document.querySelector('.__wm');
const watermarkDiv = __wm || document.createElement("div");
const styleStr = `
position:fixed;
top:0;
left:${b};
bottom:0;
right:0;
width:100%;
height:100%;
z-index:${zIndex};
pointer-events:none;
background-image:url('${base64Url}')`;
watermarkDiv.setAttribute('style', styleStr);
watermarkDiv.classList.add('__wm');
if (!__wm) {
container.style.position = 'relative';
container.insertBefore(watermarkDiv, container.firstChild);
}
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
if (MutationObserver) {
let mo = new MutationObserver(function () {
const __wm = document.querySelector('.__wm');
// 只在__wm元素变动才重新调用 __canvasWM
if ((__wm && __wm.getAttribute('style') !== styleStr) || !__wm) {
// 避免一直触发
mo.disconnect();
mo = null;
__canvasWM(JSON.parse(JSON.stringify(args)));
}
});
mo.observe(container, {
attributes: true,
subtree: true,
childList: true
})
}
}
if (typeof module != 'undefined' && module.exports) { //CMD
module.exports = __canvasWM;
} else if (typeof define == 'function' && define.amd) { // AMD
define(function () {
return __canvasWM;
});
} else {
window.__canvasWM = __canvasWM;
}
})();
// 调用
__canvasWM({
content: '霜剑覅耳B和局部将换行符is胡服务高建发股份沟通过而飞机客服呢机场水印'
});
function hypotenuse(long, angle) {
console.log(angle);
if (angle) {
console.log('enter');
return {
a: long
}
} else {
//获得弧度
var radian = 2 * Math.PI / 360 * angle;
return {
a: Math.sin(radian) * long,//邻边
b: Math.cos(radian) * long//对边
};
}
}
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ychs168/Java1020.git
git@gitee.com:ychs168/Java1020.git
ychs168
Java1020
Java1020
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385