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
Table of contents 1. Location / Matching 2. Locat...
Table of contents 1. User created script 2. Word ...
This tutorial shares the process of manually inst...
Result:Implementation code: html <!-- Please h...
Preface I looked at the previously published arti...
It is too troublesome to find the installation tu...
Phenomenon The system could compile the Linux sys...
Table of contents Thoughts triggered by an online...
1. pc-reset PC style initialization /* normalize....
Nginx (engine x) is a high-performance HTTP and r...
Table of contents K8S Master Basic Architecture P...
MySQL query by year, month, week, day group 1. Qu...
Preface: Fully encapsulating a functional module ...
Time fields are often used in database usage. Com...
1. Understanding of transition attributes 1. The ...