Do you know how to use Vue to take screenshots of web pages?

Do you know how to use Vue to take screenshots of web pages?

1. Install html2Canvas

npm install html2canvas --save

2. Introduce into the required Vue component

import 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:

Summarize

This 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:
  • How to use Vue to generate pictures from HTML pages
  • How to use Vue to generate pictures from HTML pages
  • How to use canvas in Vue to realize the synthesis of QR code and picture poster
  • How to turn HTML elements into canvas (poster generation) in Vue3 to save/screenshot pictures

<<:  Boundary and range description of between in mysql

>>:  Solve the docker.socket permission problem of vscode docker plugin

Recommend

Common rule priority issues of Nginx location

Table of contents 1. Location / Matching 2. Locat...

Manually install mysql5.7.10 on Ubuntu

This tutorial shares the process of manually inst...

Book page turning effects made with CSS3

Result:Implementation code: html <!-- Please h...

Supplementary article on front-end performance optimization

Preface I looked at the previously published arti...

MySQL 5.7.23 decompression version installation tutorial with pictures and text

It is too troublesome to find the installation tu...

Solution to the conflict between Linux kernel and SVN versions

Phenomenon The system could compile the Linux sys...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

Detailed explanation of Nginx regular expressions

Nginx (engine x) is a high-performance HTTP and r...

Detailed explanation of Kubernetes pod orchestration and lifecycle

Table of contents K8S Master Basic Architecture P...

MySQL query statement grouped by time

MySQL query by year, month, week, day group 1. Qu...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...