Skip to content
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: Allow to pass video or canvas instead just image element type #197

Closed
vladmandic opened this issue Sep 7, 2020 · 2 comments
Closed

Comments

@vladmandic
Copy link

vladmandic commented Sep 7, 2020

As the subject line says, color-thief creates canvas from an image param.
In some cases, I already have canvas created from the image, would be great if I could pass it as-is.

Quick hack - this also allows color-thief to work with image, canvas or video elements:

/*
  CanvasImage Class
  Class that wraps the html image/canvas/video element and internal canvas.
  It also simplifies some of the canvas context manipulation
  with a set of helper functions.
*/

const CanvasImage = function (element) {
    if (element.nodeName === 'CANVAS') {
        this.canvas = element;
        this.context = element.getContext('2d');
        this.width  = element.width;
        this.height = element.height;
    } else if (element.nodeName === 'VIDEO') {
        this.canvas  = document.createElement('canvas');
        this.context = this.canvas.getContext('2d');
        this.width  = this.canvas.width  = video.videoWidth;
        this.height = this.canvas.height = element.videoHeight;
        this.context.drawImage(element, 0, 0, this.width, this.height);
    } else {
        this.canvas  = document.createElement('canvas');
        this.context = this.canvas.getContext('2d');
        this.width  = this.canvas.width  = element.naturalWidth;
        this.height = this.canvas.height = element.naturalHeight;
        this.context.drawImage(element, 0, 0, this.width, this.height);
    }
};

#198

@vladmandic vladmandic changed the title Suggestion: Allow to pass canvas instead of image Suggestion: Allow to pass video or canvas instead just image element type Sep 7, 2020
@vladmandic
Copy link
Author

closing as no update from maintainer for over a year.

@marcanw
Copy link

marcanw commented Jun 17, 2024

Thx for the hack ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants