Change-Id: I1b89db473582f9fcc4d87436a590ee36e869fe45 No-Try: true Docs-Preview: https://skia.org/docs/user/build?cl=392717 Docs-Preview: https://skia.org/docs/dev/contrib/?cl=392717 Docs-Preview: https://skia.org/docs/dev/gardening/?cl=392717 Docs-Preview: https://skia.org/docs/user/tips?cl=392717 Docs-Preview: https://skia.org/docs/dev/tools/debugger?cl=392717 Docs-Preview: https://skia.org/docs/user/modules/pathkit?cl=392717 Docs-Preview: https://skia.org/docs/dev/testing/fuzz?cl=392717 Docs-Preview: https://skia.org/docs/dev/testing/ios?cl=392717 Docs-Preview: https://skia.org/docs/roles?cl=392717 Docs-Preview: https://skia.org/docs/user/api/?cl=392717 Docs-Preview: https://skia.org/docs/dev/testing/swarmingbots?cl=392717 Docs-Preview: https://skia.org/docs/dev/chrome/changes?cl=392717 Docs-Preview: https://skia.org/docs/user/download?cl=392717 Docs-Preview: https://skia.org/docs/dev/flutter/?cl=392717 Docs-Preview: https://skia.org/docs/user/modules/quickstart?cl=392717 Docs-Preview: https://skia.org/docs/user/api/skcanvas_creation?cl=392717 Docs-Preview: https://skia.org/docs/user/sample/viewer?cl=392717 Docs-Preview: https://skia.org/docs/dev/design/pdftheory?cl=392717 Docs-Preview: https://skia.org/docs/user/api/SkBlendMode_Overview?cl=392717 Docs-Preview: https://skia.org/docs/user/api/skcanvas_overview?cl=392717 Docs-Preview: https://skia.org/docs/?cl=392717 Docs-Preview: https://skia.org/docs/dev/contrib/submit?cl=392717 Docs-Preview: https://skia.org/docs/user/api/SkPath_Overview?cl=392717 Docs-Preview: https://skia.org/docs/user/modules/skottie?cl=392717 Docs-Preview: https://skia.org/docs/user/modules/canvaskit?cl=392717 Fixed: skia:404 Docs-Preview: https://skia.org/docs/dev/tools/calendar?cl=392717 Docs-Preview: https://skia.org/docs/dev/gardening/android?cl=392717 Docs-Preview: https://skia.org/docs/dev/contrib/style?cl=392717 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/392717 Reviewed-by: Heather Miller <hcm@google.com> Commit-Queue: Joe Gregorio <jcgregorio@google.com>
2.2 KiB
title: "SkCanvas Overview" linkTitle: "SkCanvas Overview"
weight: 240
The drawing context
Preview
Here is an example of a set of drawing commands to draw a filled heptagram. This function can be cut and pasted into fiddle.skia.org.
Details
SkCanvas is the drawing context for Skia. It knows where to direct the drawing (i.e. where the screen of offscreen pixels are), and maintains a stack of matrices and clips. Note however, that unlike similar contexts in other APIs like postscript, cairo, or awt, Skia does not store any other drawing attributes in the context (e.g. color, pen size). Rather, these are specified explicitly in each draw call, via a SkPaint.
The code above will draw a rectangle rotated by 45 degrees. Exactly what color and style the rect will be drawn in is described by the paint, not the canvas.
Check out more detailed info on creating a SkCanvas object.
To begin with, we might want to erase the entire canvas. We can do this by drawing an enormous rectangle, but there are easier ways to do it.
void draw(SkCanvas* canvas) {
SkPaint paint;
paint.setColor(SK_ColorWHITE);
canvas->drawPaint(paint);
}
This fills the entire canvas (though respecting the current clip of course) with whatever color or shader (and xfermode) is specified by the paint. If there is a shader in the paint, then it will respect the current matrix on the canvas as well (see SkShader). If you just want to draw a color (with an optional xfermode), you can just call drawColor(), and save yourself having to allocate a paint.
void draw(SkCanvas* canvas) {
canvas->drawColor(SK_ColorWHITE);
}
All of the other draw APIs are similar, each one ending with a paint parameter.
In some of the calls, we pass a pointer, rather than a reference, to the paint. In those instances, the paint parameter may be null. In all other cases the paint parameter is required.
Next: SkPaint