2015-05-19 17:21:29 +00:00
|
|
|
SkCanvas
|
|
|
|
========
|
|
|
|
|
|
|
|
*The drawing context*
|
|
|
|
|
|
|
|
<!-- Updated Mar 4, 2011 -->
|
|
|
|
|
|
|
|
Preview
|
|
|
|
-------
|
|
|
|
|
|
|
|
Here is an example of a set of drawing commands to draw a filled
|
|
|
|
heptagram. This function can be cut and pasted into
|
|
|
|
[fiddle.skia.org](https://fiddle.skia.org/).
|
|
|
|
|
|
|
|
<!--?prettify lang=cc?-->
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas) {
|
|
|
|
const SkScalar scale = 256.0f;
|
|
|
|
const SkScalar R = 0.45f * scale;
|
|
|
|
const SkScalar TAU = 6.2831853f;
|
|
|
|
SkPath path;
|
2015-07-08 18:34:36 +00:00
|
|
|
path.moveTo(R, 0.0f);
|
|
|
|
for (int i = 1; i < 7; ++i) {
|
2015-05-19 17:21:29 +00:00
|
|
|
SkScalar theta = 3 * i * TAU / 7;
|
2015-07-08 18:34:36 +00:00
|
|
|
path.lineTo(R * cos(theta), R * sin(theta));
|
2015-05-19 17:21:29 +00:00
|
|
|
}
|
|
|
|
path.close();
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
canvas->translate(0.5f * scale, 0.5f * scale);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:25:43 +00:00
|
|
|
<a href='https://fiddle.skia.org/c/@skcanvas_star'><img
|
2016-04-22 18:43:42 +00:00
|
|
|
src='https://fiddle.skia.org/i/@skcanvas_star_raster.png'></a>
|
2015-07-08 18:34:36 +00:00
|
|
|
|
2015-05-19 17:21:29 +00:00
|
|
|
Details
|
|
|
|
-------
|
|
|
|
|
|
|
|
SkCanvas is the drawing context for Skia. It knows where to direct the
|
|
|
|
drawing (i.e. where the screen of offscreen pixels are), and maintains
|
|
|
|
a stack of matrices and clips. Note however, that unlike similar
|
|
|
|
contexts in other APIs like postscript, cairo, or awt, Skia does not
|
|
|
|
store any other drawing attributes in the context (e.g. color, pen
|
|
|
|
size). Rather, these are specified explicitly in each draw call, via a
|
|
|
|
SkPaint.
|
|
|
|
|
|
|
|
<!--?prettify lang=cc?-->
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas) {
|
|
|
|
canvas->save();
|
2015-07-09 13:58:06 +00:00
|
|
|
canvas->translate(SkIntToScalar(128), SkIntToScalar(128));
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->rotate(SkIntToScalar(45));
|
2015-07-09 13:58:06 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f);
|
2015-05-19 17:21:29 +00:00
|
|
|
SkPaint paint;
|
2015-07-09 13:58:06 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:25:43 +00:00
|
|
|
<a href='https://fiddle.skia.org/c/@skcanvas_square'><img
|
2016-04-22 18:43:42 +00:00
|
|
|
src='https://fiddle.skia.org/i/@skcanvas_square_raster.png'></a>
|
2015-07-08 18:34:36 +00:00
|
|
|
|
2015-05-19 17:21:29 +00:00
|
|
|
The code above will draw a rectangle rotated by 45 degrees. Exactly
|
|
|
|
what color and style the rect will be drawn in is described by the
|
|
|
|
paint, not the canvas.
|
|
|
|
|
|
|
|
Check out more detailed info on [creating a SkCanvas object](canvas).
|
|
|
|
|
|
|
|
To begin with, we might want to erase the entire canvas. We can do
|
|
|
|
this by drawing an enormous rectangle, but there are easier ways to do
|
|
|
|
it.
|
|
|
|
|
|
|
|
<!--?prettify lang=cc?-->
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
This fills the entire canvas (though respecting the current clip of
|
|
|
|
course) with whatever color or shader (and xfermode) is specified by
|
|
|
|
the paint. If there is a shader in the paint, then it will respect the
|
|
|
|
current matrix on the canvas as well (see SkShader). If you just want
|
|
|
|
to draw a color (with an optional xfermode), you can just call
|
|
|
|
drawColor(), and save yourself having to allocate a paint.
|
|
|
|
|
|
|
|
<!--?prettify lang=cc?-->
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
All of the other draw APIs are similar, each one ending with a paint
|
|
|
|
parameter.
|
|
|
|
|
|
|
|
<!--?prettify lang=cc?-->
|
|
|
|
|
2015-07-08 18:34:36 +00:00
|
|
|
SkBitmap source;
|
|
|
|
|
2015-05-19 17:21:29 +00:00
|
|
|
void draw(SkCanvas* canvas) {
|
2015-07-08 18:34:36 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
|
2015-05-19 17:21:29 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2015-07-08 18:34:36 +00:00
|
|
|
paint.setStrokeWidth(4);
|
|
|
|
paint.setColor(SK_ColorRED);
|
2015-05-19 17:21:29 +00:00
|
|
|
|
|
|
|
SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
|
|
|
|
SkRRect oval;
|
|
|
|
oval.setOval(rect);
|
|
|
|
oval.offset(40, 60);
|
2015-07-08 18:34:36 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->drawRRect(oval, paint);
|
|
|
|
|
2015-07-08 18:34:36 +00:00
|
|
|
paint.setColor(SK_ColorCYAN);
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->drawCircle(180, 50, 25, paint);
|
|
|
|
|
|
|
|
rect.offset(80, 0);
|
2015-07-08 18:34:36 +00:00
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->drawRoundRect(rect, 10, 10, paint);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.cubicTo(768, 0, -512, 256, 256, 256);
|
2015-07-08 18:34:36 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
2015-05-19 17:21:29 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
2016-10-28 16:18:32 +00:00
|
|
|
canvas->drawImage(image, 128, 128, &paint);
|
2015-05-19 17:21:29 +00:00
|
|
|
|
|
|
|
SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60);
|
2016-10-28 16:18:32 +00:00
|
|
|
canvas->drawImageRect(image, rect2, &paint);
|
2015-05-19 17:21:29 +00:00
|
|
|
|
|
|
|
SkPaint paint2;
|
|
|
|
const char text[] = "Hello, Skia!";
|
|
|
|
canvas->drawText(text, strlen(text), 50, 25, paint2);
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:25:43 +00:00
|
|
|
|
|
|
|
<a href='https://fiddle.skia.org/c/@skcanvas_paint'><img
|
2016-04-22 18:43:42 +00:00
|
|
|
src='https://fiddle.skia.org/i/@skcanvas_paint_raster.png'></a>
|
2015-07-08 18:34:36 +00:00
|
|
|
|
2015-05-19 17:21:29 +00:00
|
|
|
In some of the calls, we pass a pointer, rather than a reference, to
|
|
|
|
the paint. In those instances, the paint parameter may be null. In all
|
|
|
|
other cases the paint parameter is required.
|
|
|
|
|
|
|
|
Next: [SkPaint](/user/api/skpaint)
|