Declaration
import { Canvas } from 'swiftui-react-native';nonisolated struct Canvas<Symbols> where Symbols : ViewInitializers
2 SwiftUI initializers back <Canvas>. The props you pass pick one.
<Canvas
opaque?: boolean
colorMode?: SwiftUI.ColorRenderingMode
rendersAsynchronously?: boolean
>
children: (context: SwiftUI.GraphicsContext, size: SymbolicSize) => void
</Canvas>nonisolated init(
opaque: Bool = false,
colorMode: ColorRenderingMode = .nonLinear,
rendersAsynchronously: Bool = false,
renderer: @escaping (inout GraphicsContext, CGSize) -> Void
)opaquebooleanA Boolean that indicates whether the canvas is fully opaque. You might be able to improve performance by setting this value to
true, but then drawing a non-opaque image into the context produces undefined results. The default isfalse.Default
`false`colorModeSwiftUI.ColorRenderingModeA working color space and storage format of the canvas. The default is
ColorRenderingMode/nonLinear.Default
`.nonLinear`rendersAsynchronouslybooleanA Boolean that indicates whether the canvas can present its contents to its parent view asynchronously. The default is
false.Default
`false`children*(context: SwiftUI.GraphicsContext, size: SymbolicSize) => voidA closure in which you conduct immediate mode drawing. The closure takes two inputs: a context that you use to issue drawing commands and a size --- representing the current size of the canvas --- that you can use to customize the content. The canvas calls the renderer any time it needs to redraw the content. Runs once, ahead of SwiftUI: it records what the closure does, and numbers SwiftUI decides are symbolic.
Swift
renderer: (inout GraphicsContext, CGSize) -> Void
Also takes the common view props: modifiers, style and testID.
Overview
Use a canvas to draw rich and dynamic 2D graphics inside a SwiftUI view.
The canvas passes a GraphicsContext to the closure that you use
to perform immediate mode drawing operations. The canvas also passes a
doc://com.apple.documentation/documentation/CoreFoundation/CGSize value
that you can use to customize what you draw. For example, you can use the
context's GraphicsContext.stroke(_:with:lineWidth:) command to draw
a Path instance:
Canvas { context, size in
context.stroke(
Path(ellipseIn: CGRect(origin: .zero, size: size)),
with: .color(.green),
lineWidth: 4)
}
.frame(width: 300, height: 200)
.border(Color.blue)The example above draws the outline of an ellipse that exactly inscribes a canvas with a blue border:
In addition to outlined and filled paths, you can draw images, text, and
complete SwiftUI views. To draw views, use the
init(opaque:colorMode:rendersAsynchronously:renderer:symbols:) method
to supply views that you can reference from inside the renderer. You can
also add masks, apply filters, perform transforms, control blending, and
more. For information about how to draw, see GraphicsContext.
A canvas doesn't offer interactivity or accessibility for individual elements, including for views that you pass in as symbols. However, it might provide better performance for a complex drawing that involves dynamic data. Use a canvas to improve performance for a drawing that doesn't primarily involve text or require interactive elements.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- Swift.Copyable
- Swift.Escapable
- View
See also
More in Drawing and shapes.