Cropping partial screenshot of Chrome for Retina Display

I made a chrome extension that captures one element (div) of a website.

I used chrome.tabs> captureVisibleTab to take a screenshot. Then, with the coordinates (x / y) and the dimensions (width / height) of the element (div), I crop the screenshot.

This works great for me on non-mesh displays. But not so on a Macbook with a Retina display.

For example, on www.247activemedia.com we want to capture the div header with the logo (id = "header").

According to the result without a retina: enter image description here

On a Macbook with Retina display:

enter image description here

Crop failed and incorrect result.

Here is the code:

chrome.tabs.captureVisibleTab(tab.windowId, { format: "png" }, function(screenshot) {
            if (!canvas) {
                canvas = document.createElement("canvas");
                document.body.appendChild(canvas);
            }
            var partialImage = new Image();
            partialImage.onload = function() {
                canvas.width = dimensions.width;
                canvas.height = dimensions.height;
                var context = canvas.getContext("2d");
                context.drawImage(
                    partialImage,
                    dimensions.left,
                    dimensions.top,
                    dimensions.width,
                    dimensions.height,
                    0,
                    0,
                    dimensions.width,
                    dimensions.height
                );
                var croppedDataUrl = canvas.toDataURL("image/png");
                chrome.tabs.create({
                    url: croppedDataUrl,
                    windowId: tab.windowId
                });
            }
            partialImage.src = screenshot;

        });
Run codeHide result

How can I fix this for Retina displays?

+4
2

, @gui47 - window.devicePixelRatio, 2 MBP

0

, .

let ratio = window.devicePixelRatio;
context.drawImage(image,
    dimensions.left*ratio, dimensions.top*ratio,
    dimensions.width*ratio, dimensions.height*ratio,
    0, 0,
    dimensions.width, dimensions.height
);

CanvasAPI: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage

0

All Articles