PS系統(tǒng)頁面水印解決方案

目的

實(shí)現(xiàn)PS系統(tǒng)頁面水印的需求,提升系統(tǒng)的信息保密性。

實(shí)現(xiàn)原理

通過canvas繪制水印,將水印作為蒙層覆蓋頁面。

實(shí)現(xiàn)步驟

  1. 在Application Designer中創(chuàng)建HTML對象,將watermark.js中代碼寫入;

  2. 在PeopleTools/門戶網(wǎng)站/品牌化/品牌化系統(tǒng)選項(xiàng)中,添加JavaScript對象,即上一步創(chuàng)建的HTML對象;

水印效果

img.png

watermark.js內(nèi)容

/**
 * @description 水印
 * @author LeaFish <735683662@qq.com>
 * @date 2019-03-13
 * @param {string} [id="watermark"] 水印元素ID
 * @param {number} [height=80] 單個(gè)水印塊高度
 * @param {number} [width=150] 單個(gè)水印塊寬度
 * @param {string} [text] 水印文本
 * @param {number} [fontSize=13] 文本字體大小
 * @param {string} [color="rgba(100,100,100,0.2)"] 文本顏色
 * @param {string} [displayMethod] 展現(xiàn)方式:background 背景圖(采用repeat特性) canvas 畫布(采用畫布方式,建議isOnResize=true)
 * @param {boolean} [isOnResize=false] 是否監(jiān)聽瀏覽器窗口變化,重繪水印
 */
function Watermark(options) {
  options = options || {};
  this.id = options.id || "watermark";
  this.canvas = document.createElement("canvas");
  this.ctx = this.canvas.getContext("2d");
  this.height = options.height || 80;
  this.width = options.width || 150; // 控制水印的間隙大小
  this.text = options.text || "";
  this.fontSize = options.fontSize || 13;
  this.color = options.color || "rgba(100,100,100,0.2)";
  this.displayMethod = options.displayMethod || "background";
  this.isOnResize = options.isOnResize || false;

  // 獲取設(shè)備像素比
  this.PIXEL_RATIO = (function () {
    var dpr = window.devicePixelRatio || 1;
    var bsr = this.ctx.webkitBackingStorePixelRatio ||
      this.ctx.mozBackingStorePixelRatio ||
      this.ctx.msBackingStorePixelRatio ||
      this.ctx.oBackingStorePixelRatio ||
      this.ctx.backingStorePixelRatio || 1;
    return dpr / bsr;
  }.bind(this))();

  /**
   * @description 初始化執(zhí)行函數(shù)
   * @author LeaFish <735683662@qq.com>
   * @date 2019-03-13
   */
  this.init = function () {
    this.draw();
    this.addToBody();

    this.isOnResize && (window.onresize = function () {
      this.draw();
      this.displayMethod === "canvas" || this.addToBody();
    }.bind(this));
  }

  /**
   * @description 繪制水印
   * @author LeaFish <735683662@qq.com>
   * @date 2019-03-13
   */
  this.draw = function () {
    var canvasWidth = window.innerWidth;
    var canvasHeight = window.innerHeight * 2;

    // 適配高清屏,canvas內(nèi)容的寬高是實(shí)際的寬高的PIXEL_RATIO倍
    this.canvas.width = canvasWidth * this.PIXEL_RATIO;
    this.canvas.height = canvasHeight * this.PIXEL_RATIO;
    this.canvas.style.width = canvasWidth + "px";
    this.canvas.style.height = canvasHeight + "px";
    // 縮放繪圖
    this.ctx.setTransform(this.PIXEL_RATIO, 0, 0, this.PIXEL_RATIO, 0, 0);

    this.ctx.font = this.fontSize + "px 黑體";
    this.ctx.rotate(-20 * Math.PI / 180);
    this.ctx.fillStyle = this.color;

    // 繪制文字
    for (var y = 1; y < window.innerHeight * 2 / this.height + 1; y++) {
      for (var x = 1; x < window.innerWidth * 2 / this.width; x++) {
        this.ctx.fillText(this.text, (y % 2 ? 0 : this.width / 2) + x * this.width - window.innerWidth, y * this.height);
      }
    }
  };

  /**
   * @description 將水印添加到body上
   * @author LeaFish <735683662@qq.com>
   * @date 2019-03-13
   */
  this.addToBody = function () {
    var element = null;

    if (this.displayMethod === "canvas") {
      element = this.canvas;
    } else {
      var base64 = this.canvas.toDataURL("image/png");
      element = document.createElement('div');
      element.style.backgroundSize = innerWidth + "px";
      element.style.backgroundImage = "url(" + base64 + ")";
      element.style.backgroundRepeat = "repeat";
    }

    element.id = this.id;
    element.style.position = "fixed";
    element.style.left = 0;
    element.style.right = 0;
    element.style.top = 0;
    element.style.bottom = 0;
    element.style.pointerEvents = "none"; // 蒙層事件穿透
    element.style.zIndex = 20000000;

    document.getElementById(this.id) && document.body.removeChild(document.getElementById(this.id));
    document.body.appendChild(element);
  }
}

/**
 * @description 處理多l(xiāng)oad事件回調(diào)函數(shù)覆蓋問題
 * @author LeaFish <735683662@qq.com>
 * @date 2019-03-13
 * @param {*} x
 */
function addOnloadEvent(x) {
  var oldOnLoad = window.onload;
  if (typeof oldOnLoad != "function") {
    window.onload = x;
  } else {
    window.onload = function () {
      oldOnLoad();
      x();
    }
  }
}

addOnloadEvent(function () {
  // 避免iframe間多層水印層疊
  document.getElementById("ptifrmtemplate") && setTimeout(function () {
    var options = {
      text: parent.document.getElementsByClassName("greeting")[0].innerText.split(":")[1], // 從歡迎語中獲取要作為水印的文本內(nèi)容
      isOnResize: false,
      displayMethod: "background"
    }
    new Watermark(options).init();
  }, 50);
})

watermark.js使用注意事項(xiàng)

  1. 代碼中水印文本內(nèi)容是從歡迎語中獲取的,可根據(jù)自身需求修改水印文本的獲取方式,例如請求數(shù)據(jù)接口獲??;

  2. Watermark對象繪制水印可同時(shí)支持移動(dòng)端和PC端;

開發(fā)者可理解實(shí)現(xiàn)思路,實(shí)現(xiàn)出更符合自身需求的水印排版。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概要 64學(xué)時(shí) 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,799評論 0 3
  • 【直播回顧】 在周四收評中,鵬凱指出指數(shù)仍然有反彈空間: 周五早評提示向上突破2894的概率大,事實(shí)上指數(shù)盤中沖擊...
    看市者鵬凱閱讀 599評論 0 0
  • 昨晚洗澡洗的睡得晚了,早上特意讓孩子多睡一會(huì),快7點(diǎn)了才叫他起床。收拾一下洗漱完畢趕緊吃飯,還好我去上班沒有遲到,...
    2019影閱讀 101評論 0 1
  • 第一,懷著好奇心廣泛閱讀。那些思路清晰的人,往往有跨學(xué)科背景,而廣泛閱讀正是了解不同學(xué)科的一種手段,閱讀給我們提供...
    雷米巴比力閱讀 620評論 0 0
  • 我獻(xiàn)血的起源要從2017年的夏天說起了,應(yīng)一直堅(jiān)持跑步,突發(fā)奇想想?yún)⒓玉R拉松比賽,還有一個(gè)同事,也是和我一樣...
    A劉澤成閱讀 833評論 1 1

友情鏈接更多精彩內(nèi)容