Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture screenshot with Vue.js

I followed the https://www.digitalocean.com/community/tutorials/vuejs-screenshot-ui steps to capture a screenshot using Vue.js. The screenshot is taken successfully, but the screenshot dimensions seem not to be correct.

Problem:

  • The output should be the screenshot should be the selected one from the cursor. The output and the selected box from the cursor are different.

Issue: -I think, the issue is with the takeScreenshot method

takeScreenshot: function () {
  html2canvas(document.querySelector('body')).then(canvas => {
    let croppedCanvas = document.createElement('canvas'),
        croppedCanvasContext = croppedCanvas.getContext('2d');

    croppedCanvas.width  = this.boxEndWidth;
    croppedCanvas.height = this.boxEndHeight;

    croppedCanvasContext.drawImage(canvas, this.boxLeft, this.boxTop, 
    this.boxEndWidth, this.boxEndHeight, 0, 0, this.boxEndWidth, 
    this.boxEndHeight);

    this.imageUrl = croppedCanvas.toDataURL();
  
    const screenshotImg = document.getElementById('screenshotImg');
  
    screenshotImg.src= this.imageUrl;
    console.log('imageUrl', this.imageUrl)
  });
}

Would love to know if there's a fix or any other better way to take a screenshot. Thank you.

Codepen Link: https://codepen.io/dineshmadanlal/pen/MWjeXBQ

like image 923
Dinesh Madanlal Avatar asked Dec 08 '20 12:12

Dinesh Madanlal


People also ask

How do I screenshot on Vue JS?

screenshot-img CSS class with height and width both set at 100%. As described here, if you specify a 100% size for your image it will then take 100% of its container. Instead, the thing you want is keeping your screenshot at its original size, so do not specify any size for your image CSS should be enough.

Why vue JS?

VueJS is primarily used to build web interfaces and one-page applications. In saying that, it can also be applied to both desktop and mobile app development thanks to the HTML extensions and JS base working in tandem with an Electron framework – making it a heavily favoured frontend tool.


1 Answers

Looking at your original CodePen, I noticed that you are defining a .screenshot-img CSS class with height and width both set at 100%.

As described here, if you specify a 100% size for your image it will then take 100% of its container. Instead, the thing you want is keeping your screenshot at its original size, so do not specify any size for your image CSS should be enough.

I slightly modified your CodePen with this one, where the screenshot seems working as you're expecting.

like image 148
P3trur0 Avatar answered Sep 19 '22 10:09

P3trur0