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
This article describes MySQL 8.0 user and role ma...
Nginx can generally be used for seven-layer load ...
mysql between boundary range The range of between...
I plan to use C/C++ to implement basic data struc...
When Mysql occupies too much CPU, where should we...
Click on the anchor link to scroll smoothly and a...
The information on Baidu is so diverse that it...
background In the group, some students will ask r...
1. Usage of instanceof instanceof operator is use...
What Beautiful HTML Code Looks Like How to write ...
"Grand" are probably the two words that ...
Deployment environment: Installation version red ...
Someone asked me before whether it is possible to...
This article shares the installation tutorial of ...
Add secure_file_priv = ' '; then run cmd ...