• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1SkCanvas Overview
2========
3
4*The drawing context*
5
6<!-- Updated Mar 4, 2011 -->
7
8Preview
9-------
10
11Here is an example of a set of drawing commands to draw a filled
12heptagram.  This function can be cut and pasted into
13[fiddle.skia.org](https://fiddle.skia.org/).
14
15<fiddle-embed name='@skcanvas_star'></fiddle-embed>
16
17Details
18-------
19
20SkCanvas is the drawing context for Skia. It knows where to direct the
21drawing (i.e. where the screen of offscreen pixels are), and maintains
22a stack of matrices and clips. Note however, that unlike similar
23contexts in other APIs like postscript, cairo, or awt, Skia does not
24store any other drawing attributes in the context (e.g. color, pen
25size). Rather, these are specified explicitly in each draw call, via a
26SkPaint.
27
28<fiddle-embed name='@skcanvas_square'></fiddle-embed>
29
30The code above will draw a rectangle rotated by 45 degrees. Exactly
31what color and style the rect will be drawn in is described by the
32paint, not the canvas.
33
34Check out more detailed info on [creating a SkCanvas object](canvas).
35
36To begin with, we might want to erase the entire canvas. We can do
37this by drawing an enormous rectangle, but there are easier ways to do
38it.
39
40<!--?prettify lang=cc?-->
41
42    void draw(SkCanvas* canvas) {
43        SkPaint paint;
44        paint.setColor(SK_ColorWHITE);
45        canvas->drawPaint(paint);
46    }
47
48This fills the entire canvas (though respecting the current clip of
49course) with whatever color or shader (and xfermode) is specified by
50the paint. If there is a shader in the paint, then it will respect the
51current matrix on the canvas as well (see SkShader). If you just want
52to draw a color (with an optional xfermode), you can just call
53drawColor(), and save yourself having to allocate a paint.
54
55<!--?prettify lang=cc?-->
56
57    void draw(SkCanvas* canvas) {
58        canvas->drawColor(SK_ColorWHITE);
59    }
60
61All of the other draw APIs are similar, each one ending with a paint
62parameter.
63
64<fiddle-embed name='@skcanvas_paint'></fiddle-embed>
65
66In some of the calls, we pass a pointer, rather than a reference, to
67the paint. In those instances, the paint parameter may be null. In all
68other cases the paint parameter is required.
69
70Next: [SkPaint](/user/api/skpaint_overview)
71