I've built a handful of color pickers through the years, so when I in this book stumbled upon a color-wheel with “built-in” shades and tints, I had to code it:
At first, I thought: “That's easy – it's just a conical gradient with hard stops for the colors, and a radial gradient for the tints and shades”.
But, alas, look at how “un-crisp” the edges in the conical gradient are (Chrome, MacOS):
So – I had to do it in <svg>
.
Each “ring” in the circle is built up by 12 circles with different colors and stroke-dasharray
:
<g id="wheel">
<circle class="c3" cx="16" cy="16" r="16"></circle>
<circle class="c2" cx="16" cy="16" r="16"></circle>
<circle class="c1" cx="16" cy="16" r="16"></circle>
...etc
And the CSS:
--u: calc(100 / 12);
.c1 {
stroke: hsl(0, var(--s), var(--l));
stroke-dasharray: var(--u) 100;
}
.c2 {
stroke: hsl(30, var(--s), var(--l));
stroke-dasharray: calc(2 * var(--u)) 100;
}
.c3 {
stroke: hsl(60, var(--s), var(--l));
stroke-dasharray: calc(3 * var(--u)) 100;
}
/* ... etc */
The circles are wrapped in a <g>
-tag with id="wheel"
, so I can use <use>
(pun intended) to clone the rings:
<use href="#wheel" class="r3" />
<use href="#wheel" class="r2" />
<use href="#wheel" class="r1" />
etc...
Each ring-class then sets a different --l
(lightness) and stroke-width
:
.r1 {
--l: 40%;
stroke-width: 3;
}
.r2 {
--l: 45%;
stroke-width: 6;
}
/* ... etc */
In the final result I've chosen more tints than shades, so it's not as dark as in the book-example. Firefox has issues with setting some svg-attributes in CSS, so check it out in Chrome, Edge or Safari:
Let's have some fun!
How about rotating those color rings – or changing the saturation? Scroll down to see and try the controls, if they're not at the top of the Pen:
To have an automated animation, animating the --r
custom property, we need to register a Custom Property:
@property --r {
syntax: "<angle>";
initial-value: 0deg;
inherits: false;
}
And the animation:
@keyframes rotate {
100% {
--r: 360deg;
}
}
Resulting in this (you need Chrome/Edge with Houdini):
Don't stare at it too long – or you'll be hypnotized!
Thanks for reading!
Top comments (11)
My CSS only idea: codepen.io/t_afif/pen/VwPqGyj and with rotation: codepen.io/t_afif/pen/KKabxJJ (need more html here)
Nice 😄, Can you show us how to make CSS based animations like
Thank you and Successful and congrats messages
to users, usingconfetti css
or some other technique. Like below:tympanus.net/Development/AnimatedM...
Example:
are you challenging me ? 😛
Ok, I will think about it, maybe this will be my next post 😉
Not challenging. Just asking if you have seen those kind of messages anywhere. I dont know how they do those kind of animation maybe using
CSS or SVG
.Just want to learn cool CSS animations 😄
Such animations are generally made using canvas. If you inspect the code you will notice a
<canvas>
element and then you have to dig into the JS because it's there where you will find the whole logicNice! But it has the same issue on Chrome (MacOS), as I encountered - the edges are blurry :-(
That's why I went the svg-way, although the CSS-way is way simpler!
Looks great in Safari, though!
yes it's a known issue when the gradient contain a lot of color stops. You can fix it by using multiple background instead: codepen.io/t_afif/pen/VwPqEmr
Wow! I'm super-impressed - also at the speed you've been making these demos!
Hi, but your solution doesn't let pick a color. As I understand for this purpose you HAVE TO have this color wheel on canvas to extract data with ImageData
Lovely! Thanks for sharing!
Thank you!