Clipboard API Image Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Clipboard API Image Tutorial

Clipboard API Image Tutorial

Yes, yesterdayโ€™s Clipboard API Primer Tutorialโ€˜s Clipboard API usages just involved โ€ฆ

  • text based data โ€ฆ and today we turn our attention to โ€ฆ
  • image based data

โ€ฆ to extend the functionality and interest of ourchanged clipboard_api_testโšซhtm proofโœ‚of concept
text and image clipboard using webpage.

Itโ€™s lucky for us that there are so many good resources out there to help, one link of real interest, for us, being this excellent link, thanks, getting us to use this new Javascript function โ€ฆ



async function pasteImage() {

//event.stopPropagation();

try {

const permission = await navigator.permissions.query({ name: 'clipboard-read' });

if (permission.state === 'denied') {

throw new Error('Not allowed to read clipboard.');

}

const clipboardContents = await navigator.clipboard.read();

for (const item of clipboardContents) {

if (!item.types.includes('image/png')) {

throw new Error('Clipboard contains non-image data.');

}

const blob = await item.getType('image/png');

imgz = new Image();

anothercell();

imgz.onload = () => {

destinationImage.style.width='' + imgz.width + 'px';

destinationImage.style.height='' + imgz.height + 'px';

prevw=eval('' + ('' + cnv.style.width).replace('px',''));

prevh=eval('' + ('' + cnv.style.height).replace('px',''));

console.log('canvas width becomes ' + eval('' + cnv.width) + ' + ' + eval('' + imgz.width) + ' = ' + '' + eval(eval('' + cnv.width) + eval('' + imgz.width)) + 'px');

cnv.style.width='' + eval(eval('' + ('' + cnv.style.width).replace('px','')) + eval('' + imgz.width)) + 'px';

cnv.style.height='' + eval(eval('' + ('' + cnv.style.height).replace('px','')) + eval('' + imgz.height)) + 'px';

cnv.width='' + ('' + cnv.style.width).replace('px','') + 'px';

cnv.height='' + ('' + cnv.style.height).replace('px','') + 'px';

if (mmode == 'mbefore' || 1 == 1) {

ctx.drawImage(imgz, prevw, prevh);

if (wo) {

wo.close();

wo=null;

}

cnv.style.display='block';

//wo=window.open('','_blank','top=50,left=50,height=600,width=600');

//wo.document.write(cnv.toDataURL('image/png'));

}

};

imgz.src = URL.createObjectURL(blob);

destinationImage.src = URL.createObjectURL(blob);

//cnv.style.backgroundRepeat=(('' + cnv.style.backgroundRepeat) + ',no-repeat').replace(/^\,/g,'');

//if (('' + cnv.style.background + ' ').trim() != '') { document.getElementById('mysummary').innerHTML='Conglomerated Images below ...'; }

cnv.style.background=(('' + cnv.style.background) + ',url(' + destinationImage.src + ') no-repeat').replace(/^\,/g,'');

//document.querySelector(".editor").style.display='block';

//destinationImage.style.display='none';

setTimeout(anothercellz, 6000);

}

}

catch (error) {

console.error(error.message);

}

}

โ€ฆ to achieve a lot of this image data use of the Clipboard API functionality.



Previous relevant Clipboard API Primer Tutorial is shown below.

Clipboard API Primer Tutorial

Clipboard API Primer Tutorial

Hereโ€™s another day of testing a Javascript API today, which has that โ€œdesktop feelโ€, that being the Clipboard API โ€ฆ

The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard.

โ€ฆ our clipboard_api_testโšซhtml proofโœ‚of concept version offering โ€œtext clipboard dataโ€ functionality from the text based clipboard into an HTML textarea element via โ€ฆ

  • append
  • prepend
  • at cursor

โ€ฆ modes of use, you can also try below (or get a sneak peak at tomorrowโ€™s ideas) โ€ฆ

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *