OLD | NEW |
1 SkCanvas | 1 SkCanvas |
2 ======== | 2 ======== |
3 | 3 |
4 *The drawing context* | 4 *The drawing context* |
5 | 5 |
6 <!-- Updated Mar 4, 2011 --> | 6 <!-- Updated Mar 4, 2011 --> |
7 | 7 |
8 Preview | 8 Preview |
9 ------- | 9 ------- |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 path.close(); | 27 path.close(); |
28 SkPaint p; | 28 SkPaint p; |
29 p.setAntiAlias(true); | 29 p.setAntiAlias(true); |
30 canvas->clear(SK_ColorWHITE); | 30 canvas->clear(SK_ColorWHITE); |
31 canvas->translate(0.5f * scale, 0.5f * scale); | 31 canvas->translate(0.5f * scale, 0.5f * scale); |
32 canvas->drawPath(path, p); | 32 canvas->drawPath(path, p); |
33 } | 33 } |
34 | 34 |
35 <a href='https://fiddle.skia.org/c/@skcanvas_star'><img | 35 <a href='https://fiddle.skia.org/c/@skcanvas_star'><img |
36 src='https://fiddle.skia.org/c/@skcanvas_star_raster.png'></a> | 36 src='https://fiddle.skia.org/i/@skcanvas_star_raster.png'></a> |
37 | 37 |
38 Details | 38 Details |
39 ------- | 39 ------- |
40 | 40 |
41 SkCanvas is the drawing context for Skia. It knows where to direct the | 41 SkCanvas is the drawing context for Skia. It knows where to direct the |
42 drawing (i.e. where the screen of offscreen pixels are), and maintains | 42 drawing (i.e. where the screen of offscreen pixels are), and maintains |
43 a stack of matrices and clips. Note however, that unlike similar | 43 a stack of matrices and clips. Note however, that unlike similar |
44 contexts in other APIs like postscript, cairo, or awt, Skia does not | 44 contexts in other APIs like postscript, cairo, or awt, Skia does not |
45 store any other drawing attributes in the context (e.g. color, pen | 45 store any other drawing attributes in the context (e.g. color, pen |
46 size). Rather, these are specified explicitly in each draw call, via a | 46 size). Rather, these are specified explicitly in each draw call, via a |
47 SkPaint. | 47 SkPaint. |
48 | 48 |
49 <!--?prettify lang=cc?--> | 49 <!--?prettify lang=cc?--> |
50 | 50 |
51 void draw(SkCanvas* canvas) { | 51 void draw(SkCanvas* canvas) { |
52 canvas->save(); | 52 canvas->save(); |
53 canvas->translate(SkIntToScalar(128), SkIntToScalar(128)); | 53 canvas->translate(SkIntToScalar(128), SkIntToScalar(128)); |
54 canvas->rotate(SkIntToScalar(45)); | 54 canvas->rotate(SkIntToScalar(45)); |
55 SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f); | 55 SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f); |
56 SkPaint paint; | 56 SkPaint paint; |
57 paint.setColor(SK_ColorBLUE); | 57 paint.setColor(SK_ColorBLUE); |
58 canvas->drawRect(rect, paint); | 58 canvas->drawRect(rect, paint); |
59 canvas->restore(); | 59 canvas->restore(); |
60 } | 60 } |
61 | 61 |
62 <a href='https://fiddle.skia.org/c/@skcanvas_square'><img | 62 <a href='https://fiddle.skia.org/c/@skcanvas_square'><img |
63 src='https://fiddle.skia.org/c/@skcanvas_square_raster.png'></a> | 63 src='https://fiddle.skia.org/i/@skcanvas_square_raster.png'></a> |
64 | 64 |
65 The code above will draw a rectangle rotated by 45 degrees. Exactly | 65 The code above will draw a rectangle rotated by 45 degrees. Exactly |
66 what color and style the rect will be drawn in is described by the | 66 what color and style the rect will be drawn in is described by the |
67 paint, not the canvas. | 67 paint, not the canvas. |
68 | 68 |
69 Check out more detailed info on [creating a SkCanvas object](canvas). | 69 Check out more detailed info on [creating a SkCanvas object](canvas). |
70 | 70 |
71 To begin with, we might want to erase the entire canvas. We can do | 71 To begin with, we might want to erase the entire canvas. We can do |
72 this by drawing an enormous rectangle, but there are easier ways to do | 72 this by drawing an enormous rectangle, but there are easier ways to do |
73 it. | 73 it. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); | 134 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); |
135 canvas->drawBitmapRect(source, rect2, &paint); | 135 canvas->drawBitmapRect(source, rect2, &paint); |
136 | 136 |
137 SkPaint paint2; | 137 SkPaint paint2; |
138 const char text[] = "Hello, Skia!"; | 138 const char text[] = "Hello, Skia!"; |
139 canvas->drawText(text, strlen(text), 50, 25, paint2); | 139 canvas->drawText(text, strlen(text), 50, 25, paint2); |
140 } | 140 } |
141 | 141 |
142 | 142 |
143 <a href='https://fiddle.skia.org/c/@skcanvas_paint'><img | 143 <a href='https://fiddle.skia.org/c/@skcanvas_paint'><img |
144 src='https://fiddle.skia.org/c/@skcanvas_paint_raster.png'></a> | 144 src='https://fiddle.skia.org/i/@skcanvas_paint_raster.png'></a> |
145 | 145 |
146 In some of the calls, we pass a pointer, rather than a reference, to | 146 In some of the calls, we pass a pointer, rather than a reference, to |
147 the paint. In those instances, the paint parameter may be null. In all | 147 the paint. In those instances, the paint parameter may be null. In all |
148 other cases the paint parameter is required. | 148 other cases the paint parameter is required. |
149 | 149 |
150 Next: [SkPaint](/user/api/skpaint) | 150 Next: [SkPaint](/user/api/skpaint) |
OLD | NEW |