• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Sample.h"
9 #include "SkAnimTimer.h"
10 #include "SkColorPriv.h"
11 #include "SkCanvas.h"
12 #include "SkMaskFilter.h"
13 #include "SkRandom.h"
14 
get_anim_sin(double secs,SkScalar amplitude,SkScalar periodInSec,SkScalar phaseInSec)15 SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
16     if (!periodInSec) {
17         return 0;
18     }
19     double t = secs + phaseInSec;
20     t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
21     amplitude = SK_ScalarHalf * amplitude;
22     return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
23 }
24 
25 class AnimBlurView : public Sample {
26 public:
AnimBlurView()27     AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {}
28 
29 protected:
onQuery(Sample::Event * evt)30     bool onQuery(Sample::Event* evt) override {
31         if (Sample::TitleQ(*evt)) {
32             Sample::TitleR(evt, "AnimBlur");
33             return true;
34         }
35         return this->INHERITED::onQuery(evt);
36     }
37 
onDrawContent(SkCanvas * canvas)38     void onDrawContent(SkCanvas* canvas) override {
39         static const SkBlurStyle gStyles[] = {
40             kNormal_SkBlurStyle,
41             kInner_SkBlurStyle,
42             kSolid_SkBlurStyle,
43             kOuter_SkBlurStyle,
44         };
45         SkRandom random;
46 
47         for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) {
48             SkPaint paint;
49             paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyles[i],
50                                                        fBlurSigma));
51             paint.setColor(random.nextU() | 0xff000000);
52             canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1,
53                                200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1,
54                                fCircleRadius, paint);
55         }
56     }
57 
onAnimate(const SkAnimTimer & timer)58     bool onAnimate(const SkAnimTimer& timer) override {
59         fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5);
60         fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3);
61         return true;
62     }
63 
64 private:
65     SkScalar fBlurSigma, fCircleRadius;
66 
67     typedef Sample INHERITED;
68 };
69 
70 //////////////////////////////////////////////////////////////////////////////
71 
72 DEF_SAMPLE( return new AnimBlurView(); )
73