The vue project realizes drawing a watermark in a certain area

The vue project realizes drawing a watermark in a certain area

This article shares with you how to use Vue to draw a watermark on a certain area for your reference. The specific content is as follows

First, let’s take a look at the effect:

In fact, the principle is very simple, that is, use canvas to draw a picture, and then set the background of div. Here, I refer to other people's ideas and encapsulate a plug-in according to my own needs. It can be used directly in the project. Here you can set a watermark for a separate area:

'use strict'
 
const watermark = {}
 
/**
 *
 * @param {the content of the watermark to be set} str
 * @param {Container where watermark needs to be set} container
 */
const setWatermark = (str, container) => {
  const id = '1.23452384164.123412415'
 
  if (container === undefined) {
    return
  }
 
  // Check if there is one on the page, if so delete it if (document.getElementById(id) !== null) {
    const childelement = document.getElementById(id)
    childelement.parentNode.removeChild(childelement)
  }
 
  var containerWidth = container.offsetWidth // Get the width of the parent container var containerHeight = container.offsetHeight // Get the height of the parent container container.style.position = 'relative' // Set the layout to relative layout // Create a canvas element (make a background image first)
  const can = document.createElement('canvas')
  can.width = 390 // Set the width of each block can.height = 200 // Height const cans = can.getContext('2d') // Get the canvas cans.rotate(-20 * Math.PI / 180) // Rotate counterclockwise π/9
  cans.font = '20px Vedana' // Set the font cans.fillStyle = 'rgba(200, 200, 200, 0.20)' // Set the font color cans.textAlign = 'left' // Text alignment cans.textBaseline = 'Middle' // Text baseline cans.fillText(str, 0, 4 * can.height / 5) // Draw text // Create a div element const div = document.createElement('div')
  div.id = id // Set id
  div.style.pointerEvents = 'none' // Cancel all events div.style.top = '0px'
  div.style.left = '0px'
  div.style.position = 'absolute'
  div.style.zIndex = '100000'
  div.style.width = containerWidth + 'px'
  div.style.height = containerHeight + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  container.appendChild(div) // Append to page return id
}
 
// This method can only be called once watermark.set = (str, container) => {
  let id = setWatermark(str, container)
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str, container)
    }
  }, 500)
  // Listen for changes in page size window.onresize = () => {
    setWatermark(str, container)
  }
}
 
export default watermark

How to use it on the page:

Import the plugin:

import Watermark from '@/external/watermark'

Then set refs="xxx" at the required position, because in the Vue project, you cannot directly get the element by document.getElement, you can only get it through this.$refs.xxx:

<div ref="directrecordwp" class="wrapper">

Then write this in the mounted hook function:

// Set the page watermark Watermark.set('College Party and Government Cloud Record Management Platform' + this.name, this.$refs.directrecordwp)

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:
  • Vue easily realizes watermark effect
  • 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 introduction: detailed explanation of database backup operation

>>:  Detailed explanation of the actual process of master-slave synchronization of MySQL database

Recommend

MySQL 8.0 user and role management principles and usage details

This article describes MySQL 8.0 user and role ma...

Boundary and range description of between in mysql

mysql between boundary range The range of between...

VSCode+CMake+Clang+GCC environment construction tutorial under win10

I plan to use C/C++ to implement basic data struc...

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

vue.config.js packaging optimization configuration

The information on Baidu is so diverse that it...

Use CSS to easily implement some frequently appearing weird buttons

background In the group, some students will ask r...

JavaScript manual implementation of instanceof method

1. Usage of instanceof instanceof operator is use...

How to write beautiful HTML code

What Beautiful HTML Code Looks Like How to write ...

How to create a stylish web page design (graphic tutorial)

"Grand" are probably the two words that ...

How to correctly modify the ROOT password in MySql8.0 and above versions

Deployment environment: Installation version red ...

How to dynamically add a volume to a running Docker container

Someone asked me before whether it is possible to...

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...

Solve the problem of secure_file_priv null

Add secure_file_priv = ' '; then run cmd ...