Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use copyImageAt instead of convertImageToBase64 #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions src/context-menu-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ export default class ContextMenuBuilder {
* Adds "Copy Image" and "Copy Image URL" items when `src` is valid.
*/
addImageItems(menu, menuInfo) {
let target = this.getWebContents();
let copyImage = new MenuItem({
label: this.stringTable.copyImage(),
click: () => this.convertImageToBase64(menuInfo.srcURL,
(dataURL) => clipboard.writeImage(nativeImage.createFromDataURL(dataURL)))
click: () => target.copyImageAt(menuInfo.x, menuInfo.y)
});

menu.append(copyImage);
Expand Down Expand Up @@ -406,30 +406,4 @@ export default class ContextMenuBuilder {
menu.append(inspect);
return menu;
}

/**
* Converts an image to a base-64 encoded string.
*
* @param {String} url The image URL
* @param {Function} callback A callback that will be invoked with the result
* @param {String} outputFormat The image format to use, defaults to 'image/png'
*/
convertImageToBase64(url, callback, outputFormat='image/png') {
let canvas = document.createElement('CANVAS');
let ctx = canvas.getContext('2d');
let img = new Image();
img.crossOrigin = 'Anonymous';

img.onload = () => {
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);

let dataURL = canvas.toDataURL(outputFormat);
canvas = null;
callback(dataURL);
};

img.src = url;
}
}