All Projects → LuanEdCosta → copy-image-clipboard

LuanEdCosta / copy-image-clipboard

Licence: MIT license
Lightweight library to copy PNG and JPG images to clipboard

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to copy-image-clipboard

CopyPasteJS
This a small JS library to execute clipboard functions in a fast and easy way.
Stars: ✭ 20 (-45.95%)
Mutual labels:  clipboard, copy-to-clipboard
react-hook-clipboard
A React hook for accessing clipboard
Stars: ✭ 16 (-56.76%)
Mutual labels:  clipboard, copy-to-clipboard
clipper
📋 Cross Platform Desktop App to Save history of all information you copy and use them whenever with a solitary snap
Stars: ✭ 42 (+13.51%)
Mutual labels:  clipboard, copy-to-clipboard
Xcv
✂️ Cut, Copy and Paste files with Bash
Stars: ✭ 144 (+289.19%)
Mutual labels:  clipboard
React Use Clipboard
React hook that provides copy to clipboard functionality.
Stars: ✭ 149 (+302.7%)
Mutual labels:  clipboard
Clipboardy
Chrome extension for copying posted code to clipboard from stackoverflow.com, github.com & npmjs.com
Stars: ✭ 202 (+445.95%)
Mutual labels:  clipboard
Lazyhub
lazyhub - Terminal UI Client for GitHub using gocui.
Stars: ✭ 133 (+259.46%)
Mutual labels:  clipboard
Vim Yoink
Vim plugin that maintains a yank history to cycle between when pasting
Stars: ✭ 225 (+508.11%)
Mutual labels:  clipboard
Clipboard
React Native Clipboard API for both iOS and Android.
Stars: ✭ 198 (+435.14%)
Mutual labels:  clipboard
Piknik
Copy/paste anything over the network.
Stars: ✭ 2,221 (+5902.7%)
Mutual labels:  clipboard
Windows User Action Hook
A .NET library to subscribe for Windows operating system global user actions such mouse, keyboard, clipboard & print events
Stars: ✭ 224 (+505.41%)
Mutual labels:  clipboard
Zsh Vi Mode
💻 A better and friendly vi(vim) mode plugin for ZSH.
Stars: ✭ 181 (+389.19%)
Mutual labels:  clipboard
Gnome Shell Screenshot
Gnome Shell extension for making and uploading screenshots
Stars: ✭ 163 (+340.54%)
Mutual labels:  clipboard
Ditto
Ditto is an extension to the Windows Clipboard. You copy something to the Clipboard and Ditto takes what you copied and stores it in a database to retrieve at a later time.
Stars: ✭ 193 (+421.62%)
Mutual labels:  clipboard
Tmux Yank
Tmux plugin for copying to system clipboard. Works on OSX, Linux and Cygwin.
Stars: ✭ 1,941 (+5145.95%)
Mutual labels:  clipboard
Cheval
📋 Copy to the clipboard using JavaScript without writing JS. A full solution for all browsers and all devices. LibreJS compliant.
Stars: ✭ 231 (+524.32%)
Mutual labels:  clipboard
Use Clippy
React Hook for reading from and writing to the user's clipboard.
Stars: ✭ 139 (+275.68%)
Mutual labels:  clipboard
Clipman
A simple clipboard manager for Wayland
Stars: ✭ 182 (+391.89%)
Mutual labels:  clipboard
Vue Clipboards
📋 Vue2.0 directive to copy or cut text to clipboard.
Stars: ✭ 200 (+440.54%)
Mutual labels:  clipboard
vim-poweryank
Copy text over SSH
Stars: ✭ 51 (+37.84%)
Mutual labels:  clipboard

Copy Image Clipboard Logo

Copy Image Clipboard

NPM Version NPM Downloads Per Week License GitHub Last Commit Repository Issues Dependencies

👉 TEST IN YOUR BROWSER 👈
  • Copy JPG or PNG images to clipboard easily.
  • It is a lightweight library with 0 dependencies.
  • You can use with React, Vue, Angular or with any other framework.

⬇️ Installation

Using a package manager

yarn add copy-image-clipboard
npm i copy-image-clipboard

Without a package manager

Without a package manager you have to choose one of these:

Use from a CDN Provider

jsDelivr

<script src="https://cdn.jsdelivr.net/npm/copy-image-clipboard/dist/index.browser.js"></script>

With a CDN Provider you will be using the dist/index.browser.js file. See more about this file below.


Download the entire repository
  • Use degit to download this repository without the git history:
npx degit LuanEdCosta/copy-image-clipboard
  • Download a zipped file on GitHub:
Download the entire repository

After downloading the repository, you can use a file from the dist folder in your code. See more about the dist folder files below.


Download a file from the dist folder

Open the dist folder and choose one of these files to download:

See more about these files below.


About the dist folder

  • index.browser.js
    • Minified to use in browsers
    • Exposes everything in a variable called CopyImageClipboard. e.g. CopyImageClipboard.copyImageToClipboard('...').
  • index.js
    • Uses ESM module format.
    • Is not minified.
  • index.common.js
    • Uses CommonJs module format.
    • Is not minified.
  • index.d.ts
    • Contains TypeScript types.

Usage

Copy using the image source

👉 <img src="...">

This approach downloads the image using window.fetch, transform to PNG if is JPEG and then copy to clipboard using the image original dimensions.

// Import the copy function
import { copyImageToClipboard } from 'copy-image-clipboard'

// Pass the image src attribute here
copyImageToClipboard('assets/image.png')
  .then(() => {
    console.log('Image Copied')
  })
  .catch((e) => {
    console.log('Error: ', e.message)
  })

// Can be an URL too, but be careful because this may cause CORS errors
copyImageToClipboard(
  'https://images-na.ssl-images-amazon.com/images/I/81BES%2BtsVvL.png',
)
  .then(() => {
    console.log('Image Copied')
  })
  .catch((e) => {
    console.log('Error: ', e.message)
  })

Copy the rendered image into the document

With this approach no HTTP request is necessary, but the image is copied with the image element dimensions in the document. If the image is 1000x1000 but is shown as 300x300, the copied image will be 300x300. Use copyImageToClipboard function to copy the image with its original dimensions.

import {
  getBlobFromImageElement,
  copyBlobToClipboard,
} from 'copy-image-clipboard'

const imageElement = document.getElementById('image')

getBlobFromImageElement(imageElement)
  .then((blob) => {
    return copyBlobToClipboard(blob)
  })
  .then(() => {
    console.log('Blob Copied')
  })
  .catch((e) => {
    console.log('Error: ', e.message)
  })

Check if can copy images to clipboard

Use this function to synchronously check at runtime whether you can copy images to the clipboard. It checks if it can use the Fetch API and the Clipboard API.

import { canCopyImagesToClipboard } from 'copy-image-clipboard'

const canCopy = canCopyImagesToClipboard()

console.log('Can Copy Images To Clipboard:', canCopy)

Check if the permission to write data on clipboard was granted

Warnings:

  • Permission to write data to the clipboard is automatically granted to pages when they are in the active tab, so generally you don't need to use this function.
  • If the browser has not implemented the Permissions API yet, this function will return false. Check the browser compatibility here: Permissions API Browser Compatibility.
import { requestClipboardWritePermission } from 'copy-image-clipboard'

requestClipboardWritePermission().then((hasPermission) => {
  console.log('Has Permission:', hasPermission)
})

Demos

You can contribute with more examples if you want 😄

🌐 Compatibility

This project uses the asynchronous Clipboard API and Fetch API. Most browsers already support natively these two APIs, but for the old ones like Internet Explorer this library doesn't work and there's nothing you can do about it.

Use the links below to see the browser compatibility:

Enable Clipboard API Features in Firefox

From Version 87: You need to set dom.events.asyncClipboard.clipboardItem preference to true. To change preferences in Firefox, visit about:config.

🛑 Known Limitations

  • For now you can copy only JPG and PNG images

    Other image types are not supported. If you try to copy other type an error will be thrown.

  • This library only works in pages with HTTPS

    This limitation was defined by the browsers due to security risks involved when dealing with the user's clipboard.

  • You can only copy an image in the user's active tab/document

    If the user is navigating in another tab and the copy function is called, an error will be thrown.

🤝 Contribution

Every kind of help is appreciated and this project can be better with your help.

What you can do:

📘 License

This project is under the MIT LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].