Boolean Operations

When the shape you need is not among the standard shapes (rectangles, ovals, etc) you will have to create it yourself. The first impulse might be to to use the Vector tool and draw the shape from scratch. However you will find that many times, a complex shape is easy to break down into the basic shapes. Think of a donut, which is two circles, one big one, and a smaller one making the hole.

In case of the donut you want the second path (the small one) to be subtracted from the first, but that may not always be the case. Depending on what you’re trying to achieve you may want to make the shape bigger; think of a Christmas tree as a number of stacked triangles that appear as one shape for example. The ways simple shapes can be combined into complex ones are called Boolean Operations.

Boolean Operations

There are four different boolean operations and the one you will choose depends on the situation.

Boolean operations
  • Union: The result is a vector that is the sum of both vectors’ areas.
  • Subtract: The result is a vector where the area of the top shape is removed from the one under it.
  • Intersect: The result is a vector consisting of the parts where the original shapes overlapped.
  • Difference: The result is a vector that is exactly the part where they didn’t overlap. It’s the inverse of an intersect operation.

Each of the four boolean operations can also be accessed from the Touch Bar when multiple shape layers are selected, along with options to align them.

Boolean operations on the Touch Bar

Combined Shapes

A Combined Shape is what Sketch calls the shapes that are actually built from multiple simpler shapes, which we call subpaths. A combined shape can have as many subpaths as you want.

When you perform a boolean operation in Sketch, it will create a new combined shape with the original shapes as its subpaths. The boolean operation you made the combined shape with is applied to the topmost layer.

You will remember that layers are rendered in Sketch from the bottom to the top; that is why a layer at the top of the list will draw on top of a layer at the bottom. Boolean operations work the same way. A final path for a combined shape is arrived at by starting by the subpath at the bottom, and then applying each subsequent subpath to it with the boolean operation it specified. This is why a donut combined shape was a subtract operation on the topmost smaller layer, because it is subtracted from the base circle.

Note that combined shapes themselves can be combined into new combined shapes. This way it is easy to create a more complex path that you can’t express in a combined shape of only a single hierarchy; you could take two donuts and union them together for example.

When editing a shape with subpaths with the Vector Tool, they can all be edited at once — simply enter the editing mode. New shape layers can be added whilst in the mode, and they’ll be added above the currently selected subpath with a Union operation. Because boolean operations in Sketch are dynamic you can always adjust the subpaths just as you could for any other shape layer.

Layer List

When you have a combined shape with multiple subpaths in your Canvas, take a look at the Layer List. Just like with normal group layers you will see a disclosure triangle on the left. If you click it, you will reveal the of subpaths for your shape. The icon on the right will allow you pick a boolean operation for each subpath individually from the pop-up menu.

As the Layer List is ordered from bottom to top; the order of the subpaths is the same. A boolean operation on a subpath gives an indication how it will be combined with the result of the layer(s) that came before it.

Boolean operations

You can also create a combined shape by dropping one shape on top of another in the Layer List, and the effect is the same as using the toolbar icon with a Union operation as the default.

It is also possible to hide subpaths from a shape via the Layer List. When a subpath is hidden it will change the way the shape appears, much like deleting a subpath, but this behaviour is not destructive. Subpaths can be hidden by Control-clicking it in the Layer List and choosing “Hide Layer” from the shortcut menu, as well as the Shift-Command-H keyboard shortcut. Showing the subpath again will remember what operation was applied to it before it was hidden.

Flattening Shapes

When you flatten a shape in Sketch, it will try to represent the multiple paths inside the shape as one path; it will, so to speak, ‘flatten’ the hierarchy. However there are many paths that can’t be flattened into one. A shape with a hole inside it will always have to be represented as two paths; one for outer path, and one for the inner path.

When Sketch cannot flatten a path it will warn when you try to do so. If you persist, the subpaths you had will be replaced by different subpaths.

If you’re used to other design tools, you may automatically look to flatten a path after you have applied a boolean operation. In Sketch there’s no need for that; with nesting combined shapes you can apply as many boolean operations after each other as you need while keeping each path independent and editable.

Combining other kinds of Layers

Up until now we’ve always spoken of “subpaths” of a combined shape, but this is only half true. In fact you also combine shapes with Text Layers and Symbols and those are non-destructively vectorised on the fly. So you can subtract a shape from a symbol or mask a text layer with a shape.