Code Monkey home page Code Monkey logo

copy-image-clipboard's Introduction

๐Ÿ‘‹ Hello World

LuanEdCosta Profile Views

I'm a front-end developer and I'm currently working with JavaScript/TypeScript, React, React Native and Node.js.

๐Ÿ“ง Contact

  • Linkedin Badge
  • Gmail Badge

โค๏ธ Things I Like To Do

  • ๐Ÿ“ฑ ๐Ÿ’ป Create mobile apps and websites.
  • โญ Create open source projects to solve problems.
  • ๐Ÿง  Learn technologies and tools.
  • ๐Ÿ™‹ Help people.

๐Ÿš€ Published Projects

๐Ÿ“ˆ Statistics

copy-image-clipboard's People

Contributors

dependabot[bot] avatar luanedcosta avatar reach1dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

copy-image-clipboard's Issues

Options argument for the image copy

Is your feature request related to a problem? Please describe.
[For SVGs only]
I suggest providing an optional payload to the copyImageToClipboard and copyBlobToClipboard to overcome size-related issues and make it independent from the in-DOM size of images.
Reference image:
image
Specifically, optional attributes to be used in place of the provided image's.

/**
 * Gets a blob from an image element.
 *
 * @param {HTMLImageElement} imageElement An image element
 * @returns {Promise<Blob>} A Promise that resolves to a image blob. Rejects the promise if cannot get a blob from the image element.
 */
function getBlobFromImageElement(imageElement) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise(function (resolve, reject) {
            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');
            if (context) {
                const { width, height } = imageElement;
                canvas.width = width;
                canvas.height = height;
                context.drawImage(imageElement, 0, 0, width, height);
                canvas.toBlob(function (blob) {
                    if (blob)
                        resolve(blob);
                    else
                        reject('Cannot get blob from image element');
                }, 'image/png', 1);
            }
        });
    });
}

Describe the solution you'd like

Instead, we can pass an optional parameter into the function as a payload and match the width and height attribute from it. Example:

function getBlobFromImageElement(imageElement, payload) {
    ...
    let width, height;
    if(payload){
       width  = payload.width;
       height = payload.height;
    } else {
        width  = imageElement.width;
        height = imageElement.height;
    }
    canvas.width = width;
    canvas.height = height;
    ...
}

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

  1. Apply this only for SVGs.
  2. Preserve image quality even for PNG and JPG.

Import error for SvelteKit

Describe the bug

Importing this package in SvelteKit leads to warnings in development and to errors in production builds related to ES modules.

I've set up a demo where I've made the following changes from a new SvelteKit project:

  1. npm install copy-image-clipboard
  2. In src/routes/+page.svelte I've added basic code to check for image copy support:
import { canCopyImagesToClipboard } from 'copy-image-clipboard';

onMount(() => {
	const canCopy = canCopyImagesToClipboard();

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

When running in development mode with vite dev I get the following warning: copy-image-clipboard doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix. Forced re-optimization of dependencies

When creating a production build with vite build I get the following error:

(node:13) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/projects/sveltejs-kit-template-default-8qhsvf/node_modules/copy-image-clipboard/dist/index.js:175
export { canCopyImagesToClipboard, convertBlobToPng, copyBlobToClipboard, copyImageToClipboard, createImageElement, getBlobFromImageElement, getBlobFromImageSource, isJpegBlob, isPngBlob, requestClipboardWritePermission };
^^^^^^

SyntaxError: export declarations may only appear at top level of a module
    at compileFunction (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:341450)
    at wrapSafe (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:218347)
    at Module.prototype._compile (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:218723)
    at Module._extensions[".js"] (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:219743)
    at Module.prototype.load (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:217769)
    at Module._load (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:215340)
    at <anonymous> (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:983454)
    at _evaluate (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:15:300439)
    at _evaluate (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:15:300563)
    at <anonymous> (<anonymous>)
Error: 500 /
    at normalise_error_handler/<@file://file:///home/projects/sveltejs-kit-template-default-8qhsvf/node_modules/ (sveltejs/kit/src/core/postbuild/prerender.js:68:12)
    at save@file://file:///home/projects/sveltejs-kit-template-default-8qhsvf/node_modules/ (sveltejs/kit/src/core/postbuild/prerender.js:370:21)
    at visit@file://file:///home/projects/sveltejs-kit-template-default-8qhsvf/node_modules/ (sveltejs/kit/src/core/postbuild/prerender.js:219:7)
    at <anonymous> (<anonymous>)
[vite-plugin-sveltekit-compile] Failed with code 1
error during build:
Error: Failed with code 1
    at fn/</<@file://file:///home/projects/sveltejs-kit-template-default-8qhsvf/node_modules/ (sveltejs/kit/src/utils/fork.js:83:13)
    at EventEmitter.prototype.emit (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:155574)
    at ChildProcess/this._handle.onexit (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:956239)
    at _onChildExit/< (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:15:393246)
    at _0x5d97a4 (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:15:150541)
    at _0x2fb6cc/< (https://sveltejskittemplatedefault8qhs-lxfs.w-corp.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:15:150281)
    at <anonymous> (<anonymous>)

Is copying Image to clipboard Supported in ios

Describe the bug

@LuanEdCosta , I am trying to implement copying an image. It worked fine in Linux.
Failed in ios with an error saying The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. in both chrome and safari.

Same thing happened with the given demo: https://luanedcosta.github.io/copy-image-clipboard/

Isn't it suported in ios?

Browsers (please complete the following information)

Browser name: Safari
Version: 15.6

Browser name: Chrome
Version: 104.0.5112.88

Expected behavior

To be able to copy image

Screenshots

If applicable, add screenshots to help explain your problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.