npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

image-pal-canvas

v1.2.5

Published

A browser based `image-pal` implementation that leverages `Image` and `Canvas` for fast palette retrieval

Downloads

57

Readme

Intro

A browser based Image-Pal implementation that leverages Image and Canvas for fast palette generation.

Demo

To see demo clone this repo and open demo/index.html in any browser (IE11 or later). Select image (or drag & drop) to see palette.

NPM

Usage

Very similar pattern to its parent Image-Pal, but asyncrhonous to accomodate events (onload, etc). Additionally, rgb spectrum is used by default for client to keep size small. Specify hsluv directly if you want the extra weight.

  const getColors = require('image-pal-canvas/lib/hsluv');
  // OR if you want the non-human-perceptual version based on pure RGB
  // const getColors = require('image-pal-canvas/lib/rgb');
  
  getColors(options, (err, colors) => {
    if (err) return void console.error('oops!', err.stack || err);

    colors.forEach(color => {
      console.log(color.rgb); // [ 100, 100, 100 ]
      console.log(color.alpha); // 255
      console.log(color.hex); // #abc123
      // below props only available if using `hsluv` version
      console.log(color.hsluv); // [ 1, 50, 100 ]
    });
  });

Options

Large images are not necessary for computing accurate palettes. It's highly recommended to use default settings for high performance and quality results.

| Name | Type | Default | Desc | | --- | --- | --- | --- | | srcUrl | String | (optional) | If you're supplying your own image url | | width | Number | 100 | Maximum width of canvas in pixels. Only width OR height should be set, not both, to respect aspect ratio of source image | | height | Number | undefined | Maximum height of canvas in pixels. Only width OR height should be set, not both, to respect aspect ratio of source image | | imageEl | HTMLElement | (optional) | If specified, will use supplied img element instead of automatically created background image | | canvasEl | HTMLElement | (optional) | If specified, will use supplied canvas element instead of automatically created background canvas | | inputEl | HTMLElement | (optional) | If specified, will use supplied input to use as the file load trigger. MUST be of type='file' |

Additional tuning options can be found at Image-Pal.

Source Usage

The simplest example where we want everything to happen in the background and simply return the color palette once available.

  getColors({ srcUrl: '//some-domain.com/some-image.jpg' }, (err, colors) => {
    // do something with err & colors
  });

Image Usage

Here we have an existing img element we want to attach to.

  getColors({ imageEl: document.getElementById('myImage') }, (err, colors) => {
    // do something with err & colors
  });

Canvas Usage

Here we have an existing canvas element we want to attach to.

  getColors({ canvasEl: document.getElementById('myCanvas') }, (err, colors) => {
    // do something with err & colors
  });

Upload Usage

Here we have an existing input element we want to attach to.

  getColors({ inputEl: document.getElementById('myUploadButton') }, (err, colors) => {
    // do something with err & colors
  });

Advanced Usage

Here we're attaching to an existing img, canvas, and input elements.

  getColors({
    srcUrl: '//some-domain.com/some-image.jpg', // set default image
    imageEl: document.getElementById('myImage'), // attach to existing `img` element
    canvasEl: document.getElementById('myCanvas'), // render to my visible canvas
    inputEl: document.getElementById('myUploadButton') // update image & canvas if user tries to upload file
  }, (err, colors) => {
    // do something with err & colors
  });

Unregister Usage

If you need unregister listeners simply:

  const getColorsInstance = getColors(options, cb);

  getColorsInstance.unregister(); // free me!