-
Notifications
You must be signed in to change notification settings - Fork 27
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
Suggestion: copy image from ViewContainer #4
Comments
A similar (but not quite identical) bit of code is https://github.com/lynchjames/obsidian-mind-map/blob/main/src/copy-image.ts#L3 export function copyImageToClipboard(svg: SVGElement) {
const canvas = createCanvas(svg);
const img = generateImage(svg, canvas, () => {
canvas.toBlob((blob: any) => {
const item = new ClipboardItem({ "image/png": blob });
navigator.clipboard.write([item]);
new Notice('Screenshot copied to the clipboard.')
});
});
} |
Hi, you can try the following code, and I have tested it. export function copyImage(imgSrc: string) {
let image = new Image();
image.src = imgSrc;
image.crossOrigin = 'anonymous';
image.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
// const imgBase64 = canvas.toDataURL("image/png");
// console.log('imgBase64', imgBase64);
canvas.toBlob((blob: any) => {
const item = new ClipboardItem({ "image/png": blob });
navigator.clipboard.write([item]);
console.log('copy end!');
});
};
} |
@sissilab This is amazing! I spent an entire day trying to figure this out. Your code works and is so much more elegant than what I came up with. Thank you! I'll be incorporating this into NomarCub/obsidian-copy-url-in-preview#2 shortly... |
Glad to help you! |
@sissilab one possibly important note:
|
Thanks, I'll try it. ^v^ |
@sissilab This is a great plugin! Really slick.
I was trying to borrow some of your techniques to implement a "Copy Image" function (see this fork/PR: NomarCub/obsidian-copy-url-in-preview#2) but I don't think it's directly possible. I wonder if you have any idea on how to implement a "copy image to clipboard" function?
I believe the key would be to create a hidden
BrowserWindow
orcanvas
object and then use the webContents →copyImageAt()
API method?The text was updated successfully, but these errors were encountered: