h5网页水印SDK的实现代码示例
导读:收集整理的这篇文章主要介绍了h5网页水印SDK的实现代码示例,觉得挺不错的,现在分享给大家,也给大家做个参考。 @H_360_0@在网站浏览中,常常需要网页水印,以便防止用户截图或录屏暴露敏感信息后,追踪用户来源。如我们常用的钉钉...
收集整理的这篇文章主要介绍了h5网页水印SDK的实现代码示例,觉得挺不错的,现在分享给大家,也给大家做个参考。 @H_360_0@在网站浏览中,常常需要网页水印,以便防止用户截图或录屏暴露敏感信息后,追踪用户来源。如我们常用的钉钉软件,聊天背景就会有你的名字。那么如何实现网页水印效果呢?网页水印SDK,实现思路
1.能更具获取到的当前用户信息,如名字,昵称,ID等,生成水印
2.生成一个Canvas,覆盖整个窗口,并且不影响其他元素
3.可以修改字体间距,大小,颜色
4.不依赖jquery
5.需要防止用户手动删除这个Canvas
实现分析
初始参数
Size: 字体大小color: 字体颜色id: canvasIdtext: 文本内容densITy: 间距clarity: 清晰度supportTip: Canvas不支持的文字提示
生成Canvas
根据id生成Canvas,画布大小为window.screen大小,若存在原有老的Canvas,清除并重新生成。
画布固定定位在可视窗口,z-index为-1
let body = document.getelementsbytagname('body'); let canvas = document.createElement('canvas'); canvas.style.cssText= 'position: fixed; width: 100%; height: 100%; left:0; top:0; z-index: -1; '; body[0].apPEndChild(canvas);
指纹生成算法
let canvas = document.getElementById(this.params.id); let cxt = canvas.getContext('2d'); let times = window.screen.width * this.params.clarity / this.params.density; //横向文字填充次数 let heightTimes = window.screen.height * this.params.clarity * 1.5/ this.params.density; //纵向文字填充次数 cxt.rotate(-15*Math.PI/180); //倾斜画布 for(let i = 0; i times; i++) { for(let j = 0; j heightTimes; j++) { cxt.fillStyle = this.params.color; cxt.font = this.params.size + ' Arial'; cxt.fillText(this.params.text, this.params.density*i, j*this.params.density); } }
防止用户删除
使用定时器,定时检查指纹是否存在
let self = this; window.setInterval(function(){ if (!document.getElementById(self.params.id)) { self._init(); } } , 1000);
项目编译
使用glup编译
VAR gulp = require('gulp'), uglify = require("gulp-uglify"), babel = require("gulp-babel"); gulp.task('minify', function () { return gulp.src('./src/index.js') // 要压缩的js文件 .pipe(babel()) .pipe(uglify()) .pipe(gulp.dest('./dist')); //压缩后的路径 } );
指纹效果
效果地址
https://tianshengjie.cn/apps/web_fingerprint
项目地址
Github: https://github.com/Jay-tian/web-fingerprint
Npm包: https://www.npmjs.com/package/web-fingerprint
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: h5网页水印SDK的实现代码示例
本文地址: https://pptw.com/jishu/585928.html