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

Six-step example code for JDBC connection (connecting to MySQL)

Six steps of JDBC: 1. Register the driver 2. Get ...

Native JS to achieve cool paging effect

This article uses an example to share with you a ...

Grid systems in web design

Formation of the grid system In 1692, the newly c...

How to run Hadoop and create images in Docker

Reinventing the wheel, here we use repackaging to...

Detailed explanation of Vue's props configuration

<template> <div class="demo"&g...

A brief discussion on CSS cascading mechanism

Why does CSS have a cascading mechanism? Because ...

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

The marquee tag in HTML achieves seamless scrolling marquee effect

The <marquee> tag is a tag that appears in ...

Use Docker Compose to quickly deploy ELK (tested and effective)

Table of contents 1. Overview 1.1 Definition 1.2 ...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

Vue implements calling PC camera to take photos in real time

Vue calls the PC camera to take pictures in real ...