Cross-browser imageless linear gradients v2

A while ago, I posted a script of mine for creating 2-color cross-browser imageless linear gradients. As I stated there, I needed them for a color picker I have to create. And even though 2-color gradients are sufficient for most components, in most co…


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou

A while ago, I posted a script of mine for creating 2-color cross-browser imageless linear gradients. As I stated there, I needed them for a color picker I have to create. And even though 2-color gradients are sufficient for most components, in most color spaces, I had forgotten an important one: Hue. You can’t represent Hue with a 2-color gradient! So, I had to revise the script, and make it able to produce linear gradients of more than 2 colors. Furthermore, I needed to be able to specify a fully transparent color as one of the gradient colors, in order to create the photoshop-like 2d plane used by the picker (and no, a static image background like the one used in most JS color pickers wouldn’t suffice, for reasons irrelevant with this post). I hereby present you Cross-browser, imageless, linear gradients v2!

The API has stayed just the same, with the following differences:

  • You may specify the keyword “transparent” instead of a #RRGGBB color (that was such a pain to implement btw!).

  • When creating a Gradient object, color strings are now defined in an array. Example:

    var g = new Gradient(200, 100, [‘#000000’, ‘#ff1166’, ‘#23ff46’], true);

  • When calling g.paint() it now takes 2 arguments instead of 3: The new color array (or null if you don’t want that to change) and the direction (true for vertical, false for horizontal). For example:

    g.paint([‘#000000’, ‘#ff1166’, ‘#23ff46’], true);

  • 2 new methods have been added: g.setColorAt(index, color) and g.direction(newDirection). The first allows you to set a particular gradient color (index starting from 0) and the second to alter or toggle the direction (if you specify a direction parameter, you set the direction, if you call it with no parameters, it toggles from horizontal to vertical).

  • The fields g.startColor and g.endColor have been replaced by the array g.colors.

Update: v2.0.1 Fixed a small bug with the ‘transparent’ keyword that affected multi-color gradients in browsers != IE when the transparent color wasn’t first or last.

Enjoy:

gradient.js (5.1 KB)

gradient-min.js (2.7 KB)

Test page


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2009-04-24T00:00:00+00:00) Cross-browser imageless linear gradients v2. Retrieved from https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/

MLA
" » Cross-browser imageless linear gradients v2." Lea Verou | Sciencx - Friday April 24, 2009, https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/
HARVARD
Lea Verou | Sciencx Friday April 24, 2009 » Cross-browser imageless linear gradients v2., viewed ,<https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/>
VANCOUVER
Lea Verou | Sciencx - » Cross-browser imageless linear gradients v2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/
CHICAGO
" » Cross-browser imageless linear gradients v2." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/
IEEE
" » Cross-browser imageless linear gradients v2." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/. [Accessed: ]
rf:citation
» Cross-browser imageless linear gradients v2 | Lea Verou | Sciencx | https://www.scien.cx/2009/04/24/cross-browser-imageless-linear-gradients-v2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.