A simple way to implement Vue's drag screenshot function

A simple way to implement Vue's drag screenshot function

Drag the mouse to take a screenshot of the page (you can also drag the screenshot in a specified area)

1. Install html2canvas and vue-cropper

npm i html2canvas --save //Used to convert the specified area into an image npm i vue-cropper -S //Crop the image

2. Register the vue-cropper component in main.js

import VueCropper from 'vue-cropper'
Vue.use(VueCropper)

3. Introducing html2canvas into the page

  import html2canvas from "html2canvas"
  export default{
  }

4. Code Decomposition

1. Convert the specified area to a picture

this.$nextTick(()=>{
   html2canvas(document.body,{}).then(canvas => {    
     let dataURL = canvas.toDataURL("image/png");
     this.uploadImg = dataURL
     this.loading = true
   });
 })

Here, the entire body page is converted into an image to obtain base64 format data, and other areas directly obtain class or id

2. Drag the generated image to take a screenshot

<template>
	<div class="pop_alert" v-if="show">
	   <vueCropper
	      @mouseenter.native="enter"
	      @mouseleave.native="leave"
	      ref="cropper"
	      :img="uploadImg"
	      :outputSize="option.size"
	      :outputType="option.outputType"
	      :info="true"
	      :full="option.full"
	      :canMove="option.canMove"
	      :canMoveBox="option.canMoveBox"
	      :original="option.original"
	      :autoCrop="option.autoCrop"
	      :fixed="option.fixed"
	      :fixedNumber="option.fixedNumber"
	      :centerBox="option.centerBox"
	      :infoTrue="option.infoTrue"
	      :fixedBox="option.fixedBox"
	      style="background-image:none"
	    ></vueCropper>
	    <div class="btn_box">
	    	<div @click="save">Confirm screenshot</div>
	   	    <div @click="close">Cancel</div>
	    </div>
	 </div>
 </template>
<script>
 export default{
   data(){
       option: {
          info: true, // size information of cropping boxoutputSize: 0.8, // quality of cropped imageoutputType: "jpeg", // format of cropped imagecanScale: false, // whether image allows scroll wheel scalingautoCrop: false, // whether to generate screenshot box by defaultfixedBox: false, // fixed screenshot box size cannot be changedfixed: false, // whether to enable fixed aspect ratio of screenshot boxfixedNumber: [7, 5], // aspect ratio of screenshot boxfull: true, // whether to output screenshot in original aspect ratiocanMove: false, // can original image be movedcanMoveBox: true, // whether screenshot box can be draggedoriginal: false, // uploaded image is rendered in original aspect ratiocenterBox: false, // whether screenshot box is limited to imageinfoTrue: true // true to show actual output image width and heightfalse to show screenshot box width and height},
        uploadImg:"",
        show:false
   },
   methods:{
     enter() {
       if (this.uploadImg == "") {
         return;
       }
       this.$refs.cropper.startCrop(); //Start cropping},
     leave() {
       this.$refs.cropper.stopCrop();//Stop cropping},
     save() { //Confirm the screenshot this.$refs.cropper.getCropData((data) => { //Get the screenshot's base64 format data console.log(data)
          this.show = false
        })
        // this.$refs.cropper.getCropBlob(data => { //Get the Blob format data of the screenshot// this.cutImg = data;
        // });
      },
      close(){ //Cancel this.show = false
      }
   }
 }
 </script>

5. All codes

<template>
   <div>
     <div @click="tailoring">Cut</div>
	<!--Continue to write other content of the page. Pop_alert can be encapsulated into a component for use-->
	
     <div class="pop_alert" v-if="show">
	   <vueCropper
	      @mouseenter.native="enter"
	      @mouseleave.native="leave"
	      ref="cropper"
	      :img="uploadImg"
	      :outputSize="option.size"
	      :outputType="option.outputType"
	      :info="true"
	      :full="option.full"
	      :canMove="option.canMove"
	      :canMoveBox="option.canMoveBox"
	      :original="option.original"
	      :autoCrop="option.autoCrop"
	      :fixed="option.fixed"
	      :fixedNumber="option.fixedNumber"
	      :centerBox="option.centerBox"
	      :infoTrue="option.infoTrue"
	      :fixedBox="option.fixedBox"
	      style="background-image:none"
	    ></vueCropper>
	    <div class="btn_box">
	    	<div @click="save">Confirm screenshot</div>
	   	    <div @click="close">Cancel</div>
	    </div>
	 </div>
   </div>
</template>
<script>
import html2canvas from "html2canvas"
 export default{
  data(){
   return {
     option: {
          info: true, // size information of cropping boxoutputSize: 0.8, // quality of cropped imageoutputType: "jpeg", // format of cropped imagecanScale: false, // whether image allows scroll wheel scalingautoCrop: false, // whether to generate screenshot box by defaultfixedBox: false, // fixed screenshot box size cannot be changedfixed: false, // whether to enable fixed aspect ratio of screenshot boxfixedNumber: [7, 5], // aspect ratio of screenshot boxfull: true, // whether to output screenshot in original aspect ratiocanMove: false, // can original image be movedcanMoveBox: true, // whether screenshot box can be draggedoriginal: false, // uploaded image is rendered in original aspect ratiocenterBox: false, // whether screenshot box is limited to imageinfoTrue: true // true to show actual output image width and heightfalse to show screenshot box width and height},
        uploadImg:"",
        show:false
   }
  },
  methods:{
    tailoring(){ //Cut this.$nextTick(()=>{
           html2canvas(document.body,{}).then(canvas => {
             let dataURL = canvas.toDataURL("image/png");
             this.uploadImg = dataURL
             this.show = true
           });
       })
    },
    enter() {
       if (this.uploadImg == "") {
         return;
       }
       this.$refs.cropper.startCrop(); //Start cropping},
     leave() {
       this.$refs.cropper.stopCrop();//Stop cropping},
     save() { //Confirm the screenshot this.$refs.cropper.getCropData((data) => { //Get the screenshot's base64 format data console.log(data)
          this.show = false
        })
        // this.$refs.cropper.getCropBlob(data => { //Get the Blob format data of the screenshot// this.cutImg = data;
        // });
      },
      close(){ //Cancel this.show = false
      }
   }
 }
