Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 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(purplestamp, 256, 256, false, 0) { |
| 5 | // purplestamp |
| 6 | |
| 7 | // Whether to use a separate bitmap (with a color type) at all. |
| 8 | bool drawWithRasterImage = true; |
| 9 | |
| 10 | // This works with either kN32_SkColorType or kAlpha_8_SkColorType. |
| 11 | // Applies only if drawWithRasterImage is true. |
| 12 | SkColorType colorType = kN32_SkColorType; |
| 13 | |
| 14 | void drawStamp(SkCanvas* canvas, int size) { |
| 15 | canvas->save(); |
| 16 | canvas->clipRect(SkRect::MakeWH(size, size), SkClipOp::kIntersect, true); |
| 17 | |
| 18 | canvas->clear(0x3F000000 /* translucent black */); |
| 19 | |
| 20 | SkPaint paint; |
| 21 | paint.setAntiAlias(true); |
| 22 | paint.setColor(SK_ColorBLACK); |
| 23 | canvas->drawRRect( |
| 24 | SkRRect::MakeOval(SkRect::MakeXYWH(size / 4, size / 4, size / 2, size / 2)), paint); |
| 25 | |
| 26 | paint.setStyle(SkPaint::kStroke_Style); |
| 27 | paint.setStrokeWidth(20); |
| 28 | canvas->drawRect(SkRect::MakeWH(size, size), paint); |
| 29 | |
| 30 | canvas->restore(); |
| 31 | } |
| 32 | |
| 33 | sk_sp<SkImage> stampImage(int size) { |
| 34 | sk_sp<SkSurface> surface = SkSurface::MakeRaster( |
| 35 | SkImageInfo::Make(size, size, colorType, kPremul_SkAlphaType)); |
| 36 | drawStamp(surface->getCanvas(), size); |
| 37 | return surface->makeImageSnapshot(); |
| 38 | } |
| 39 | |
| 40 | void draw(SkCanvas* canvas) { |
| 41 | SkPaint paint; |
| 42 | paint.setColorFilter(SkColorFilters::Blend(0xFF7F00FF, SkBlendMode::kSrcIn)); |
| 43 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 44 | paint.setAntiAlias(true); |
| 45 | canvas->rotate(30); |
| 46 | canvas->translate(60, 0); |
| 47 | |
| 48 | int stampSize = 200; |
| 49 | if (drawWithRasterImage) { |
| 50 | canvas->drawImage(stampImage(stampSize), 0, 0, &paint); |
| 51 | } else { |
| 52 | canvas->saveLayer(nullptr, &paint); |
| 53 | drawStamp(canvas, stampSize); |
| 54 | canvas->restore(); |
| 55 | } |
| 56 | } |
| 57 | } // END FIDDLE |