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