HTML5中如何實(shí)現(xiàn)網(wǎng)頁(yè)水印SDK-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)HTML5中如何實(shí)現(xiàn)網(wǎng)頁(yè)水印SDK,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

目前創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、臺(tái)江網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

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

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

防止用戶刪除

使用定時(shí)器,定時(shí)檢查指紋是否存在

  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')); //壓縮后的路徑
    });

指紋效果

HTML5中如何實(shí)現(xiàn)網(wǎng)頁(yè)水印SDK

關(guān)于“HTML5中如何實(shí)現(xiàn)網(wǎng)頁(yè)水印SDK”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

文章名稱:HTML5中如何實(shí)現(xiàn)網(wǎng)頁(yè)水印SDK-創(chuàng)新互聯(lián)
文章源于:http://muchs.cn/article24/dddhje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站排名品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈微信小程序、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名