Bug 959066 - Hi-res screenshot for getScreenshot API. r=smaug

This commit is contained in:
Tim Chien 2014-02-12 09:53:07 -05:00
Родитель 34f969356b
Коммит ba289d36b7
1 изменённых файлов: 24 добавлений и 10 удалений

Просмотреть файл

@ -758,17 +758,21 @@ BrowserElementChild.prototype = {
/**
* Actually take a screenshot and foward the result up to our parent, given
* the desired maxWidth and maxHeight, and given the DOMRequest ID associated
* with the request from the parent.
* the desired maxWidth and maxHeight (in CSS pixels), and given the
* DOMRequest ID associated with the request from the parent.
*/
_takeScreenshot: function(maxWidth, maxHeight, mimeType, domRequestID) {
// You can think of the screenshotting algorithm as carrying out the
// following steps:
//
// - Calculate maxWidth, maxHeight, and viewport's width and height in the
// dimension of device pixels by multiply the numbers with
// window.devicePixelRatio.
//
// - Let scaleWidth be the factor by which we'd need to downscale the
// viewport so it would fit within maxWidth. (If the viewport's width
// is less than maxWidth, let scaleWidth be 1.) Compute scaleHeight
// the same way.
// viewport pixel width so it would fit within maxPixelWidth.
// (If the viewport's pixel width is less than maxPixelWidth, let
// scaleWidth be 1.) Compute scaleHeight the same way.
//
// - Scale the viewport by max(scaleWidth, scaleHeight). Now either the
// viewport's width is no larger than maxWidth, the viewport's height is
@ -794,13 +798,23 @@ BrowserElementChild.prototype = {
return;
}
let scaleWidth = Math.min(1, maxWidth / content.innerWidth);
let scaleHeight = Math.min(1, maxHeight / content.innerHeight);
let devicePixelRatio = content.devicePixelRatio;
let maxPixelWidth = Math.round(maxWidth * devicePixelRatio);
let maxPixelHeight = Math.round(maxHeight * devicePixelRatio);
let contentPixelWidth = content.innerWidth * devicePixelRatio;
let contentPixelHeight = content.innerHeight * devicePixelRatio;
let scaleWidth = Math.min(1, maxPixelWidth / contentPixelWidth);
let scaleHeight = Math.min(1, maxPixelHeight / contentPixelHeight);
let scale = Math.max(scaleWidth, scaleHeight);
let canvasWidth = Math.min(maxWidth, Math.round(content.innerWidth * scale));
let canvasHeight = Math.min(maxHeight, Math.round(content.innerHeight * scale));
let canvasWidth =
Math.min(maxPixelWidth, Math.round(contentPixelWidth * scale));
let canvasHeight =
Math.min(maxPixelHeight, Math.round(contentPixelHeight * scale));
let transparent = (mimeType !== 'image/jpeg');
@ -812,7 +826,7 @@ BrowserElementChild.prototype = {
canvas.height = canvasHeight;
var ctx = canvas.getContext("2d", { willReadFrequently: true });
ctx.scale(scale, scale);
ctx.scale(scale * devicePixelRatio, scale * devicePixelRatio);
ctx.drawWindow(content, 0, 0, content.innerWidth, content.innerHeight,
transparent ? "rgba(255,255,255,0)" : "rgb(255,255,255)");