One Logo Pool Ball
SwiftUICore · IOS API REFERENCE

GraphicsContext

struct · iOS 15.0+ · SDK 27.1

@frozen struct GraphicsContext

An immediate mode drawing destination, and its current state. Use a context to execute 2D drawing primitives. For example, you can draw filled shapes using the ``fill(_:with:style:)`` method inside a ``Canvas`` view: Canvas { context, size in context.fill( Path(ellipseIn: CGRect(origin: .zero, size: size)), with: .color(.green)) } .frame(width: 300, height: 200) The example above draws an ellipse that just fits inside a canvas that's constrained to 300 points wide and 200 points tall: ![A screenshot of a view that shows a green ellipse.](GraphicsContext-1) 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: let halfSize = size.applying(CGAffineTransform(scaleX: 0.5, y: 0.5)) context.clip(to: Path(CGRect(origin: .zero, size: halfSize))) context.fill( Path(ellipseIn: CGRect(origin: .zero, size: size)), with: .color(.green)) The rectangular mask hides all but one quadrant of the ellipse: ![A screenshot of a view that shows the upper left quarter of a green ellipse.](GraphicsContext-2) 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 = context let 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: ![A screenshot of a view that shows the upper left quarter of a green ellipse, overlaid by a blue rectangle centered in the view.](GraphicsContext-3) 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.

Members (37)

method · iOS 15.0+

clipToLayer

TypeScript
clipToLayer(opacity: number, options: GraphicsContext_ClipOptions, content: ((arg0: GraphicsContext) => void)): void
Swift declaration
mutating func clipToLayer(opacity: Double = 1, options: GraphicsContext.ClipOptions = ClipOptions(), content: (inout GraphicsContext) throws -> Void) rethrows

Adds a clip shape that you define in a new layer to the context's array of clip shapes. Call this method to add a shape to the array of clip shapes that the context uses to define a clipping mask. Shapes that you add affect only subsequent drawing operations. - Parameters: - opacity: A value that SwiftUI uses to multiply the alpha channel of the rasterized layer that you define in the `content` closure. The alpha values that result define the clip shape. - options: A set of options that tell SwiftUI how to interpret the clip shape. For example, you can invert the clip shape by setting the ``ClipOptions/inverse`` option. - content: A closure that receives as input a new ``GraphicsContext``, which represents a new transparency layer. The alpha channel of content that you draw into this context, multiplied by the `opacity` parameter, defines the clip shape.

method · iOS 15.0+

concatenate

TypeScript
concatenate(matrix: _CoreFoundation_CGAffineTransform): void
Swift declaration
mutating func concatenate(_ matrix: CGAffineTransform)

Appends the given transform to the context's existing transform. Calling this method is equivalent to updating the context's ``transform`` directly using the `matrix` parameter: transform = matrix.concatenating(transform) - Parameter matrix: A transform to append to the existing transform.

get · iOS 15.0+

environment

TypeScript
get environment(): EnvironmentValues
Swift declaration
var environment: EnvironmentValues { get set }

The environment associated with the graphics context. SwiftUI initially sets this to the environment of the context's enclosing view. The context uses values like display resolution and the color scheme from the environment to resolve types like ``Image`` and ``Color``. You can also access values stored in the environment for your own purposes.

method · iOS 15.0+

translateBy

TypeScript
translateBy(x: number, y: number): void
Swift declaration
mutating func translateBy(x: CGFloat, y: CGFloat)

Moves subsequent drawing operations by an amount in each dimension. Calling this method is equivalent to updating the context's ``transform`` directly using the given translation amount: transform = transform.translatedBy(x: x, y: y) - Parameters: - x: The amount to move in the horizontal direction. - y: The amount to move in the vertical direction.

method · iOS 15.0+

resolveSymbol

TypeScript
resolveSymbol(id: number): (GraphicsContext_ResolvedSymbol) | null
Swift declaration
func resolveSymbol<ID>(id: ID) -> GraphicsContext.ResolvedSymbol? where ID : Hashable

Gets the identified child view as a resolved symbol, if the view exists. - Parameter id: The value that you used to tag the view when you define it in the `symbols` parameter of the ``Canvas`` initializer ``Canvas/init(opaque:colorMode:rendersAsynchronously:renderer:symbols:)``. - Returns: The resolved symbol, or `nil` if SwiftUI can't find a child view with the given `id`.

method · iOS 15.0+

withCGContext

TypeScript
withCGContext(content: ((arg0: _CoreGraphics_CGContext) => void)): void
Swift declaration
func withCGContext(content: (CGContext) throws -> Void) rethrows

