1 /*
2 * Copyright 2012 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/SkColorPriv.h"
10 #include "include/core/SkMaskFilter.h"
11 #include "include/utils/SkRandom.h"
12 #include "samplecode/Sample.h"
13
get_anim_sin(double secs,SkScalar amplitude,SkScalar periodInSec,SkScalar phaseInSec)14 SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
15 if (!periodInSec) {
16 return 0;
17 }
18 double t = secs + phaseInSec;
19 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
20 amplitude = SK_ScalarHalf * amplitude;
21 return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
22 }
23
24 class AnimBlurView : public Sample {
25 SkScalar fBlurSigma = 0;
26 SkScalar fCircleRadius = 100;
27
name()28 SkString name() override { return SkString("AnimBlur"); }
29
onDrawContent(SkCanvas * canvas)30 void onDrawContent(SkCanvas* canvas) override {
31 static const SkBlurStyle gStyles[] = {
32 kNormal_SkBlurStyle,
33 kInner_SkBlurStyle,
34 kSolid_SkBlurStyle,
35 kOuter_SkBlurStyle,
36 };
37 SkRandom random;
38
39 for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) {
40 SkPaint paint;
41 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyles[i],
42 fBlurSigma));
43 paint.setColor(random.nextU() | 0xff000000);
44 canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1,
45 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1,
46 fCircleRadius, paint);
47 }
48 }
49
onAnimate(double nanos)50 bool onAnimate(double nanos) override {
51 fBlurSigma = get_anim_sin(1e-9 * nanos, 100, 4, 5);
52 fCircleRadius = 3 + get_anim_sin(1e-9 * nanos, 150, 25, 3);
53 return true;
54 }
55 };
56 DEF_SAMPLE( return new AnimBlurView(); )
57