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-croppernpm 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.jsimport VueCropper from 'vue-cropper' Vue.use(VueCropper) 3. Introducing html2canvas into the pageimport html2canvas from "html2canvas" export default{ } 4. Code Decomposition1. 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> RenderingSummarizeThis 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:
|
<<: Detailed explanation of the problem of mixed use of limit and sum functions in MySQL
>>: Linux traceroute command usage detailed explanation
Six steps of JDBC: 1. Register the driver 2. Get ...
Let me first explain why the text is not vertical...
This article uses an example to share with you a ...
Formation of the grid system In 1692, the newly c...
Reinventing the wheel, here we use repackaging to...
<template> <div class="demo"&g...
Why does CSS have a cascading mechanism? Because ...
1. Download MySQL Community Server 5.7.16 and ins...
1. System Configuration 1. Turn off sudo password...
The <marquee> tag is a tag that appears in ...
Table of contents 1. Overview 1.1 Definition 1.2 ...
1. Go to the official website www.mysql.com and s...
Table of contents summary Overall process front e...
This article shares with you the graphic tutorial...
Vue calls the PC camera to take pictures in real ...