Provides a Core Graphics context that you can use as a proxy to draw into this context. Use this method to use existing drawing code that relies on Core Graphics primitives. - Parameter content: A closure that receives a <doc://com.apple.documentation/documentation/CoreGraphics/CGContext> that you use to perform drawing operations, just like you draw into a ``GraphicsContext`` instance. Any filters, blend mode settings, clip masks, and other state set before calling `withCGContext(content:)` apply to drawing operations in the Core Graphics context as well. Any state you set on the Core Graphics context is lost when the closure returns. Accessing the Core Graphics context after the closure returns produces undefined behavior.

get · iOS 15.0+

clipBoundingRect

TypeScript
get clipBoundingRect(): { x: number; y: number; width: number; height: number }
Swift declaration
var clipBoundingRect: CGRect { get }

The bounding rectangle of the intersection of all current clip shapes in the current user space.

method · iOS 15.0+

clip

TypeScript
clip(path: Path, style?: FillStyle, options?: GraphicsContext_ClipOptions): void
Swift declaration
mutating func clip(to path: Path, style: FillStyle = FillStyle(), options: GraphicsContext.ClipOptions = ClipOptions())

Adds a path to the context's array of clip shapes. Call this method to add a shape to the array of clip shapes that the context uses to define a clipping mask. Shapes that you add affect only subsequent drawing operations. - Parameters: - path: A ``Path`` that defines the shape of the clipping mask. - style: A ``FillStyle`` that defines how to rasterize the shape. - options: Clip options that tell SwiftUI how to interpret the `path` as a clip shape. For example, you can invert the clip shape by setting the ``ClipOptions/inverse`` option.

method · iOS 15.0+

draw

TypeScript
drawAtAnchor(text: Text, point: { x: number; y: number }, anchor?: UnitPoint): void
Swift declaration
func draw(_ text: Text, at point: CGPoint, anchor: UnitPoint = .center)

Draws text into the context, aligning an anchor within the ideal size of the rendered text to a point in the context. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the text. - Parameters: - text: The ``Text`` view to draw. Before drawing, the method converts the view to ``ResolvedText`` by calling ``resolve(_:)-4dx65``. - point: A point within the rectangle of the resolved text to anchor to a point in the context. - anchor: A ``UnitPoint`` within the context to align the text with. The default is ``UnitPoint/center``.

method · iOS 15.0+

draw

TypeScript
drawAtAnchor95337(image: Image, point: { x: number; y: number }, anchor?: UnitPoint): void
Swift declaration
func draw(_ image: Image, at point: CGPoint, anchor: UnitPoint = .center)

Draws an image into the context, aligning an anchor within the image to a point in the context. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the image. - Parameters: - image: The ``Image`` to draw. Before drawing, the method converts the image to a ``ResolvedImage`` by calling ``resolve(_:)-898z6``. - point: A point within the rectangle of the resolved image to anchor to a point in the context. - anchor: A ``UnitPoint`` within the context to align the image with. The default is ``UnitPoint/center``.

method · iOS 15.0+

draw

TypeScript
drawAtAnchor21319(text: GraphicsContext_ResolvedText, point: { x: number; y: number }, anchor?: UnitPoint): void
Swift declaration
func draw(_ text: GraphicsContext.ResolvedText, at point: CGPoint, anchor: UnitPoint = .center)

Draws resolved text into the context, aligning an anchor within the ideal size of the text to a point in the context. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the text. - Parameters: - text: The ``ResolvedText`` to draw. Get resolved text from a ``Text`` view by calling ``resolve(_:)-4dx65``. Alternatively, you can call ``draw(_:at:anchor:)-5dgmd`` with a ``Text`` view, and that method performs the resolution automatically. - point: A point within the rectangle of the ideal size of the resolved text to anchor to a point in the context. - anchor: A ``UnitPoint`` within the context to align the text with. The default is ``UnitPoint/center``.

method · iOS 15.0+

draw

TypeScript
drawAtAnchor47802(image: GraphicsContext_ResolvedImage, point: { x: number; y: number }, anchor?: UnitPoint): void
Swift declaration
func draw(_ image: GraphicsContext.ResolvedImage, at point: CGPoint, anchor: UnitPoint = .center)

