欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 使用class創(chuàng)建和清除水印的示例代碼

 更新時(shí)間:2020年12月25日 10:14:02   作者:紫藤蘿yu  
這篇文章主要介紹了vue 使用class創(chuàng)建和清除水印的示例代碼,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下

頁(yè)面添加水印的方法有很多,以下舉例使用class定義的方法進(jìn)行水印內(nèi)容渲染:

1、新建文件:WatermarkClass.js

export default class WatermarkClass {
  constructor({id="watermarkID", str = "", fontSize = 18, width = 400, height = 400, fillStyle="#333333", opacity = 1 }) {
    this.id = id;
    this.str = str;
    this.fontSize = fontSize;
    this.width = width;
    this.height = height;
    this.fillStyle = fillStyle
    this.opacity = opacity;

  }

  // 繪制水印
  draw() {
   if (document.getElementById(this.id) !== null) {
    document.body.removeChild(document.getElementById(this.id));
   }

   const canvas = document.createElement("canvas");
   // 設(shè)置canvas畫(huà)布大小
   canvas.width = this.width;
   canvas.height = this.height;

   const ctx = canvas.getContext("2d");
   ctx.rotate(-(15 * Math.PI) / 180); // 水印旋轉(zhuǎn)角度
   ctx.font = `${this.fontSize}px Vedana`;
   ctx.fillStyle = this.fillStyle;
   ctx.textAlign = "center";
   ctx.textBaseline = "middle";
   this.str.split(",").forEach((item, index) => {
    ctx.fillText(item, canvas.width / 2, canvas.height / 2 + (index * this.fontSize + 10)); // 水印在畫(huà)布的位置x,y軸
   });

   const div = document.createElement("div");
   div.id = this.id;
   div.style.pointerEvents = "none";
   div.style.top = "30px";
   div.style.left = "10px";
   div.style.opacity = this.opacity;
   div.style.position = "fixed";
   div.style.zIndex = "999999";
   div.style.width = `${document.documentElement.clientWidth}px`;
   div.style.height = `${document.documentElement.clientHeight}px`;
   div.style.background = `url(${canvas.toDataURL("image/png")}) left top repeat`;
   document.body.appendChild(div);
  }

  setOptions({fontSize = 18, width = 300, height = 300, opacity = 1, str = ""}) {
   this.fontSize = fontSize;
   this.width = width;
   this.height = height;
   this.fillStyle = fillStyle
   this.opacity = opacity;
   this.str = str;
   this.draw();
  }

  // 繪制
  render() {
   this.draw();
   window.onresize = () => {
    this.draw();
   };
  }

  // 移除水印
  removeWatermark() {
   if (document.getElementById(this.id) !== null) {
    document.body.removeChild(document.getElementById(this.id));
   }
  }
 }

2、在頁(yè)面種引入使用:

import watermarkClass from "@/libs/watermarkClass";
export default {
 name: "App",
 mounted: function () {
  this.initWatermark()
 },
 methods: {
  initWatermark() {
   // 方法一
   let watermark = new watermarkClass({
    id: "watermarkID",
    str: "紫藤蘿-watermarkClass",
    fontSize: 20,
    width: 300,
    height: 200,
    fillStyle: "#dddddd",
    opacity: 0.4,
   });
   watermark.render();
   // 5秒后,清除水印
   setTimeout(() => {
    watermark.removeWatermark();
   }, 5000);
  }
 },
};

以上就是vue 使用class創(chuàng)建和清除水印的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于vue 創(chuàng)建和清除水印的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue-better-scroll 的使用實(shí)例代碼詳解

    vue-better-scroll 的使用實(shí)例代碼詳解

    這篇文章主要介紹了vue-better-scroll 的使用實(shí)例代碼詳解,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-12-12
  • vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能

    vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能

    這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺(tái)小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Vue.js項(xiàng)目實(shí)戰(zhàn)之多語(yǔ)種網(wǎng)站的功能實(shí)現(xiàn)(租車(chē))

    Vue.js項(xiàng)目實(shí)戰(zhàn)之多語(yǔ)種網(wǎng)站的功能實(shí)現(xiàn)(租車(chē))

    這篇文章主要介紹了Vue.js項(xiàng)目實(shí)戰(zhàn)之多語(yǔ)種網(wǎng)站(租車(chē))的功能實(shí)現(xiàn) ,需要的朋友可以參考下
    2019-08-08
  • Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能

    Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能

    這篇文章主要介紹了Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題

    解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題

    這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • vue-mounted中如何處理data數(shù)據(jù)

    vue-mounted中如何處理data數(shù)據(jù)

    這篇文章主要介紹了vue-mounted中如何處理data數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 在vue中寫(xiě)jsx的幾種方式

    在vue中寫(xiě)jsx的幾種方式

    本文主要介紹了在vue中寫(xiě)jsx的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 利用Vue.js指令實(shí)現(xiàn)全選功能

    利用Vue.js指令實(shí)現(xiàn)全選功能

    最近做了兩個(gè)vue的項(xiàng)目,都需要實(shí)現(xiàn)全選反選的功能,兩個(gè)項(xiàng)目用了兩種實(shí)現(xiàn)方法,第一個(gè)項(xiàng)目用vue的computed,第二個(gè)項(xiàng)目用指令來(lái)實(shí)現(xiàn),用起來(lái),發(fā)覺(jué)指令更加方便。下面就來(lái)介紹如何利用指令來(lái)實(shí)現(xiàn)全選。
    2016-09-09
  • 詳解Vue This$Store總結(jié)

    詳解Vue This$Store總結(jié)

    這篇文章主要介紹了詳解Vue This$Store總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • vue中v-for和v-if一起使用之使用compute的示例代碼

    vue中v-for和v-if一起使用之使用compute的示例代碼

    這篇文章主要介紹了vue中v-for和v-if一起使用之使用compute的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論