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

Implementation of FIFO in Linux process communication

FIFO communication (first in first out) FIFO name...

Will Update in a Mysql transaction lock the table?

Two cases: 1. With index 2. Without index Prerequ...

Comparison of the use of form element attributes readonly and disabled

1) Scope of application: readonly:input[type="...

About ROS2 installation and docker environment usage

Table of contents Why use Docker? Docker installa...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

How to use mysql index merge

Index merging is an intelligent algorithm provide...

mysql-8.0.17-winx64 deployment method

1. Download mysql-8.0.17-winx64 from the official...

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to ...

Some basic instructions of docker

Table of contents Some basic instructions 1. Chec...

Detailed explanation of custom swiper component in JavaScript

Table of contents Effect display Component Settin...

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance h...

MySQL download and installation details graphic tutorial

1. To download the MySQL database, visit the offi...