Draws a resolved image into the context, aligning an anchor within the image to a point in the context. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the image. - Parameters: - image: The ``ResolvedImage`` to draw. Get a resolved image from an ``Image`` by calling ``resolve(_:)-898z6``. Alternatively, you can call ``draw(_:at:anchor:)-7l217`` with an ``Image``, and that method performs the resolution automatically. - point: A point within the rectangle of the resolved image to anchor to a point in the context. - anchor: A ``UnitPoint`` within the context to align the image with. The default is ``UnitPoint/center``.

method · iOS 15.0+

draw

TypeScript
drawAtAnchor50030(symbol: GraphicsContext_ResolvedSymbol, point: { x: number; y: number }, anchor?: UnitPoint): void
Swift declaration
func draw(_ symbol: GraphicsContext.ResolvedSymbol, at point: CGPoint, anchor: UnitPoint = .center)

Draws a resolved symbol into the context, aligning an anchor within the symbol to a point in the context. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the symbol. - Parameters: - symbol: The ``ResolvedSymbol`` view to draw. Get a resolved symbol by calling ``resolveSymbol(id:)`` with the identifier that you use to tag the corresponding child view during ``Canvas`` initialization. - point: A point within the rectangle of the resolved symbol to anchor to a point in the context. - anchor: A ``UnitPoint`` within the context to align the symbol with. The default is ``UnitPoint/center``.

method · iOS 15.0+

draw

TypeScript
drawInStyle(image: Image, rect: { x: number; y: number; width: number; height: number }, style?: FillStyle): void
Swift declaration
func draw(_ image: Image, in rect: CGRect, style: FillStyle = FillStyle())

Draws an image into the context, using the specified rectangle as a layout frame. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the image. - Parameters: - image: The ``Image`` to draw. Before drawing, the method converts the image to a ``ResolvedImage`` by calling ``resolve(_:)-898z6``. - rect: The rectangle in the current user space to draw the image in. - style: A fill style to use when rasterizing the image.

method · iOS 15.0+

draw

TypeScript
drawInStyle25258(image: GraphicsContext_ResolvedImage, rect: { x: number; y: number; width: number; height: number }, style?: FillStyle): void
Swift declaration
func draw(_ image: GraphicsContext.ResolvedImage, in rect: CGRect, style: FillStyle = FillStyle())

Draws a resolved image into the context, using the specified rectangle as a layout frame. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the image. - Parameters: - image: The ``ResolvedImage`` to draw. Get a resolved image from an ``Image`` by calling ``resolve(_:)-898z6``. Alternatively, you can call ``draw(_:in:style:)-blhz`` with an ``Image``, and that method performs the resolution automatically. - rect: The rectangle in the current user space to draw the image in. - style: A fill style to use when rasterizing the image.

method · iOS 15.0+

draw

TypeScript
drawIn(text: Text, rect: { x: number; y: number; width: number; height: number }): void
Swift declaration
func draw(_ text: Text, in rect: CGRect)

Draws text into the context using the specified rectangle as a layout frame. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the text. - Parameters: - text: The ``Text`` view to draw. Before drawing, the method converts the view to ``ResolvedText`` by calling ``resolve(_:)-4dx65``. - rect: The rectangle in the current user space to draw the text in.

method · iOS 15.0+

draw

TypeScript
drawIn93574(text: GraphicsContext_ResolvedText, rect: { x: number; y: number; width: number; height: number }): void
Swift declaration
func draw(_ text: GraphicsContext.ResolvedText, in rect: CGRect)

Draws resolved text into the context using the specified rectangle as a layout frame. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the text. - Parameters: - text: The ``ResolvedText`` to draw. Get resolved text from a ``Text`` view by calling ``resolve(_:)-4dx65``. Alternatively, you can call ``draw(_:in:)-5opqf`` with a ``Text`` view, and that method performs the resolution automatically. - rect: The rectangle in the current user space to draw the text in.

method · iOS 15.0+

draw

TypeScript
drawIn18655(symbol: GraphicsContext_ResolvedSymbol, rect: { x: number; y: number; width: number; height: number }): void
Swift declaration
func draw(_ symbol: GraphicsContext.ResolvedSymbol, in rect: CGRect)

Draws a resolved symbol into the context, using the specified rectangle as a layout frame. The current context state defines the full drawing operation. For example, the current transformation and clip shapes affect how SwiftUI draws the symbol. - Parameters: - symbol: The ``ResolvedSymbol`` to draw. Get a resolved symbol by calling ``resolveSymbol(id:)`` with the identifier that you use to tag the corresponding child view during ``Canvas`` initialization. - rect: The rectangle in the current user space to draw the symbol in.

method · iOS 17.0+

draw

