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

How to write HTML head in mobile device web development

Copy code The code is as follows: <head> &l...

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

Ten useful and simple MySQL functions

function 0. Display current time Command: select ...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

MySQL uses binlog logs to implement data recovery

MySQL binlog is a very important log in MySQL log...

Implementation of MySQL Shell import_table data import

Table of contents 1. Introduction to import_table...

Use of hasOwnProperty method of js attribute object

Object's hasOwnProperty() method returns a Bo...

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts ...

【HTML element】Detailed explanation of tag text

1. Use basic text elements to mark up content Fir...

Mysql varchar type sum example operation

Some friends, when learning about databases, acci...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to...

mysql error number 1129 solution

SQLyog connects to mysql error number 1129: mysql...