I am trying to get a screenshot of a page using chrome extension and trying to crop the image. Upon taking the screenshot, I get a base64 dataUrl of the image.
When I try to render the image using the dataURL, the image renders larger(appears zoomed in) than the original image. When I manually give the <img>
object the original size it renders fine. But when I draw this image to a canvas, the canvas takes the enlarged size.
I have been banging my head against the wall to get a solution for this problem but to no avail. Any help is greatly appreciated.
//Here is the JS code:
var image = document.getElementById("image");
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
image.onload = function() {
var sourceX = 0;
var sourceY = 0;
var sourceWidth = 150;
var sourceHeight = 80;
var destWidth = sourceWidth;
var destHeight = sourceHeight;
var destX = 0;
var destY = 0;
context.drawImage(image, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
};
And here is the HTML code, I am removing the dataurl as it takes too much space, and pasting another URL.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<img height = "678" width = "1280" id = "image" src = "http://s12.postimg.org/7r9q3h2vx/ss2.jpg"/>
<canvas id="myCanvas" width="500" height="200"></canvas>
</body>
</html>
The following is the image(from the dataurl) I want to crop:
The following is the cropped image that I get. It is not the intended image because it appears enlarged(zoomed in). It is the top left section of the original image.
For further reference, here is the jsbin link: https://jsbin.com/pomayowara/edit?output