TypeScript
drawOptions(run: Text_Layout_Run, options?: Text_Layout_DrawingOptions): void
Swift declaration
func draw(_ run: Text.Layout.Run, options: Text.Layout.DrawingOptions = .init())

Draws `run` into the graphics context.

method · iOS 17.0+

draw

TypeScript
drawOptions75326(line: Text_Layout_Line, options?: Text_Layout_DrawingOptions): void
Swift declaration
func draw(_ line: Text.Layout.Line, options: Text.Layout.DrawingOptions = .init())

Draws `line` into the graphics context.

method · iOS 17.0+

draw

TypeScript
drawOptions63387(slice: Text_Layout_RunSlice, options?: Text_Layout_DrawingOptions): void
Swift declaration
func draw(_ slice: Text.Layout.RunSlice, options: Text.Layout.DrawingOptions = .init())

Draws `slice` into the graphics context.

method · iOS 15.0+

fill

TypeScript
fill(path: Path, shading: GraphicsContext_Shading, style?: FillStyle): void
Swift declaration
func fill(_ path: Path, with shading: GraphicsContext.Shading, style: FillStyle = FillStyle())

Draws a path into the context and fills the outlined region. The current drawing state of the context defines the full drawing operation. For example, the current transformation and clip shapes, and any styles applied to the result, affect the final result. - Parameters: - path: The outline of the region to fill. - shading: The color or pattern to use when filling the region bounded by `path`. - style: A style that indicates how to rasterize the path.

method · iOS 15.0+

rotate

TypeScript
rotate(angle: Angle): void
Swift declaration
mutating func rotate(by angle: Angle)

Rotates subsequent drawing operations by an angle. Calling this method is equivalent to updating the context's ``transform`` directly using the `angle` parameter: transform = transform.rotated(by: angle.radians) - Parameters: - angle: The amount to rotate.

method · iOS 15.0+

stroke

TypeScript
strokeWithStyle(path: Path, shading: GraphicsContext_Shading, style: StrokeStyle): void
Swift declaration
func stroke(_ path: Path, with shading: GraphicsContext.Shading, style: StrokeStyle)

Draws a path into the context with a specified stroke style. If you only need to control the style's ``StrokeStyle/lineWidth`` property, use ``stroke(_:with:lineWidth:)`` instead. - Parameters: - path: The path to outline. - shading: The color or pattern to use when outlining the `path`. - style: A style that indicates how to outline the path.

method · iOS 15.0+

stroke

TypeScript
strokeWithLineWidth(path: Path, shading: GraphicsContext_Shading, lineWidth?: number): void
Swift declaration
func stroke(_ path: Path, with shading: GraphicsContext.Shading, lineWidth: CGFloat = 1)

Draws a path into the context with a specified line width. When you call this method, all ``StrokeStyle`` properties other than ``StrokeStyle/lineWidth`` take their default values. To control other style properties, use ``stroke(_:with:style:)`` instead. - Parameters: - path: The path to outline. - shading: The color or pattern to use when outlining the `path`. - lineWidth: The width of the stroke, which defaults to `1`.

get · iOS 15.0+

opacity

TypeScript
get opacity(): number
Swift declaration
var opacity: Double { get set }

The opacity of drawing operations in the context. Set this value to affect the opacity of content that you subsequently draw into the context. Changing this value has no impact on the content you previously drew into the context.

set · iOS 15.0+

opacity

TypeScript
set opacity(value: number)
Swift declaration
var opacity: Double { get set }

The opacity of drawing operations in the context. Set this value to affect the opacity of content that you subsequently draw into the context. Changing this value has no impact on the content you previously drew into the context.

method · iOS 15.0+

resolve

TypeScript
resolve(text: Text): GraphicsContext_ResolvedText
Swift declaration
func resolve(_ text: Text) -> GraphicsContext.ResolvedText

Gets a version of a text view that's fixed with the current values of the graphics context's environment. You can measure the resolved text by calling its ``ResolvedText/measure(in:)`` method. You can draw the resolved text with the context's ``draw(_:in:)-69ad8`` or ``draw(_:at:anchor:)-6xr87`` method. - Parameter text: The ``Text`` view to resolve. - Returns: A text view that's resolved into the current context's environment, taking into account environment values like the display resolution and current color scheme.

method · iOS 15.0+

resolve

TypeScript
resolve38418(image: Image): GraphicsContext_ResolvedImage
Swift declaration
func resolve(_ image: Image) -> GraphicsContext.ResolvedImage

