Home | History | Annotate | Download | only in api
      1 SkCanvas
      2 ========
      3 
      4 *The drawing context*
      5 
      6 <!-- Updated Mar 4, 2011 -->
      7 
      8 Preview
      9 -------
     10 
     11 Here is an example of a set of drawing commands to draw a filled
     12 heptagram.  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 
     17 Details
     18 -------
     19 
     20 SkCanvas is the drawing context for Skia. It knows where to direct the
     21 drawing (i.e. where the screen of offscreen pixels are), and maintains
     22 a stack of matrices and clips. Note however, that unlike similar
     23 contexts in other APIs like postscript, cairo, or awt, Skia does not
     24 store any other drawing attributes in the context (e.g. color, pen
     25 size). Rather, these are specified explicitly in each draw call, via a
     26 SkPaint.
     27 
     28 <fiddle-embed name='@skcanvas_square'></fiddle-embed>
     29 
     30 The code above will draw a rectangle rotated by 45 degrees. Exactly
     31 what color and style the rect will be drawn in is described by the
     32 paint, not the canvas.
     33 
     34 Check out more detailed info on [creating a SkCanvas object](canvas).
     35 
     36 To begin with, we might want to erase the entire canvas. We can do
     37 this by drawing an enormous rectangle, but there are easier ways to do
     38 it.
     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 
     48 This fills the entire canvas (though respecting the current clip of
     49 course) with whatever color or shader (and xfermode) is specified by
     50 the paint. If there is a shader in the paint, then it will respect the
     51 current matrix on the canvas as well (see SkShader). If you just want
     52 to draw a color (with an optional xfermode), you can just call
     53 drawColor(), 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 
     61 All of the other draw APIs are similar, each one ending with a paint
     62 parameter.
     63 
     64 <fiddle-embed name='@skcanvas_paint'></fiddle-embed>
     65 
     66 In some of the calls, we pass a pointer, rather than a reference, to
     67 the paint. In those instances, the paint parameter may be null. In all
     68 other cases the paint parameter is required.
     69 
     70 Next: [SkPaint](/user/api/skpaint)
     71