Canvas Color Picker

Currently, color pickers on the web use CSS gradients instead of canvas. The trouble with this approach is that the picker has to be a fixed size. Any change in the size of the color picker means a change in your calculations.

Furthermore, the colors you get from such pickers are approximates. They’re based on a calculation involving your mouse coordinates, the size of the picker and HSL.

By using HTML5 Canvas, we can get extract absolute color values, so they’re always spot on. Canvas was built for this sort of application, and is the ideal tool for the job.

When I was building Stylo I needed a top notch color picker, such as Photoshop’s, but couldn’t find one. I ended up writing my own, called ColorCanvas, which I’m happy to announce I’m open sourcing today.

You can find a demo here, and the source on GitHub.


I’ve split up the CoffeeScript code into separate modules, which is compiled down by Sprockets into two files, colorcanvas.js and colorcanvas.input.js. The latter is a shim for color inputs.


Include the two scripts, lib/colorcanvas.js and lib/colorcanvas.input.js inside your page.

  <script src="lib/colorcanvas.js"></script>
  <script src="lib/colorcanvas.input.js"></script>

Any color inputs will now automatically be shimmed.

<input type="color" value="#FF0000">

You may also want to include the default styles, located in site/styles.css.

If you like this post you should follow me on Twitter.


Now read this

SEO in JS Web Apps

JavaScript and search engines have always had a tricky relationship, and SEO is often used as an argument against single page applications. I’m aiming to put some of those misconceptions to rest in this article and show you that it’s... Continue →