Vue easily realizes watermark effect

Vue easily realizes watermark effect

Preface:

Use watermark effect in vue project, you can specify the container

Effect picture:

1. Do not specify a container

2. Specify the container

Implementation method:

1. Create a new configuration file watermark.js, which can be placed in util or somewhere else

let watermark = {}
 
let setWatermark = (text, sourceBody) => {
  let id = Math.random()*10000+'-'+Math.random()*10000+'/'+Math.random()*10000
 
  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }
 
  let can = document.createElement('canvas')
  can.width = 150
  can.height = 120
 
  let cans = can.getContext('2d')
  cans.rotate(-20 * Math.PI / 180)
  cans.font = '15px Vedana'
  cans.fillStyle = 'rgba(0, 0, 0, .5)'
  cans.textAlign = 'left'
  cans.textBaseline = 'Middle'
  cans.fillText(text, can.width / 20, can.height )
 
  let water_div = document.createElement('div')
  water_div.id = id
  water_div.style.pointerEvents = 'none'
  water_div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  if(sourceBody){
    water_div.style.width = '100%'
    water_div.style.height = '100%'
    sourceBody.appendChild(water_div)
  }else{
    water_div.style.top = '3px'
    water_div.style.left = '0px'
    water_div.style.position = 'fixed'
    water_div.style.zIndex = '100000'
    water_div.style.width = document.documentElement.clientWidth + 'px'
    water_div.style.height = document.documentElement.clientHeight + 'px'
    document.body.appendChild(water_div)
  }
 
  return id
}
 
/**
 * This method can only be called once * @param:
 * @text == watermark content * @sourceBody == where the watermark is added, if not passed, it is body
 * */
watermark.set = (text, sourceBody) => {
  let id = setWatermark(text, sourceBody)
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(text, sourceBody)
    }
  }, 2000)
  window.onresize = () => {
    setWatermark(text, sourceBody)
  }
}
 
export default watermark

2. Global configuration in main.js

// Watermarkimport watermark from './utils/watermark.js'
Vue.prototype.$watermark = watermark

3. Use full screen watermark in the page

this.$watermark.set("Haoxing 2731")

4. Use-specified container in the page

<el-button @click="addWatermark">Click me to add watermark</el-button>
<div ref="content" style="width: 500px;height: 500px;border: 1px solid #ccc;">
 addWatermark(){
      this.$watermark.set("Haoxing 2731",this.$refs.content)
    }

5. If you think the distance between fonts is too large, just change this property

can.width = 150
can.height = 120

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • The vue project realizes drawing a watermark in a certain area
  • Vue's global watermark implementation example
  • 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

<<:  MySQL Database Basics: A Summary of Basic Commands

>>:  Summary of 10 common HBase operation and maintenance tools

Recommend

The whole process record of Vue export Excel function

Table of contents 1. Front-end leading process: 2...

HTML basic summary recommendation (text format)

HTML text formatting tags 標簽 描述 <b> 定義粗體文本 ...

A simple example of mysql searching for data within N kilometers

According to the coefficient of pi and the radius...

Nginx stream configuration proxy (Nginx TCP/UDP load balancing)

Prelude We all know that nginx is an excellent re...

Three Ways to Find the Longest Word in a String in JavaScript (Recommended)

This article is based on the Free Code Camp Basic...

A brief understanding of the relevant locks in MySQL

This article is mainly to take you to quickly und...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Working principle and example analysis of Linux NFS mechanism

What is NFS? network file system A method or mech...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

Pessimistic locking and optimistic locking in MySQL

In relational databases, pessimistic locking and ...

MySQL 8.0.18 deployment and installation tutorial under Windows 7

1. Preliminary preparation (windows7+mysql-8.0.18...

Nginx access log and error log parameter description

illustrate: There are two main types of nginx log...