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 14 matching lines...) Expand all Loading... |
25 path.lineTo(R * cos(theta), R * sin(theta)); | 25 path.lineTo(R * cos(theta), R * sin(theta)); |
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/d7b4ccb6d6281b68a274a72b187fc450"> | 35 <a href='https://fiddle.skia.org/c/@skcanvas_star'><img |
36 <img src="https://fiddle.skia.org/i/d7b4ccb6d6281b68a274a72b187fc450_raster.png"
></a> | 36 src='https://fiddle.skia.org/c/@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/6af99894b40ea1331f6a79d55a4cbfd7"> | 62 <a href='https://fiddle.skia.org/c/@skcanvas_square'><img |
63 <img src="https://fiddle.skia.org/i/6af99894b40ea1331f6a79d55a4cbfd7_raster.png"
></a> | 63 src='https://fiddle.skia.org/c/@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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 canvas->drawRoundRect(rect, 10, 10, paint); | 125 canvas->drawRoundRect(rect, 10, 10, paint); |
126 | 126 |
127 SkPath path; | 127 SkPath path; |
128 path.cubicTo(768, 0, -512, 256, 256, 256); | 128 path.cubicTo(768, 0, -512, 256, 256, 256); |
129 paint.setColor(SK_ColorGREEN); | 129 paint.setColor(SK_ColorGREEN); |
130 canvas->drawPath(path, paint); | 130 canvas->drawPath(path, paint); |
131 | 131 |
132 canvas->drawBitmap(source, 128, 128, &paint); | 132 canvas->drawBitmap(source, 128, 128, &paint); |
133 | 133 |
134 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); | 134 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); |
135 canvas->drawBitmapRect(source, rect2); | 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 <a href="https://fiddle.skia.org/c/35b614d41e60289461d658a9d509e28d"> | 142 |
143 <img src="https://fiddle.skia.org/i/35b614d41e60289461d658a9d509e28d_raster.png"
></a> | 143 <a href='https://fiddle.skia.org/c/@skcanvas_paint'><img |
| 144 src='https://fiddle.skia.org/c/@skcanvas_paint_raster.png'></a> |
144 | 145 |
145 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 |
146 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 |
147 other cases the paint parameter is required. | 148 other cases the paint parameter is required. |
148 | 149 |
149 Next: [SkPaint](/user/api/skpaint) | 150 Next: [SkPaint](/user/api/skpaint) |
OLD | NEW |