OLD | NEW |
(Empty) | |
| 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 <!--?prettify lang=cc?--> |
| 16 |
| 17 void draw(SkCanvas* canvas) { |
| 18 const SkScalar scale = 256.0f; |
| 19 const SkScalar R = 0.45f * scale; |
| 20 const SkScalar TAU = 6.2831853f; |
| 21 SkPath path; |
| 22 for (int i = 0; i < 7; ++i) { |
| 23 SkScalar theta = 3 * i * TAU / 7; |
| 24 if (i == 0) { |
| 25 path.moveTo(R * cos(theta), R * sin(theta)); |
| 26 } else { |
| 27 path.lineTo(R * cos(theta), R * sin(theta)); |
| 28 } |
| 29 } |
| 30 path.close(); |
| 31 SkPaint p; |
| 32 p.setAntiAlias(true); |
| 33 canvas->clear(SK_ColorWHITE); |
| 34 canvas->translate(0.5f * scale, 0.5f * scale); |
| 35 canvas->drawPath(path, p); |
| 36 } |
| 37 |
| 38 Details |
| 39 ------- |
| 40 |
| 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 |
| 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 |
| 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 |
| 47 SkPaint. |
| 48 |
| 49 <!--?prettify lang=cc?--> |
| 50 |
| 51 void draw(SkCanvas* canvas) { |
| 52 canvas->save(); |
| 53 canvas->rotate(SkIntToScalar(45)); |
| 54 SkRect rect = SkRect::MakeXYWH(150, -50, 100, 100); |
| 55 SkPaint paint; |
| 56 canvas->drawRect(rect, paint); |
| 57 canvas->restore(); |
| 58 } |
| 59 |
| 60 The code above will draw a rectangle rotated by 45 degrees. Exactly |
| 61 what color and style the rect will be drawn in is described by the |
| 62 paint, not the canvas. |
| 63 |
| 64 Check out more detailed info on [creating a SkCanvas object](canvas). |
| 65 |
| 66 To begin with, we might want to erase the entire canvas. We can do |
| 67 this by drawing an enormous rectangle, but there are easier ways to do |
| 68 it. |
| 69 |
| 70 <!--?prettify lang=cc?--> |
| 71 |
| 72 void draw(SkCanvas* canvas) { |
| 73 SkPaint paint; |
| 74 paint.setColor(SK_ColorWHITE); |
| 75 canvas->drawPaint(paint); |
| 76 } |
| 77 |
| 78 This fills the entire canvas (though respecting the current clip of |
| 79 course) with whatever color or shader (and xfermode) is specified by |
| 80 the paint. If there is a shader in the paint, then it will respect the |
| 81 current matrix on the canvas as well (see SkShader). If you just want |
| 82 to draw a color (with an optional xfermode), you can just call |
| 83 drawColor(), and save yourself having to allocate a paint. |
| 84 |
| 85 <!--?prettify lang=cc?--> |
| 86 |
| 87 void draw(SkCanvas* canvas) { |
| 88 canvas->drawColor(SK_ColorWHITE); |
| 89 } |
| 90 |
| 91 All of the other draw APIs are similar, each one ending with a paint |
| 92 parameter. |
| 93 |
| 94 <!--?prettify lang=cc?--> |
| 95 |
| 96 void draw(SkCanvas* canvas) { |
| 97 SkPaint paint; |
| 98 paint.setStyle(SkPaint::kStroke_Style); |
| 99 paint.setStrokeWidth(2); |
| 100 |
| 101 SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60); |
| 102 canvas->drawRect(rect, paint); |
| 103 |
| 104 SkRRect oval; |
| 105 oval.setOval(rect); |
| 106 oval.offset(40, 60); |
| 107 canvas->drawRRect(oval, paint); |
| 108 |
| 109 canvas->drawCircle(180, 50, 25, paint); |
| 110 |
| 111 rect.offset(80, 0); |
| 112 canvas->drawRoundRect(rect, 10, 10, paint); |
| 113 |
| 114 SkPath path; |
| 115 path.cubicTo(768, 0, -512, 256, 256, 256); |
| 116 canvas->drawPath(path, paint); |
| 117 |
| 118 canvas->drawBitmap(source, 128, 128, &paint); |
| 119 |
| 120 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); |
| 121 canvas->drawBitmapRect(source, rect2); |
| 122 |
| 123 SkPaint paint2; |
| 124 const char text[] = "Hello, Skia!"; |
| 125 canvas->drawText(text, strlen(text), 50, 25, paint2); |
| 126 } |
| 127 |
| 128 In some of the calls, we pass a pointer, rather than a reference, to |
| 129 the paint. In those instances, the paint parameter may be null. In all |
| 130 other cases the paint parameter is required. |
| 131 |
| 132 Next: [SkPaint](/user/api/skpaint) |
OLD | NEW |