Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Use canvas.drawWindow when it is available
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Feb 17, 2017
1 parent 4a913d7 commit b2c3b5a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
31 changes: 18 additions & 13 deletions webextension/background/takeshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ window.takeshot = (function () {
let { captureType, captureText, scroll, selectedPos, shotId, shot } = options;
shot = new Shot(main.getBackend(), shotId, shot);
shot.deviceId = auth.getDeviceId();
screenshotPage(selectedPos, scroll).then((dataUrl) => {
shot.addClip({
createdDate: Date.now(),
image: {
url: dataUrl,
captureType,
text: captureText,
location: selectedPos,
dimensions: {
x: selectedPos.right - selectedPos.left,
y: selectedPos.bottom - selectedPos.top
let capturePromise = Promise.resolve();
if (! shot.clipNames().length) {
// canvas.drawWindow isn't available, so we fall back to captureVisibleTab
capturePromise = screenshotPage(selectedPos, scroll).then((dataUrl) => {
shot.addClip({
createdDate: Date.now(),
image: {
url: dataUrl,
captureType,
text: captureText,
location: selectedPos,
dimensions: {
x: selectedPos.right - selectedPos.left,
y: selectedPos.bottom - selectedPos.top
}
}
}
});
});
}
capturePromise.then(() => {
return uploadShot(shot);
}).then(() => {
let id = makeUuid();
Expand All @@ -33,7 +39,6 @@ window.takeshot = (function () {
message: "The link to your shot has been copied to the clipboard"
});
chrome.tabs.create({url: shot.viewUrl});
console.log("Accomplished my goal", JSON.stringify(shot.asJson()).length);
}).catch((e) => {
// FIXME: report
console.error("Error uploading shot:", e);
Expand Down
43 changes: 41 additions & 2 deletions webextension/selector/shooter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals callBackground, documentMetadata, uicontrol, util */
/* globals callBackground, documentMetadata, uicontrol, util, ui */
/* globals XMLHttpRequest, window, location, alert, console, domainFromUrl, randomString */
/* globals document, setTimeout, location */

Expand All @@ -14,8 +14,47 @@ window.shooter = (function () { // eslint-disable-line no-unused-vars
uicontrol.deactivate();
};

function screenshotPage(selectedPos) {
let height = selectedPos.bottom - selectedPos.top;
let width = selectedPos.right - selectedPos.left;
let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
canvas.width = width * window.devicePixelRatio;
canvas.height = height * window.devicePixelRatio;
let ctx = canvas.getContext('2d');
if (! ctx.drawWindow) {
return null;
}
if (window.devicePixelRatio !== 1) {
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}
ui.iframe.hide();
try {
ctx.drawWindow(window, selectedPos.left, selectedPos.top, width, height, "#fff");
} finally {
ui.iframe.unhide();
}
return canvas.toDataURL();
}

exports.takeShot = function (captureType, selectedPos) {
selectedPos = selectedPos.asJson();
let captureText = util.captureEnclosedText(selectedPos);
let dataUrl = screenshotPage(selectedPos);
if (dataUrl) {
shot.addClip({
createdDate: Date.now(),
image: {
url: dataUrl,
captureType,
text: captureText,
location: selectedPos,
dimensions: {
x: selectedPos.right - selectedPos.left,
y: selectedPos.bottom - selectedPos.top
}
}
});
}
callBackground("takeShot", {
captureType,
captureText,
Expand All @@ -25,7 +64,7 @@ window.shooter = (function () { // eslint-disable-line no-unused-vars
innerHeight: window.innerHeight,
innerWidth: window.innerWidth
},
selectedPos: selectedPos.asJson(),
selectedPos,
shotId: shot.id,
shot: shot.asJson()
});
Expand Down
8 changes: 8 additions & 0 deletions webextension/selector/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ window.ui = (function () { // eslint-disable-line no-unused-vars
});
},

hide: function () {
this.element.style.display = "none";
},

unhide: function () {
this.element.style.display = "";
},

updateElementSize: function (force) {
// Note: if someone sizes down the page, then the iframe will keep the
// document from naturally shrinking. We use force to temporarily hide
Expand Down

0 comments on commit b2c3b5a

Please sign in to comment.