<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/GraphicsContext",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI15GraphicsContextV"
  },
  "title" : "GraphicsContext"
}
-->

# GraphicsContext

An immediate mode drawing destination, and its current state.

```
@frozen struct GraphicsContext
```

## Overview

Use a context to execute 2D drawing primitives. For example, you can draw
filled shapes using the [`fill(_:with:style:)`](/documentation/SwiftUI/GraphicsContext/fill(_:with:style:)) method inside a [`Canvas`](/documentation/SwiftUI/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.](images/com.apple.SwiftUI/GraphicsContext-1@2x.png)

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:)`](/documentation/SwiftUI/GraphicsContext/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](images/com.apple.SwiftUI/GraphicsContext-2@2x.png)

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](images/com.apple.SwiftUI/GraphicsContext-3@2x.png)

The context has access to an [`EnvironmentValues`](/documentation/SwiftUI/EnvironmentValues) instance called
[`environment`](/documentation/SwiftUI/GraphicsContext/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`](/documentation/SwiftUI/Image) and
[`Color`](/documentation/SwiftUI/Color) that appear in the context. You can also access values stored
in the environment for your own purposes.

## Topics

### Drawing a path

[`stroke(_:with:lineWidth:)`](/documentation/SwiftUI/GraphicsContext/stroke(_:with:lineWidth:))

Draws a path into the context with a specified line width.

[`stroke(_:with:style:)`](/documentation/SwiftUI/GraphicsContext/stroke(_:with:style:))

Draws a path into the context with a specified stroke style.

[`fill(_:with:style:)`](/documentation/SwiftUI/GraphicsContext/fill(_:with:style:))

Draws a path into the context and fills the outlined region.

[`Shading`](/documentation/SwiftUI/GraphicsContext/Shading)

A color or pattern that you can use to outline or fill a path.

[`GradientOptions`](/documentation/SwiftUI/GraphicsContext/GradientOptions)

Options that affect the rendering of color gradients.

### Drawing images, text, and views

[`func   draw ( _ : in :)`](/documentation/SwiftUI/GraphicsContext/draw(_:in:))

Draws a resolved symbol into the context, using the specified rectangle
as a layout frame.

[`func   draw ( _ : in : style :)`](/documentation/SwiftUI/GraphicsContext/draw(_:in:style:))

Draws a resolved image into the context, using the specified rectangle
as a layout frame.

[`func   draw ( _ : at : anchor :)`](/documentation/SwiftUI/GraphicsContext/draw(_:at:anchor:))

Draws a resolved image into the context, aligning an anchor within the
image to a point in the context.

### Drawing into a new layer

[`drawLayer(content:)`](/documentation/SwiftUI/GraphicsContext/drawLayer(content:))

Draws a new layer, created by drawing code that you provide, into the
context.

### Resolving a drawn entity

[`func   resolve ( _ :)`](/documentation/SwiftUI/GraphicsContext/resolve(_:))

Gets a version of an image that’s fixed with the current values of
the graphics context’s environment.

[`resolveSymbol(id:)`](/documentation/SwiftUI/GraphicsContext/resolveSymbol(id:))

Gets the identified child view as a resolved symbol, if the view exists.

[`ResolvedSymbol`](/documentation/SwiftUI/GraphicsContext/ResolvedSymbol)

A static sequence of drawing operations that may be drawn
multiple times, preserving their resolution independence.

[`ResolvedImage`](/documentation/SwiftUI/GraphicsContext/ResolvedImage)

An image resolved to a particular environment.

[`ResolvedText`](/documentation/SwiftUI/GraphicsContext/ResolvedText)

A text view resolved to a particular environment.

### Masking

[`clip(to:style:options:)`](/documentation/SwiftUI/GraphicsContext/clip(to:style:options:))

Adds a path to the context’s array of clip shapes.

[`clipToLayer(opacity:options:content:)`](/documentation/SwiftUI/GraphicsContext/clipToLayer(opacity:options:content:))

Adds a clip shape that you define in a new layer to the context’s array
of clip shapes.

[`clipBoundingRect`](/documentation/SwiftUI/GraphicsContext/clipBoundingRect)

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

[`ClipOptions`](/documentation/SwiftUI/GraphicsContext/ClipOptions)

Options that affect the use of clip shapes.

### Setting opacity and the blend mode

[`opacity`](/documentation/SwiftUI/GraphicsContext/opacity)

The opacity of drawing operations in the context.

[`blendMode`](/documentation/SwiftUI/GraphicsContext/blendMode-swift.property)

The blend mode used by drawing operations in the context.

[`BlendMode`](/documentation/SwiftUI/GraphicsContext/BlendMode-swift.struct)

The ways that a graphics context combines new content with background
content.

### Filtering

[`addFilter(_:options:)`](/documentation/SwiftUI/GraphicsContext/addFilter(_:options:))

Adds a filter that applies to subsequent drawing operations.

[`Filter`](/documentation/SwiftUI/GraphicsContext/Filter)

A type that applies image processing operations to rendered content.

[`FilterOptions`](/documentation/SwiftUI/GraphicsContext/FilterOptions)

Options that configure a filter that you add to a graphics context.

[`BlurOptions`](/documentation/SwiftUI/GraphicsContext/BlurOptions)

Options that configure the graphics context filter that creates blur.

[`ShadowOptions`](/documentation/SwiftUI/GraphicsContext/ShadowOptions)

Options that configure the graphics context filter that creates shadows.

### Applying transforms

[`scaleBy(x:y:)`](/documentation/SwiftUI/GraphicsContext/scaleBy(x:y:))

Scales subsequent drawing operations by an amount in each dimension.

[`rotate(by:)`](/documentation/SwiftUI/GraphicsContext/rotate(by:))

Rotates subsequent drawing operations by an angle.

[`translateBy(x:y:)`](/documentation/SwiftUI/GraphicsContext/translateBy(x:y:))

Moves subsequent drawing operations by an amount in each dimension.

[`concatenate(_:)`](/documentation/SwiftUI/GraphicsContext/concatenate(_:))

Appends the given transform to the context’s existing transform.

[`transform`](/documentation/SwiftUI/GraphicsContext/transform)

The current transform matrix, defining user space coordinates.

### Drawing with a core graphics context

[`withCGContext(content:)`](/documentation/SwiftUI/GraphicsContext/withCGContext(content:))

Provides a Core Graphics context that you can use as a proxy to draw
into this context.

### Accessing the environment

[`environment`](/documentation/SwiftUI/GraphicsContext/environment)

The environment associated with the graphics context.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)