Exploring the CSS Paint API: Rounding Shapes

No Comments

Including borders to complicated shapes is a ache, however rounding the nook of complicated shapes is a nightmare! Fortunately, the CSS Paint API is right here to the rescue! That’s what we’re going to have a look at as a part of this “Exploring the CSS Paint API” sequence.

Exploring the CSS Paint API sequence:

Half 1: Picture Fragmentation ImpactHalf 2: Blob AnimationHalf 3: Polygon BorderHalf 4: Rounding Shapes (you might be right here!)

Right here’s what we’re aiming for. Like all the pieces else we’ve checked out on this sequence, observe that solely Chrome and Edge help this for now.

Dwell Demo

You’ll have seen a sample forming when you’ve adopted together with the remainder of the articles. Generally, after we work with the CSS Paint API:

We write some primary CSS that we are able to simply alter.All of the complicated logic is completed behind the scene contained in the paint() operate.

We are able to really do that with out the Paint API

There are in all probability a variety of methods to place rounded corners on complicated shapes, however I’ll share with you three strategies I’ve utilized in my very own work.

I already hear you saying: If you happen to already know three strategies, then why are you utilizing the Paint API? Good query. I’m utilizing it as a result of the three strategies I’m conscious of are tough, and two of them are particularly associated to SVG. I’ve nothing towards SVG, however a CSS-only answer makes factor simpler to take care of, plus it’s simpler for somebody to stroll into and perceive when grokking the code.

Onto these three strategies…

Utilizing clip-path: path()

If you’re an SVG guru, this methodology is for you. the clip-path property accepts SVG paths. Which means we are able to simply cross within the path for a posh rounded form and be executed. This strategy is tremendous simple if you have already got the form you need, nevertheless it’s unsuitable if you’d like an adjustable form the place, for instance, you need to alter the radius.

Under an instance of a rounded hexagon form. Good luck attempting to regulate the curvature and the form measurement! You’re gonna should edit that crazy-looking path to do it.

CodePen Embed Fallback

I suppose you would confer with this illustrated information to SVG paths that Chris put collectively. However it’s nonetheless going to be a variety of work to plot the factors and curves simply the way you need it, even referencing that information.

Utilizing an SVG filter

I found this system from Lucas Bebber’s put up about making a gooey impact. You’ll find all of the technical particulars there, however the thought is to use an SVG filter to any factor to spherical its corners.

CodePen Embed Fallback

We merely use clip-path to create the form we would like then apply the SVG filter on a mum or dad factor. To manage the radius, we alter the stdDeviation variable.

It is a good approach, however once more, it requires a deep degree of SVG know-how to make changes on the spot.

Utilizing Ana Tudor’s CSS-only strategy

Sure, Ana Tudor discovered a CSS-only approach for a gooey impact that we are able to use to not far away of complicated shapes. She’s in all probability writing an article about it proper now. Till then, you possibly can confer with the slides she made the place she clarify the way it works.

Under a demo the place I’m changing the SVG filter along with her approach:

CodePen Embed Fallback

Once more, one other neat trick! However so far as being simple to work with? Not a lot right here, both, particularly if we’re contemplating extra complicated conditions the place we want transparency, photographs, and so forth. It’s work discovering the proper mixture of filter, mix-blend-mode and different properties to get issues excellent.

Utilizing the CSS Paint API as a substitute

Except you could have a killer CSS-only option to put rounded borders on complicated shapes that you simply’re retaining from me (share it already!), you possibly can in all probability see why I made a decision to succeed in for the CSS Paint API.

The logic behind this depends on the identical code construction I used within the article protecting the polygon border. I’m utilizing the –path variable that defines our form, the cc() operate to transform our factors, and some different methods we’ll cowl alongside the way in which. I extremely suggest studying that article to higher perceive what we’re doing right here.

First, the CSS setup

We first begin with a traditional rectangular factor and outline our form contained in the –path variable (form 2 above). The –path variable behaves the identical method as the trail we outline inside clip-path: polygon(). Use Clippy to generate it. 

