The example above draws an ellipse that just fits inside a canvas that’s constrained to 300 points wide and 200 points tall:
In addition to outlining or filling paths, you can draw images, text, and SwiftUI views. You can also use the context to perform many common graphical operations, like adding masks, applying filters and transforms, and setting a blend mode. For example you can add a mask using the clip(to:style:options:) method:
The rectangular mask hides all but one quadrant of the ellipse:
The order of operations matters. Changes that you make to the state of the context, like adding a mask or a filter, apply to later drawing operations. If you reverse the fill and clip operations in the example above, so that the fill comes first, the mask doesn’t affect the ellipse.
Each context references a particular layer in a tree of transparency layers, and also contains a full copy of the drawing state. You can modify the state of one context without affecting the state of any other, even if they refer to the same layer. For example you can draw the masked ellipse from the previous example into a copy of the main context, and then add a rectangle into the main context:
// Create a copy of the context to draw a clipped ellipse.var maskedContext = contextlet halfSize = size.applying(CGAffineTransform(scaleX: 0.5, y: 0.5))maskedContext.clip(to: Path(CGRect(origin: .zero, size: halfSize)))maskedContext.fill(Path(ellipseIn: CGRect(origin: .zero, size: size)), with: .color(.green))// Go back to the original context to draw the rectangle.let origin =CGPoint(x: size.width /4, y: size.height /4)context.fill(Path(CGRect(origin: origin, size: halfSize)), with: .color(.blue))
The mask doesn’t clip the rectangle because the mask isn’t part of the main context. However, both contexts draw into the same view because you created one context as a copy of the other:
The context has access to an EnvironmentValues instance called environment that’s initially copied from the environment of its enclosing view. SwiftUI uses environment values — like the display resolution and color scheme — to resolve types like Image and Color that appear in the context. You can also access values stored in the environment for your own purposes.