-
-
Notifications
You must be signed in to change notification settings - Fork 132
feat: add Canvas and GraphicsContext reference #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DexZane
wants to merge
1
commit into
AvdLee:main
Choose a base branch
from
DexZane:add-canvas-graphics-reference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # SwiftUI Canvas and GraphicsContext Reference | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Overview](#overview) | ||
| - [Performance Optimization](#performance-optimization) | ||
| - [Using Symbols](#using-symbols) | ||
| - [GraphicsContext State](#graphicscontext-state) | ||
| - [Best Practices](#best-practices) | ||
| - [Summary Checklist](#summary-checklist) | ||
|
|
||
| ## Overview | ||
|
|
||
| `Canvas` provides an immediate mode 2D drawing environment in SwiftUI, passing a `GraphicsContext` to its renderer closure. It is ideal for complex, dynamic graphics that don't require individual element interactivity or accessibility. | ||
|
|
||
| ```swift | ||
| Canvas { context, size in | ||
| context.fill( | ||
| Path(ellipseIn: CGRect(origin: .zero, size: size)), | ||
| with: .color(.blue)) | ||
| } | ||
| .frame(width: 200, height: 200) | ||
| ``` | ||
|
|
||
| ## Performance Optimization | ||
|
|
||
| ### 1. Opaque Canvas | ||
|
|
||
| If your drawing doesn't require transparency, set `opaque` to `true`. This allows the system to optimize rendering by skipping alpha blending. | ||
|
|
||
| ```swift | ||
| // GOOD - optimized for non-transparent backgrounds | ||
| Canvas(opaque: true) { context, size in | ||
| context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(.white)) | ||
| // ... drawing commands | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Asynchronous Rendering | ||
|
|
||
| For complex drawings that might block the main thread, enable `rendersAsynchronously`. This allows the canvas to present its contents in a non-blocking manner. | ||
|
|
||
| ```swift | ||
| // GOOD - prevents main thread stalls for complex graphics | ||
| Canvas(rendersAsynchronously: true) { context, size in | ||
| // Expensive drawing operations | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Path Caching | ||
|
|
||
| Avoid recreating complex paths inside the renderer closure if they don't change. While `Canvas` is immediate mode, pre-calculating paths outside the renderer can save CPU cycles. | ||
|
|
||
| ```swift | ||
| // BAD - recreating path every frame | ||
| Canvas { context, size in | ||
| let path = Path { p in /* complex path construction */ } | ||
| context.fill(path, with: .color(.red)) | ||
| } | ||
|
|
||
| // GOOD - pre-calculating or caching path | ||
| let cachedPath = Path { p in /* complex path construction */ } | ||
| Canvas { context, size in | ||
| context.fill(cachedPath, with: .color(.red)) | ||
| } | ||
| ``` | ||
|
|
||
| ## Using Symbols | ||
|
|
||
| Symbols allow you to reuse SwiftUI views as drawing elements within a `Canvas`. This is more efficient than drawing complex shapes manually when those shapes can be represented as views. | ||
|
|
||
| ```swift | ||
| Canvas { context, size in | ||
| if let mark = context.resolveSymbol(id: "mark") { | ||
| context.draw(mark, at: CGPoint(x: size.width / 2, y: size.height / 2)) | ||
| } | ||
| } symbols: { | ||
| Circle() | ||
| .fill(.red) | ||
| .frame(width: 20, height: 20) | ||
| .tag("mark") | ||
| } | ||
| ``` | ||
|
|
||
| **Note**: Views passed as symbols lose their interactivity and accessibility within the `Canvas`. | ||
|
|
||
| ## GraphicsContext State | ||
|
|
||
| `GraphicsContext` is a value type. To perform independent operations like clipping or transforms without affecting the rest of the drawing, create a copy of the context. | ||
|
|
||
| ```swift | ||
| Canvas { context, size in | ||
| // Create a copy for a specific operation | ||
| var maskedContext = context | ||
| maskedContext.clip(to: Path(CGRect(x: 0, y: 0, width: 50, height: 50))) | ||
| maskedContext.fill(Path(Circle(in: CGRect(origin: .zero, size: size))), with: .color(.green)) | ||
|
|
||
| // Original context remains unaffected | ||
| context.fill(Path(CGRect(x: 60, y: 60, width: 40, height: 40)), with: .color(.blue)) | ||
| } | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| - **Use Canvas for Performance**: Prefer `Canvas` over many individual `Shape` views when drawing hundreds or thousands of elements (e.g., particle systems, complex charts). | ||
| - **Avoid Interactivity**: Do not use `Canvas` if you need users to interact with individual drawn elements. Use standard SwiftUI views instead. | ||
| - **Accessibility**: `Canvas` is treated as a single image by accessibility tools. Provide a high-level `accessibilityLabel` for the entire `Canvas` rather than trying to make internal elements accessible. | ||
| - **Coordinate System**: `Canvas` uses a standard 2D coordinate system where `(0,0)` is the top-left corner. Use the `size` parameter to make your drawing responsive. | ||
|
|
||
| ## Summary Checklist | ||
|
|
||
| - [ ] Use `opaque: true` if no transparency is needed | ||
| - [ ] Enable `rendersAsynchronously: true` for complex, expensive drawings | ||
| - [ ] Use `symbols` to reuse SwiftUI views as drawing elements | ||
| - [ ] Copy `GraphicsContext` for independent state changes (clipping, transforms) | ||
| - [ ] Pre-calculate or cache complex paths outside the renderer when possible | ||
| - [ ] Provide a high-level `accessibilityLabel` for the entire `Canvas` | ||
| - [ ] Use `Canvas` only when individual element interactivity is not required | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
GraphicsContextsample usesPath(Circle(in: ...)), butCirclehas noinit(in:), so this snippet does not compile if copied into a SwiftUI project. Since this reference is used to drive generated guidance, an invalid API call here can cause downstream code suggestions to fail to build; use a valid conversion likePath(ellipseIn:)orCircle().path(in:)instead.Useful? React with 👍 / 👎.