首页 > 开发 > HTML > 正文

h5网页水印SDK的实现代码示例

2019-10-27 21:05:42
字体:
来源:转载
供稿:网友

在网站浏览中,常常需要网页水印,以便防止用户截图或录屏暴露敏感信息后,追踪用户来源。如我们常用的钉钉软件,聊天背景就会有你的名字。那么如何实现网页水印效果呢?

网页水印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')); //压缩后的路径    });

指纹效果

h5,网页,水印,SDK,代码

效果地址

https://tianshengjie.cn/apps/web_fingerprint

项目地址

Github: https://github.com/Jay-tian/web-fingerprint
Npm包: https://www.npmjs.com/package/web-fingerprint

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CuoXin错新网。


注:相关教程知识阅读请移步到HTML教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表