| 1 |
// Copyright 2020 Google LLC. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 |
#include "tools/fiddle/examples.h" |
| 4 |
REG_FIDDLE(Canvas_saveLayer_4, 256, 256, false, 3) { |
| 5 |
void draw(SkCanvas* canvas) { |
| 6 |
SkPaint pRed; |
| 7 |
pRed.setColor(SK_ColorRED); |
| 8 |
|
| 9 |
SkPaint pSolidBlue; |
| 10 |
pSolidBlue.setColor(SK_ColorBLUE); |
| 11 |
|
| 12 |
SkPaint pThirtyBlue; |
| 13 |
pThirtyBlue.setColor(SK_ColorBLUE); |
| 14 |
pThirtyBlue.setAlphaf(0.3); |
| 15 |
|
| 16 |
SkPaint alpha; |
| 17 |
alpha.setAlphaf(0.3); |
| 18 |
|
| 19 |
// First row: Draw two opaque red rectangles into the 0th layer. Then draw two blue |
| 20 |
// rectangles overlapping the red, one is solid, the other is 30% transparent. |
| 21 |
canvas->drawRect(SkRect::MakeLTRB(10, 10, 60, 60), pRed); |
| 22 |
canvas->drawRect(SkRect::MakeLTRB(150, 10, 200, 60), pRed); |
| 23 |
|
| 24 |
canvas->drawRect(SkRect::MakeLTRB(30, 10, 80, 60), pSolidBlue); |
| 25 |
canvas->drawRect(SkRect::MakeLTRB(170, 10, 220, 60), pThirtyBlue); |
| 26 |
|
| 27 |
// Second row: Draw two opaque red rectangles into the 0th layer. Then save a new layer; |
| 28 |
// when the 1st layer gets merged onto the 0th layer (i.e. when restore() is called), it will |
| 29 |
// use the provided paint to do so. In this case, the paint is set to have 30% opacity, but |
| 30 |
// it could also have things set like blend modes or image filters. |
| 31 |
canvas->drawRect(SkRect::MakeLTRB(10, 70, 60, 120), pRed); |
| 32 |
canvas->drawRect(SkRect::MakeLTRB(150, 70, 200, 120), pRed); |
| 33 |
|
| 34 |
canvas->saveLayer(nullptr, &alpha); |
| 35 |
|
| 36 |
// In the 1st layer, draw the same blue overlapping rectangles as in the first row. Notice in |
| 37 |
// the final output, we have two different shades of purple. The layer's alpha made the |
| 38 |
// opaque blue rectangle transparent, and it made the transparent blue rectangle even more so |
| 39 |
canvas->drawRect(SkRect::MakeLTRB(30, 70, 80, 120), pSolidBlue); |
| 40 |
canvas->drawRect(SkRect::MakeLTRB(170, 70, 220, 120), pThirtyBlue); |
| 41 |
|
| 42 |
canvas->restore(); |
| 43 |
|
| 44 |
// Third row: save the layer first, before drawing the two red rectangle, followed by the |
| 45 |
// overlapping blue rectangles. Notice that the blue overwrites the red in the same way as |
| 46 |
// the first row because the alpha of the layer is not applied until the layer is restored. |
| 47 |
canvas->saveLayer(nullptr, &alpha); |
| 48 |
|
| 49 |
canvas->drawRect(SkRect::MakeLTRB(10, 130, 60, 180), pRed); |
| 50 |
canvas->drawRect(SkRect::MakeLTRB(150, 130, 200, 180), pRed); |
| 51 |
|
| 52 |
canvas->drawRect(SkRect::MakeLTRB(30, 130, 80, 180), pSolidBlue); |
| 53 |
canvas->drawRect(SkRect::MakeLTRB(170, 130, 220, 180), pThirtyBlue); |
| 54 |
|
| 55 |
canvas->restore(); |
| 56 |
} |
| 57 |
} // END FIDDLE |