1. Install html2Canvasnpm install html2canvas --save 2. Introduce into the required Vue componentimport html2canvas from "html2canvas"; 3. Write a screenshot button<el-button class="button-dalod" size="mini" title="Generate image" @click="toImage()" icon="el-icon-download"></el-button> 4. Call the toImage function// Convert page elements to images toImage () { // Manually create a canvas tag const canvas = document.createElement("canvas") // Get the parent tag, which means the DOM element in this tag generates the image // imageTofile is a custom ref name for the parent element within the screenshot range let canvasBox = this.$refs.imageTofile // Get the width and height of the parent const width = parseInt(window.getComputedStyle(canvasBox).width) const height = parseInt(window.getComputedStyle(canvasBox).height) // Width and height * 2 and enlarged by 2 times is to prevent the image from being blurred canvas.width = width * 2 canvas.height = height * 2 canvas.style.width = width + 'px' canvas.style.height = height + 'px' const context = canvas.getContext("2d"); context.scale(2, 2); const options = { backgroundColor: null, canvas: canvas, useCORS: true } html2canvas(canvasBox, options).then((canvas) => { // toDataURL image format converted to base64 let dataURL = canvas.toDataURL("image/png") console.log(dataURL) this.downloadImage(dataURL) }) }, //Download the image downloadImage(url) { // If it is on a web page, you can directly create an a tag to download directly let a = document.createElement('a') a.href = url a.download = 'Home Screenshot' a.click() }, Don't forget to add the ref attribute to the parent of the page within the screenshot range, so that canvas can find the parent and calculate the width and height to take a screenshot. This is the screenshot result: SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Boundary and range description of between in mysql
>>: Solve the docker.socket permission problem of vscode docker plugin
Copy code The code is as follows: <head> &l...
We will install phpMyAdmin to work with Apache on...
function 0. Display current time Command: select ...
One sentence to introduce HOC What is a higher-or...
MySQL binlog is a very important log in MySQL log...
Table of contents 1. Introduction to import_table...
Object's hasOwnProperty() method returns a Bo...
The storage size and range of each floating point...
In the previous article, we played with timeouts ...
1. Use basic text elements to mark up content Fir...
Some friends, when learning about databases, acci...
Table of contents forEach() Method How to jump ou...
Preface Readers who are familiar with MySQL may f...
To deploy multiple sites on a server, you need to...
SQLyog connects to mysql error number 1129: mysql...