Vue's global watermark implementation example

Vue's global watermark implementation example

[Requirement] The system pages display watermarks, but the login page does not have a watermark (the login page will not display a watermark when logging out)

1. Create a watermark Js file

/*
 * @Author: Liu Xiaoer* @Date: 2021-07-15 14:43:27
 * @LastEditTime: 2021-07-15 15:00:27
 * @LastEditors: Please set LastEditors
 * @Description: Add watermark * @FilePath: /huashijc_MeetingSys/src/common/warterMark.js
 */
'use strict'
 
let watermark = {}
 
let setWatermark = (str) => {
  let id = '1.23452384164.123412415'
 
  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }
 
  let can = document.createElement('canvas')
  can.width = 250
  can.height = 120
 
  let cans = can.getContext('2d')
  cans.rotate(-15 * Math.PI / 150)
  cans.font = '20px Vedana'
  cans.fillStyle = 'rgba(200, 200, 200, 0.20)'
  cans.textAlign = 'left'
  cans.textBaseline = 'Middle'
  cans.fillText(str, can.width / 8, can.height / 2)
 
  let div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '35px'
  div.style.left = '0px'
  div.style.position = 'fixed'
  div.style.zIndex = '100000'
  div.style.width = document.documentElement.clientWidth + 'px'
  div.style.height = document.documentElement.clientHeight + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  document.body.appendChild(div)
  return id
}
 
// This method is only allowed to be called oncewatermark.set = (str) => {
  let id = setWatermark(str)
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str)
    }
  }, 500)
  window.onresize = () => {
    setWatermark(str)
  }
}

const outWatermark = (id) => {
    if (document.getElementById(id) !== null) {
      const div = document.getElementById(id)
      div.style.display = 'none'
    }
}
watermark.out = () => {
    const str = '1.23452384164.123412415'
    outWatermark(str)
}
 
export default watermark

2. Introduction operation

2.1 Reference in App.vue or other pages

// 1.In the App.vue file, import the file import Watermark from '@/common/watermark';

computed: {
  userName() {
    const name = this.$store.state.user.name
    return (name && name.length > 0) ? name : 'User name not obtained'
  }
},
mounted() {
  Watermark.set(this.userName)
}

// 2. Reference import Watermark from '@/common/watermark' in other pages;

created() {
  Watermark.set('admin')
}

2.2 Reference in the router configuration file

const outWatermark = (id) => {
  if (document.getElementById(id) !== null) {
    const div = document.getElementById(id)
    div.style.display = 'none'
  }
}

router.afterEach((to) => {
 if(to.path == '/'){
  Watermark.out() // Clear watermark }else{
  Watermark.set('Username not obtained') // Set watermark title
 }
});

This is the end of this article about the implementation example of Vue's global watermark. For more relevant Vue global watermark content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The vue project realizes drawing a watermark in a certain area
  • Vue easily realizes watermark effect
  • Vue uses custom instructions to add watermarks to the bottom of the page
  • Vue integrates PDF.js to implement PDF preview and add watermark steps
  • Vue example code using class to create and clear watermark
  • How to use pictures and picture watermarks with hidden text information in Vue
  • Vue implements page watermark function
  • Vue implements adding watermark effect to the page

<<:  Example analysis of the page splitting principle of MySQL clustered index

>>:  Detailed explanation of linux crm deployment code

Recommend

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

vue-cli4.5.x quickly builds a project

1. Install vue-cli npm i @vue/cli -g 2. Create a ...

In-depth explanation of currying of JS functions

Table of contents 1. Supplementary knowledge poin...

Detailed analysis of mysql MDL metadata lock

Preface: When you execute a SQL statement in MySQ...

Detailed explanation of Nginx current limiting configuration

This article uses examples to explain the Nginx c...

Analysis of JavaScript's event loop mechanism

Table of contents Preface: 1. Reasons for the eve...

Compatibility with the inline-block property

<br />A year ago, there were no articles abo...

Detailed explanation of html printing related operations and implementation

The principle is to call the window.print() metho...

7 Best VSCode Extensions for Vue Developers

Adding the right VS Code extension to Visual Stud...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

Several ways to easily traverse object properties in JS

Table of contents 1. Self-enumerable properties 2...

Flex layout realizes the layout mode of upper and lower fixed and middle sliding

This article mainly introduces the layout method ...

In-depth understanding of React Native custom routing management

Table of contents 1. Custom routing 2. Tab naviga...