I want to implement ctrl+c event in canvas (like in painters) from selected region to OS buffer. Based on this answer I can add copy
listener and change clipboard data there - which works perfectly with text. But I can't find how to put Image
/ImageData
object there there. Here's MDN copy documentation and setData. Seems like there is nothing about image/*
format. Well Specification doesn't say anything as well. But I smell if first param named as format
in setData
there should be a way to put file there.
Here's where I come so far:
document.addEventListener('copy', function(e) {
var data = ctx.getImageData(params.left, params.top, params.width, params.height);
var file = new File(data.data, "file.png", {type: "image/png"});
e.clipboardData.items.add(file, 'image/png'); // This doesn't work, But it create the structure like on the image below(with items and types, but without FileList)
e.clipboardData.setDragImage(tool.img, 10, 10); // doesn't work
e.clipboardData.setData("image/png", tool.file); // doesn't work
e.preventDefault();
})
I also found setDragImage method, I implemented it but after putting Image
it doesn't appear in buffer.
NOTE:
When I paste image FROM clipboard my 'paste' event shows event structure like on the image bellow, so I guess I need create something similar.
Any Ideas?
p.s. I also know about document.execCommand('copy');, but it doesn't work in chrome (at least in my case) so I don't wanna use it.