im working on react web app and one of the feature needs to be implemented is to copy an image when clicked, so the user could paste it in: paint, word, etc...
i tried several approaches, first was to follow the instructions detailed in this post: https://mcmap.net/q/326485/-copy-image-to-clipboard
this is what i came up with (the containerId refers to a div element which contains an image element as its first child):
copyImg = (containerId) => {
const imgContainerElement = document.getElementById(containerId);
this.selectText(imgContainerElement.children[0]);
document.execCommand('copy');
window.getSelection().removeAllRanges();
alert('image copied!');
}
selectText = (element) => {
var doc = document;
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
didn't work. I tried implement the solution marked with a 2 stars here: https://www.tek-tips.com/viewthread.cfm?qid=833917
function copyImg(imgId){
var r = document.body.createControlRange();
r.add(document.getElementById(imgId));
r.select();
r.execCommand("COPY");
}
but the createControlRange() is undefined.
i tried using the navigator.clipboard api but it only works with png, and the app works with jpg.
i looked for an npm library that can accomplish this, but all i found was for text-copying. npms like: react-copy-to-clipboard
any help would be appreciated.
Edit 1:
following dw_https://mcmap.net/q/2000211/-react-how-to-copy-an-image-to-clipboard instructions this is what i came up with: (note: i had to npm install babel-polyfill and import it in App.js, in order to make the async function to work and pass this error: regeneratorRuntime is not defined)
copyImg = async (imgElementId) => {
const imgElement = document.getElementById(imgElementId);
const src = imgElement.src;
const img = await fetch(src);
const imgBlob = await img.blob();
if (src.endsWith(".jpg") || src.endsWith(".jpeg")) {
copyService.convertToPng(imgBlob);
} else if (src.endsWith(".png")) {
copyService.copyToClipboard(imgBlob);
} else {
console.error("Format unsupported");
}
}
convertToPng = (imgBlob) => {
const imageUrl = window.URL.createObjectURL(imgBlob);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const imageEl = createImage({ src: imageUrl });
imageEl.onload = (e) => {
canvas.width = e.target.width;
canvas.height = e.target.height;
ctx.drawImage(e.target, 0, 0, e.target.width, e.target.height);
canvas.toBlob(copyToClipboard, "image/png", 1);
};
}
createImage = (options) => {
options = options || {};
const img = (Image) ? new Image() : document.createElement("img");
if (options.src) {
img.src = options.src;
}
return img;
}
copyToClipboard = (pngBlob) => {
try {
navigator.clipboard.write([
new ClipboardItem({
[pngBlob.type]: pngBlob
})
]);
console.log("Image copied");
} catch (error) {
console.error(error);
}
}
the code reaches to the Image copied message, but still when paste it on word it does not shown. anther thing is that i get
console error: Uncaught (in promise) DOMException
copyToClipboard
tocopyService.copyToClipboard
, andcreateImage({ src: imageUrl });
tocopyService.createImage({ src: imageUrl });
– BrittainUncaught in promise
error? – Brittain