Gets a version of an image that's fixed with the current values of the graphics context's environment. You can measure the resolved image by looking at its ``ResolvedImage/size`` and ``ResolvedImage/baseline`` properties. You can draw the resolved image with the context's ``draw(_:in:style:)-7rvee`` or ``draw(_:at:anchor:)-1z5wt`` method. - Parameter image: The ``Image`` to resolve. - Returns: An image that's resolved into the current context's environment, taking into account environment values like the display resolution and current color scheme.

method · iOS 15.0+

resolve

TypeScript
resolve89852(shading: GraphicsContext_Shading): GraphicsContext_Shading
Swift declaration
func resolve(_ shading: GraphicsContext.Shading) -> GraphicsContext.Shading

Returns a version of a shading resolved with the current values of the graphics context's environment. Calling this function once and then drawing multiple times with the result will often have less overhead than drawing with the original shading multiple times.

method · iOS 15.0+

scaleBy

TypeScript
scaleBy(x: number, y: number): void
Swift declaration
mutating func scaleBy(x: CGFloat, y: CGFloat)

Scales subsequent drawing operations by an amount in each dimension. Calling this method is equivalent to updating the context's ``transform`` directly using the given scale factors: transform = transform.scaledBy(x: x, y: y) - Parameters: - x: The amount to scale in the horizontal direction. - y: The amount to scale in the vertical direction.

method · iOS 15.0+

addFilter

TypeScript
addFilter(filter: GraphicsContext_Filter, options?: GraphicsContext_FilterOptions): void
Swift declaration
mutating func addFilter(_ filter: GraphicsContext.Filter, options: GraphicsContext.FilterOptions = FilterOptions())

Adds a filter that applies to subsequent drawing operations. To draw with filtering, SwiftUI: - Rasterizes the drawing operation to an implicit transparency layer without blending, adjusting opacity, or applying any clipping. - Applies the filter to the layer containing the rasterized image. - Composites the layer onto the background, using the context's current blend mode, opacity setting, and clip shapes. When SwiftUI draws with a filter, the blend mode might apply to regions outside the drawing operation's intrinsic shape, but inside its clip shape. That might result in unexpected behavior for certain blend modes like ``GraphicsContext/BlendMode-swift.struct/copy``, where the drawing operation completely overwrites the background even if the source alpha is zero. - Parameters: - filter: A graphics context filter that you create by calling one of the ``Filter`` factory methods. - options: A set of options from ``FilterOptions`` that you can use to configure filter operations.

get · iOS 15.0+

blendMode

TypeScript
get blendMode(): GraphicsContext_BlendMode
Swift declaration
var blendMode: GraphicsContext.BlendMode { get set }

The blend mode used by drawing operations in the context. Set this value to affect how any content that you subsequently draw into the context blends with content that's already in the context. Use one of the ``GraphicsContext/BlendMode-swift.struct`` values.

set · iOS 15.0+

blendMode

TypeScript
set blendMode(value: GraphicsContext_BlendMode)
Swift declaration
var blendMode: GraphicsContext.BlendMode { get set }

The blend mode used by drawing operations in the context. Set this value to affect how any content that you subsequently draw into the context blends with content that's already in the context. Use one of the ``GraphicsContext/BlendMode-swift.struct`` values.

method · iOS 15.0+

drawLayer

TypeScript
drawLayer(content: ((arg0: GraphicsContext) => void)): void
Swift declaration
func drawLayer(content: (inout GraphicsContext) throws -> Void) rethrows

Draws a new layer, created by drawing code that you provide, into the context. - Parameter content: A closure that receives a new ``GraphicsContext`` as input. This context represents a new transparency layer that you can draw into. When the closure returns, SwiftUI draws the new layer into the current context.

get · iOS 15.0+

transform

TypeScript
get transform(): _CoreFoundation_CGAffineTransform
Swift declaration
var transform: CGAffineTransform { get set }

The current transform matrix, defining user space coordinates. Modify this matrix to transform content that you subsequently draw into the context. Changes that you make don't affect existing content.

set · iOS 15.0+

transform

TypeScript
set transform(value: _CoreFoundation_CGAffineTransform)
Swift declaration
var transform: CGAffineTransform { get set }

The current transform matrix, defining user space coordinates. Modify this matrix to transform content that you subsequently draw into the context. Changes that you make don't affect existing content.

Skipped declarations

1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

GraphicsContext.environment

The iOS SDK compiler rejected this binding: setter for 'environment' is only available in iOS 18.2 or newer

All SwiftUICore types →