Boolean Operations

When a shape you need is not among the standard shapes you will have to create it yourself. The first impulse might be to to use 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. With boolean operation you can do exactly that; combine basic shapes to create more complex ones.

Subpaths

Sketch has dynamic boolean operations and before we can discuss those in more detail we have quickly revisit vector shapes. Most vector shapes in Sketch will consist of only one series of points; one path so to speak. However a shape can have as many subpaths as you want and the how the resulting shape will look depends on how these are combined.

When you perform a boolean operation in Sketch, it will add the topmost shape as a subpath of the second shape and use the particular boolean operation. Because boolean operations in Sketch are dynamic you can always adjust the subpaths and if one of your shapes is a rectangle, you can adjust the corner radius for this single subpath.

Operations

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

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

To help better explain boolean operations, this video outlines the operations explained above:

Layer List

When you have a shape with multiple subpaths in your canvas take a look at it in the layer list. Just like with normal groups you will see a disclosure arrow on the left and if you expand that you will see a list of subpaths for your shape. On the right side you pick a boolean operation for each subpath individually.

As the layer list is ordered from bottom to top; the order of the subpaths is the same. The boolean operation you pick will combine the layer with the layer below. The result of that will be passed to the subpath above if there is any.

Flattening

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 in flattening all the same, the subpaths you had will be replaced by different subpaths, maybe fewer paths, maybe the same number of paths.

If you’re coming from other design apps you might be inclined to flatten a path after you have applied a boolean operation. In Sketch there’s no need for that; you can apply as many boolean operations on top of one-another without the need to flatten.