.field {
show: inline-block;
peak: 200px;
width: 200px;

–path: 50% 0,100% 100%,0 100%;
–radius: 20px;
-webkit-mask: paint(rounded-shape);

Nothing complicated thus far. We apply the customized masks and we outline each the –path and a –radius variable. The latter can be used to regulate the curvature.

Subsequent, the JavaScript setup

Along with the factors outlined by the trail variable (pictured as purple factors above), we’re including much more factors (pictured as inexperienced factors above) which are merely the midpoints of every section of the form. Then we use the arcTo() operate to construct the ultimate form (form 4 above).

Including the midpoints is fairly simple, however utilizing arcTo() is a bit tough as a result of we’ve got to know the way it works. In accordance with MDN:

[It] provides a round arc to the present sub-path, utilizing the given management factors and radius. The arc is mechanically related to the trail’s newest level with a straight line, if essential for the desired parameters.

This methodology is usually used for making rounded corners.

The truth that this methodology requires management factors is the primary purpose for the additional midpoints factors. It additionally require a radius (which we’re defining as a variable referred to as –radius).

If we proceed studying MDN’s documentation:

A method to consider arcTo() is to think about two straight segments: one from the start line to a primary management level, and one other from there to a second management level. With out arcTo(), these two segments would type a pointy nook: arcTo() creates a round arc that matches this nook and smooths is out. In different phrases, the arc is tangential to each segments.

Every arc/nook is constructed utilizing three factors. If you happen to verify the determine above, discover that for every nook we’ve got one purple level and two inexperienced factors on both sides. Every red-green mixture creates one section to get the 2 segments detailed above.

Let’s zoom into one nook to higher perceive what is occurring:

Now we have each segments illustrated in black.
The circle in blue illustrates the radius.

Now think about that we’ve got a path that goes from the primary inexperienced level to the following inexperienced level, transferring round that circle. We do that for every nook and we’ve got our rounded form.

Right here’s how that appears in code:

// We first learn the variables for the trail and the radius.
const factors = properties.get(‘–path’).toString().cut up(‘,’);
const r = parseFloat(properties.get(‘–radius’).worth);

var Ppoints = [];
var Cpoints = [];
const w = measurement.width;
const h = measurement.peak;
var N = factors.size;
var i;
// Then we loop by way of the factors to create two arrays.
for (i = 0; i < N; i++) {
var j = i-1;
if(j<0) j=N-1;

var p = factors[i].trim().cut up(/(?!(.*)s(?![^(]*?))/g);
// One defines the purple factors (Ppoints)
p = cc(p[0],p[1]);
var pj = factors[j].trim().cut up(/(?!(.*)s(?![^(]*?))/g);
pj = cc(pj[0],pj[1]);
// The opposite defines the inexperienced factors (Cpoints)

/* … */

// Utilizing the arcTo() operate to create the form
for (i = 0; i < (Cpoints.size – 1); i++) {
ctx.arcTo(Ppoints[i][0], Ppoints[i][1], Cpoints[i+1][0],Cpoints[i+1][1], r);
ctx.arcTo(Ppoints[i][0], Ppoints[i][1], Cpoints[0][0],Cpoints[0][1], r);

/* … */

ctx.fillStyle = ‘#000’;

The final step is to fill our form with a stable shade. Now we’ve got our rounded form and we are able to use it as a masks on any factor.

That’s it! Now all we’ve got to do is to construct our form and management the radius like we would like — a radius that we are able to animate, because of @property which can make issues extra fascinating!

Dwell Demo

Are there any drawbacks with this methodology?

Sure, there are drawbacks, and also you in all probability seen them within the final instance. The primary downside is said to the hover-able space. Since we’re utilizing masks, we are able to nonetheless work together with the preliminary rectangular form. Keep in mind, we confronted the identical difficulty with the polygon border and we used clip-path to repair it. Sadly, clip-path doesn’t assist right here as a result of it additionally impacts the rounded nook.

Let’s take the final instance and add clip-path. Discover how we’re shedding the “inward” curvature.

CodePen Embed Fallback

There’s no difficulty with the hexagon and triangle shapes, however the others are lacking some curves. It may very well be an fascinating function to maintain solely the outward curvature — because of clip-path— and on the similar time we repair the hover-able space. However we can not maintain all of the curvatures and scale back the hover-able space on the similar time.

The second difficulty? It’s associated to the usage of an enormous radius worth. Hover over the shapes under and see the loopy outcomes we get:

CodePen Embed Fallback

It’s really not a “main” downside since we’ve got management over the radius, nevertheless it positive could be good to keep away from such a state of affairs in case we wrongly use an excessively giant radius worth. We may repair this by limiting the worth of the radius to inside a variety that caps it at a most worth. For every nook, we calculate the radius that permits us to have the most important arc with none overflow. I gained’t dig into the mathematics logic behind this (😱), however right here is the ultimate code to cap the radius worth:

var angle =
Math.atan2(Cpoints[i+1][1] – Ppoints[i][1], Cpoints[i+1][0] – Ppoints[i][0]) –
Math.atan2(Cpoints[i][1] – Ppoints[i][1], Cpoints[i][0] – Ppoints[i][0]);
if (angle < 0) {
angle += (2*Math.PI)
if (angle > Math.PI) {
angle = 2*Math.PI – angle
var distance = Math.min(
(Cpoints[i+1][1] – Ppoints[i][1]) ** 2 +
(Cpoints[i+1][0] – Ppoints[i][0]) ** 2),
(Cpoints[i][1] – Ppoints[i][1]) ** 2 +
(Cpoints[i][0] – Ppoints[i][0]) ** 2)
var rr = Math.min(distance * Math.tan(angle/2),r);

r is the radius we’re defining and rr is the radius we’re really utilizing. It equal both to r or the utmost worth allowed with out overflow.

CodePen Embed Fallback

If you happen to hover the shapes in that demo, we now not get unusual shapes however the “most rounded form” (I simply coined this) as a substitute. Discover that the common polygons (just like the triangle and hexagon) logically have a circle as their “most rounded form” so we are able to have cool transitions or animations between completely different shapes.

Can we’ve got borders?

Sure! All we’ve got to do is to make use of stroke() as a substitute of fill() inside our paint() operate. So, as a substitute of utilizing:

ctx.fillStyle = ‘#000’;

…we use this:

ctx.lineWidth = b;
ctx.strokeStyle = ‘#000’;

This introduces one other variable, b, that controls the border’s thickness.

CodePen Embed Fallback

Did you discover that we’ve got some unusual overflow? We confronted the identical difficulty within the earlier article, and that attributable to how stroke() works. I quoted MDN in that article and can do it once more right here as nicely:

Strokes are aligned to the middle of a path; in different phrases, half of the stroke is drawn on the inside facet, and half on the outer facet.

Once more, it’s that “half inside facet, half outer facet” that’s getting us! With a purpose to repair it, we have to disguise the outer facet utilizing one other masks, the primary one the place we use the fill(). First, we have to introduce a conditional variable to the paint() operate so as to select if we need to draw the form or solely its border.

Right here’s what we’ve got:

if(t==0) {
ctx.fillStyle = ‘#000’;
} else {
ctx.lineWidth = 2*b;
ctx.strokeStyle = ‘#000’;

Subsequent, we apply the primary kind of masks (t=0) on the primary factor, and the second kind (t=1) on a pseudo-element. The masks utilized on the pseudo-element produces the border (the one with the overflow difficulty). The masks utilized on the primary factor addresses the overflow difficulty by hiding the outer a part of the border. And when you’re questioning, that’s why we’re including twice the border thickness to lineWidth.

Dwell Demo

See that? Now we have excellent rounded shapes as outlines and we are able to alter the radius on hover. And may use any sort of background on the form.

And we did all of it with a little bit of CSS:

div {
–radius: 5px; /* Defines the radius */
–border: 6px; /* Defines the border thickness */
–path: /* Outline your form right here */;
–t: 0; /* The primary masks on the primary factor */

-webkit-mask: paint(rounded-shape);
transition: –radius 1s;
div::earlier than {
content material: “”;
background: ..; /* Use any background you need */
–t: 1; /* The second masks on the pseudo-element */
-webkit-mask: paint(rounded-shape); /* Take away this if you’d like the complete form */
div[class]:hover {
–radius: 80px; /* Transition on hover */

Let’s not overlook that we are able to simply introduce dashes utilizing setLineDash() the identical method we did within the earlier article.

Dwell Demo

Controlling the radius

In all of the examples we’ve checked out, we at all times think about one radius utilized to all of the corners of every form. It might be fascinating if we may management the radius of every nook individually, the identical method the border-radius property takes as much as 4 values. So let’s lengthen the –path variable to contemplate extra parameters.

Truly, our path may be expressed as an inventory of [x y] values. We’ll make an inventory of [x y r] values the place we introduce a 3rd worth for the radius. This worth isn’t necessary; if omitted, it falls again to the primary radius.

.field {
show: inline-block;
peak: 200px;
width: 200px;

–path: 50% 0 10px,100% 100% 5px,0 100%;
–radius: 20px;
-webkit-mask: paint(rounded-shape);

Above, we’ve got a 10px radius for the primary nook, 5px for the second, and since we didn’t specify a price for the third nook, it inherits the 20px outlined by the –radius variable.

Right here’s our JavaScript for the values:

var Radius = [];
// …
var p = factors[i].trim().cut up(/(?!(.*)s(?![^(]*?))/g);

This defines an array that shops the radius of every nook. Then, after splitting the worth of every level, we take a look at whether or not we’ve got a 3rd worth (p[2]). If it’s outlined, we use it; if not, we use the default radius. Afterward, we’re utilizing Radius[i] as a substitute of r.

Dwell Demo

This minor addition is a pleasant function for after we need to disable the radius for a selected nook of the form. In actual fact, let’s take a look at a number of completely different examples subsequent.

Extra examples!

I made a sequence of demos utilizing this trick. I like to recommend setting the radius to 0 to higher see the form and perceive how the trail is created. Keep in mind that the –path variable behaves the identical method as the trail we outline inside clip-path: polygon(). If you happen to’re in search of a path to play with, strive utilizing Clippy to generate one for you.

Instance 1: CSS shapes

A whole lot of fancy shapes may be created utilizing this system. Listed here are a number of of them executed with none further components, pseudo-elements, or hack-y code.

CodePen Embed Fallback

Instance 2: Speech bubble

In the earlier article, we added border to a speech bubble factor. Now we are able to enhance it and around the corners utilizing this new methodology.

CodePen Embed Fallback

If you happen to examine with this instance with the unique implementation, you might discover the very same code. I merely made two or three modifications to the CSS to make use of the brand new Worklet.

Instance 3: Frames

Discover under some cool frames in your content material. No extra complications after we want gradient borders!

CodePen Embed Fallback

Merely play with the –path variable to create your individual responsive body with any coloration your need.

Instance 4: Part divider

SVG is now not wanted to create these wavy part dividers which are standard today.

CodePen Embed Fallback

Discover that the CSS is mild and comparatively easy. I solely up to date the trail to generate new cases of the divider.

Instance 5: Navigation menu

Right here’s a traditional design sample that I’m positive many people have ran into at a while: How on earth will we invert the radius? You’ve possible seen it in navigation designs.

CodePen Embed Fallback

A barely completely different tackle it:

CodePen Embed Fallback

Instance 6: Gooey impact

If we play with the trail values we are able to attain for some fancy animation.
Under an thought the place I’m making use of a transition to just one worth of the trail and but we get a fairly cool impact

CodePen Embed Fallback

This one’s impressed by Ana Tudor’s demo.

One other thought with a distinct animation

CodePen Embed Fallback

One other instance with a extra complicated animation:

CodePen Embed Fallback

What a few bouncing ball

CodePen Embed Fallback

Instance 7: Form morphing

Enjoying with huge radius values permits us to create cool transitions between completely different shapes, particularly between a circle and an everyday polygon.

CodePen Embed Fallback

If we add some border animation, we get “respiratory” shapes!

CodePen Embed Fallback

Let’s spherical this factor up

I hope you’ve loved getting nerdy with the CSS Paint API. All through this sequence, we’ve utilized paint() to a bunch of real-life examples the place having the API permits us to control components in a method we’ve by no means been capable of do with CSS — or with out resorting to hacks or loopy magic numbers and whatnot. I really imagine the CSS Paint API makes seemingly difficult issues so much simpler to unravel in an easy method and can be a function we attain for again and again. That’s, when browser help catches as much as it.

If you happen to’ve adopted together with this sequence, and even simply stumbled into this one article, I’d like to know what you consider the CSS Paint API and the way you think about utilizing it in your work. Are there any present design tendencies that may profit from it, just like the wavy part dividers? Or blobby designs? Experiment and have enjoyable!

CodePen Embed Fallback

This one’s taken from my earlier article

Exploring the CSS Paint API sequence:

Half 1: Picture Fragmentation ImpactHalf 2: Blob AnimationHalf 3: Polygon BorderHalf 4: Rounding Shapes (you might be right here!)

The put up Exploring the CSS Paint API: Rounding Shapes appeared first on CSS-Tips. You may help CSS-Tips by being an MVP Supporter.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment