Debug panel

Close debug panel
Roma’s Unpolished Posts

Boolean Operations On Shapes

Published on:
Categories:
Wishlist 5, CSS 103
Current music:
Haru Nemuri — 
Take Back The World
Current drink:
Peppermint Tea

When it comes to SVG or CSS shapes, mask, and so on, I have no idea what I am doing. Their syntax is often so convoluted!

Like, take one of the many articles that Temani Afif wrote, for example “Better CSS Shapes Using shape() — Part 2: More on Arcs” — like… why? I understand that the shape syntax comes from SVG, and there it was made to be consise, but if we were to think about the priority of constituencies…

Anyways. I am a simple person. But I still want to use shapes. The shape() syntax is great, and is already more accessible than the SVG path shorthands.

But what if we could do it much easier?

Ok, CSS some other basic shapes like rect(), circle(), xywh() (that one is fun).

When you open any graphical editor that has shapes, you know what you will find there?

Boolean operations on those shapes.

Unions, subtractions, exclusions, intersections… All that.

Let’s say we had two simple shapes: square, and a circle. We can use them as basic shapes to clip a certain part of some element:

<div>
	<div class="shape square"></div>
	<div class="shape circle"></div>
</div>
.shape {
	display: inline-block;
	width: 150px;
	height: 150px;
	margin: 0.5rem;

	background: linear-gradient(deepPink, lightGreen);
}
.shape.square {
	clip-path: rect(0 100px 100px 0);
}
.shape.circle {
    clip-path: circle(50px at 100px 100px);
}

And now… what if wanted to achieve this:

And do it only inside the shape() syntax — so it could be used anywhere it could be applied?

Here is how I did it — involving some Cyclic Toggles with some space toggles boolean algebra (it felt particularly thematic here):

.boolean {
	--type: var(--add);
	--circle: var(--type,);
	--square: var(--type,);
	--add: var(--type,);
	--subtract: var(--type,);
	--intersect: var(--type,);
	--exclude: var(--type,);

	--full-circle:
		arc by 100px 0 of 50px var(--add, cw),
		arc by -100px 0 of 50px var(--add, cw),
		var(--subtract)
		var(--intersect)
	;

	--quarter-circle:
		arc to 100px 50px of 50px cw,
		line to 100px 100px,
		var(--add)
		var(--exclude)
	;

	--not-intersect:
		0 0,
		hline to 100px,
		vline to 100px,
		hline to 0,
		close,
		move to
		var(--intersect)
	;

	--boolean:
		shape(
			var(--subtract, evenodd)

			from var(--not-intersect,) 50px 100px,
			var(--full-circle,)
			var(--quarter-circle,)
			close
		)
		var(--circle)
		var(--square)
	;

	clip-path:
		var(--square,
			rect(0 100px 100px 0)
		)
		var(--circle,
			circle(50px at 100px 100px)
		)
		var(--boolean,)
	;
}

Don’t ask me how this works — a lot of trial & error. Although, closer to the end of it, I started to understand something.

But… what if it could be just something like

.shape.add {
	clip-path: add(
		rect(0 100px 100px 0),
		circle(50px at 100px 100px)
	);
}
.shape.subtract {
	clip-path: subtract(
		rect(0 100px 100px 0),
		circle(50px at 100px 100px)
	);
}
.shape.exclude {
	clip-path: exclude(
		rect(0 100px 100px 0),
		circle(50px at 100px 100px)
	);
}
.shape.intersect {
	clip-path: intersect(
		rect(0 100px 100px 0),
		circle(50px at 100px 100px)
	);
}
Not real syntax.

We could take any other shapes (including other results of boolean operations) and just — whoosh — combine them? Pen, pineapple, apple, pen, all that.

As an author, I would love to be able to use shapes this way. It could open so many doors where the existing syntax requires purchasing a set of lockpicks from Temani.

I tried looking for a CSS Working Group issue about this, and the closest I could find was this one from Bramus, but it was not exactly it.

Although, in its comments, Simon Fraser proposed something closer to what I want: compound-path(). Although, it was still using the windRule, which is that weird evenodd/nonzero thingy I don’t understand.

Boolean operations? Those I do understand.

So, I opened a new issue about them: “[css-shapes-2] Boolean operations on sha”.

Feel free to provide your own examples there. I remember, for example, Yuan Chuan was playing with adding those boolean operations to the CSS Doodle’s @shape.

But we really need this in native CSS.

Please share your thoughts about this on Mastodon!