Paste an image from clipboard with JavaScript
In this post, we will learn how to paste an image from the clipboard with JavaScript using the Clipboard API, which is a modern and standardized way of accessing and manipulating the clipboard contents.
Pasting an image from the clipboard is a feature that allows users to copy and paste images from other sources, such as screenshots, web pages, or documents, into a web page using JavaScript. This feature can be useful for various purposes, such as uploading images, editing images, or sharing images. However, implementing this feature can be challenging, as different browsers have different ways of handling the clipboard data and the image elements.
1. What is the Clipboard API?
The Clipboard API is a set of methods and properties that enable us to interact with the system clipboard, which is a temporary storage area for data that can be copied and pasted between applications. The Clipboard API is supported by most modern browsers and allows us to:
- Get the data of the clipboard using
event.clipboardDataornavigator.clipboard.read(). - Set the data of the clipboard using
event.clipboardDataornavigator.clipboard.write(). - Listen to copy, cut, and paste events that are fired when the user performs clipboard operations using
document.addEventListener('copy', handler),document.addEventListener('cut', handler), ordocument.addEventListener('paste', handler). - Request permission to access the clipboard using
navigator.permissions.query({name: 'clipboard-read'})ornavigator.permissions.query({name: 'clipboard-write'}).
The data of the clipboard can be in various formats, such as text, HTML, or images. The data can be accessed as a list of items, where each item has a type and a value. The type is a MIME type that indicates the format of the data, such as text/plain, text/html, or image/png. The value is a blob that contains the binary data of the item.
2. How to Paste an Image from the Clipboard?
To paste an image from the clipboard with Clipboard API, we need to follow these steps:
- Listen to the paste event that is fired when the user presses
Ctrl+Vor uses the context menu to paste an image from the clipboard. - Get the data of the clipboard using
event.clipboardDataand check if it contains an image item. - Get the blob of the image item using
item.getAsFile()and read its contents usingFileReader.readAsDataURL(). - Get an image element using
document.getElementById('id')and set its source attribute to the data URL of the image blob.
Here is an example of how to implement these steps in JavaScript:
JS
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
document.onpaste = function(pasteEvent) { // consider the first item (can be easily extended for multiple items) var item = pasteEvent.clipboardData.items[0]; if (item.type.indexOf("image") === 0) { // Get the blob of the image var blob = item.getAsFile(); // Create a file reader var reader = new FileReader(); // Set the onload event handler reader.onload = function(event) { // Get the data URL of the image let dataURL = event.target.result; // get the img container id let img = document.getElementById("container"); // Set the source attribute of the image element img.src = dataURL; }; // Read the blob as a data URL reader.readAsDataURL(blob); } } |
HTML
|
1 2 |
<p>Paste our image hereā¦</p> <img id="container"/> |
This code will paste any image from the clipboard into the image container. We can modify this code according to our needs and preferences. For example:
- We can use a different selector or method to append or replace the image element, such as
document.getElementById(),document.querySelector(),element.appendChild(), orelement.replaceChild(). - We can use a different attribute or property to set or get the source of the image element, such as img.setAttribute(‘src’, dataURL), img.getAttribute(‘src’), or
img.src. - We can use a different event or method to read the blob of the image, such as
FileReader.readAsArrayBuffer(),FileReader.readAsBinaryString(), orFileReader.readAsText(). - We can use a different event or method to create or get the blob of the image, such as
Blob(),URL.createObjectURL(), ornavigator.clipboard.read().
That’s all about pasting an image from the clipboard with JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)