App下載

在html5中怎么實(shí)現(xiàn)SDK水?。繉?shí)現(xiàn)案例分享!

猿友 2021-07-27 10:13:24 瀏覽數(shù) (1725)
反饋

對于水印大家應(yīng)該不陌生,那么今天我們就來說說有關(guān)于:“在html5中怎么實(shí)現(xiàn)SDK水???實(shí)現(xiàn)案例分享! ”這個問題,下面是小編收集的相關(guān)內(nèi)容!

網(wǎng)頁水印SDK,實(shí)現(xiàn)思路

1.能更具獲取到的當(dāng)前用戶信息,如名字,昵稱,ID等,生成水印
2.生成一個Canvas,覆蓋整個窗口,并且不影響其他元素
3.可以修改字體間距,大小,顏色
4.不依賴Jquery
5.需要防止用戶手動刪除這個Canvas

實(shí)現(xiàn)分析

初始參數(shù)

size: 字體大小
color: 字體顏色
id: canvasId
text: 文本內(nèi)容
density: 間距
clarity: 清晰度
supportTip: Canvas不支持的文字提示

生成Canvas

根據(jù)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;//橫向文字填充次數(shù)
      let heightTimes = window.screen.height * this.params.clarity * 1.5/ this.params.density; //縱向文字填充次數(shù)
      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);

項(xiàng)目編譯

使用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

項(xiàng)目地址

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

以上分享的就是有關(guān)于:“在html5中怎么實(shí)現(xiàn)SDK水???實(shí)現(xiàn)案例分享! ”這方面的相關(guān)內(nèi)容,當(dāng)然我們還可以在W3Cschool中學(xué)習(xí)其他html5這方面的相關(guān)內(nèi)容。希望小編的分享對大家有所幫助。 

0 人點(diǎn)贊