| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkPath.h" |
| 10 |
#include "include/core/SkPathBuilder.h" |
| 11 |
#include "include/core/SkPathEffect.h" |
| 12 |
#include "include/core/SkPathMeasure.h" |
| 13 |
#include "include/core/SkRegion.h" |
| 14 |
#include "include/core/SkShader.h" |
| 15 |
#include "include/effects/Sk1DPathEffect.h" |
| 16 |
#include "include/effects/SkCornerPathEffect.h" |
| 17 |
#include "include/effects/SkGradient.h" |
| 18 |
#include "src/base/SkRandom.h" |
| 19 |
#include "src/base/SkUTF.h" |
| 20 |
#include "src/core/SkColorPriv.h" |
| 21 |
#include "tools/timer/TimeUtils.h" |
| 22 |
#include "tools/viewer/Slide.h" |
| 23 |
|
| 24 |
#define CORNER_RADIUS 12 |
| 25 |
|
| 26 |
static const int gXY[] = { |
| 27 |
4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 28 |
}; |
| 29 |
|
| 30 |
static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) { |
| 31 |
if (flags == 1) { |
| 32 |
return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
| 33 |
} |
| 34 |
|
| 35 |
SkPathBuilder builder; |
| 36 |
builder.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 37 |
for (unsigned i = 2; i < std::size(gXY); i += 2) |
| 38 |
builder.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 39 |
builder.close(); |
| 40 |
builder.offset(-6, 0); |
| 41 |
|
| 42 |
SkPath path = builder.detach(); |
| 43 |
|
| 44 |
auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style); |
| 45 |
|
| 46 |
if (flags == 2) |
| 47 |
return outer; |
| 48 |
|
| 49 |
auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
| 50 |
|
| 51 |
return SkPathEffect::MakeCompose(outer, inner); |
| 52 |
} |
| 53 |
|
| 54 |
static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) { |
| 55 |
SkPathBuilder builder; |
| 56 |
builder.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 57 |
for (unsigned i = 2; i < std::size(gXY); i += 2) { |
| 58 |
builder.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 59 |
} |
| 60 |
builder.close(); |
| 61 |
builder.offset(-6, 0); |
| 62 |
|
| 63 |
SkPath path = builder.detach(); |
| 64 |
|
| 65 |
auto outer = SkPath1DPathEffect::Make( |
| 66 |
path, 12, phase, SkPath1DPathEffect::kMorph_Style); |
| 67 |
auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS)); |
| 68 |
|
| 69 |
return SkPathEffect::MakeCompose(outer, inner); |
| 70 |
} |
| 71 |
|
| 72 |
/////////////////////////////////////////////////////////// |
| 73 |
|
| 74 |
#include "include/core/SkColorFilter.h" |
| 75 |
|
| 76 |
class PathEffectSlide : public Slide { |
| 77 |
SkPath fPath; |
| 78 |
SkPoint fClickPt; |
| 79 |
SkScalar fPhase; |
| 80 |
|
| 81 |
public: |
| 82 |
PathEffectSlide() : fPhase(0) { fName = "PathEffects"; } |
| 83 |
|
| 84 |
void load(SkScalar w, SkScalar h) override { |
| 85 |
SkRandom rand; |
| 86 |
int steps = 20; |
| 87 |
SkScalar dist = SkIntToScalar(400); |
| 88 |
SkScalar x = SkIntToScalar(20); |
| 89 |
SkScalar y = SkIntToScalar(50); |
| 90 |
|
| 91 |
SkPathBuilder builder; |
| 92 |
builder.moveTo(x, y); |
| 93 |
for (int i = 0; i < steps; i++) { |
| 94 |
x += dist/steps; |
| 95 |
SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); |
| 96 |
if (i == steps/2) { |
| 97 |
builder.moveTo(x, tmpY); |
| 98 |
} else { |
| 99 |
builder.lineTo(x, tmpY); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
SkRect oval; |
| 105 |
oval.setLTRB(20, 30, 100, 60); |
| 106 |
oval.offset(x, 0); |
| 107 |
builder.addRRect(SkRRect::MakeRectXY(oval, 8, 8)); |
| 108 |
} |
| 109 |
fPath = builder.detach(); |
| 110 |
|
| 111 |
fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
| 112 |
} |
| 113 |
|
| 114 |
void draw(SkCanvas* canvas) override { |
| 115 |
canvas->clear(0xFFDDDDDD); |
| 116 |
|
| 117 |
SkPaint paint; |
| 118 |
|
| 119 |
canvas->translate(0, 50); |
| 120 |
|
| 121 |
paint.setColor(SK_ColorBLUE); |
| 122 |
paint.setPathEffect(make_pe(2, fPhase)); |
| 123 |
canvas->drawPath(fPath, paint); |
| 124 |
|
| 125 |
canvas->translate(0, 50); |
| 126 |
|
| 127 |
paint.setARGB(0xFF, 0, 0xBB, 0); |
| 128 |
paint.setPathEffect(make_pe(3, fPhase)); |
| 129 |
canvas->drawPath(fPath, paint); |
| 130 |
|
| 131 |
canvas->translate(0, 50); |
| 132 |
|
| 133 |
paint.setARGB(0xFF, 0, 0, 0); |
| 134 |
paint.setPathEffect(make_warp_pe(fPhase)); |
| 135 |
canvas->drawPath(fPath, paint); |
| 136 |
} |
| 137 |
|
| 138 |
bool animate(double nanos) override { |
| 139 |
fPhase = TimeUtils::Scaled(1e-9 * nanos, 40); |
| 140 |
return true; |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
////////////////////////////////////////////////////////////////////////////// |
| 145 |
|
| 146 |
DEF_SLIDE( return new PathEffectSlide(); ) |