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

10 skills that make front-end developers worth millions

The skills that front-end developers need to mast...

How to change the encoding of MySQL database to utf8mb4

The utf8mb4 encoding is a superset of the utf8 en...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

Methods for defragmenting and reclaiming space in MySQL tables

Table of contents Causes of MySQL Table Fragmenta...

Linux uses iptables to limit multiple IPs from accessing your server

Preface In the Linux kernel, netfilter is a subsy...

Example of how to mosaic an image using js

This article mainly introduces an example of how ...

JS ES6 asynchronous solution

Table of contents Initially using the callback fu...

Detailed explanation of views in MySQL

view: Views in MySQL have many similarities with ...

Linux editing start, stop and restart springboot jar package script example

Preface In the springboot configuration file, the...

v-html rendering component problem

Since I have parsed HTML before, I want to use Vu...

How to change the domestic image source for Docker

Configure the accelerator for the Docker daemon S...

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Html page supports dark mode implementation

Since 2019, both Android and IOS platforms have s...