</script>
<style>
	.pop_alert{
      width: 100%;
	  height: 100%;
	  position: absolute;
	  top: 0;
	  left: 0;
	  border: 1px dashed red;
	  background-color: #000000;
    }
    .btn_box{
        position: absolute;
	    top: 0;
	    color: red;
	    right: 0;
	    font-size: 30px;
	    display: flex;
        align-items: center;
	    z-index: 6666;
    }
</style>

Rendering

Summarize

This is the end of this article about the implementation of Vue drag screenshot function. For more relevant Vue drag screenshot function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Realizing drag effect based on Vue
  • Implementing drag and drop function based on Vue
  • Example of implementing drag and drop effect with Vue.js
  • Vue.js implements random dragging of pictures
  • Vue implements div drag and drop
  • Vue custom directive drag function example
  • Vue implements irregular screenshots

<<:  Detailed explanation of the problem of mixed use of limit and sum functions in MySQL

>>:  Linux traceroute command usage detailed explanation

Recommend

Detailed tutorial on installing MariaDB on CentOS 8

MariaDB database management system is a branch of...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

CentOS 7.x docker uses overlay2 storage method

Edit /etc/docker/daemon.json and add the followin...

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...

How to configure nginx to return text or json

Sometimes when requesting certain interfaces, you...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...

Docker installs Redis and introduces the visual client for operation

1 Introduction Redis is a high-performance NoSQL ...

Specific use of MySQL global locks and table-level locks

Table of contents Preface Global Lock Table lock ...

Summary of MySQL slow log practice

Slow log query function The main function of slow...

CSS3 custom scroll bar style::webkit-scrollbar sample code detailed explanation

The default scroll bar style in Windows is ugly, ...

Tutorial on using prepare, execute and deallocate statements in MySQL

Preface MySQL officially refers to prepare, execu...

Comprehensive analysis of isolation levels in MySQL

When the database concurrently adds, deletes, and...