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

Specific use of MySQL binlog_ignore_db parameter

Preface: After studying the previous article, we ...

How to use MySQL common functions to process JSON

Official documentation: JSON Functions Name Descr...

CSS float property diagram float property details

Using the CSS float property correctly can become...

Install redis and MySQL on CentOS

1|0MySQL (MariaDB) 1|11. Description MariaDB data...

A brief discussion on the implementation of fuzzy query using wildcards in MySQL

In the MySQL database, when we need fuzzy query, ...

Vue implements mobile phone verification code login

This article shares the specific code of Vue to i...

Html comments Symbols for marking text comments in Html

HTML comments, we often need to make some HTML co...

How to use filters to implement monitoring in Zabbix

Recently, when I was working on monitoring equipm...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Methods for optimizing Oracle database with large memory pages in Linux

Preface PC Server has developed to this day and h...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

HTML background color gradient achieved through CSS

Effect screenshots: Implementation code: Copy code...

MySQL stored procedure in, out and inout parameter examples and summary

Stored Procedures 1. Create a stored procedure an...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...