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