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:
|
<<: MySQL database introduction: detailed explanation of database backup operation
>>: Detailed explanation of the actual process of master-slave synchronization of MySQL database
FIFO communication (first in first out) FIFO name...
Two cases: 1. With index 2. Without index Prerequ...
Analyze the execution process. Move the mouse int...
1) Scope of application: readonly:input[type="...
Table of contents Why use Docker? Docker installa...
To do MySQL performance adjustment and service st...
Index merging is an intelligent algorithm provide...
1. Download mysql-8.0.17-winx64 from the official...
The LIKE operator is used in the WHERE clause to ...
Table of contents Some basic instructions 1. Chec...
Table of contents Effect display Component Settin...
As a super rookie, I just started learning MySQL ...
A CSS layout and style question: how to balance h...
Database stored procedures DROP PROCEDURE IF EXIS...
1. To download the MySQL database, visit the offi...