blob: 5330fdfa71788585a3257ecc86d30c6f3d593078 [file] [log] [blame]
bsalomon@google.com85003222012-03-28 14:44:37 +00001/*
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 */
reed76113a92015-02-02 12:55:02 -08007
bsalomon@google.com85003222012-03-28 14:44:37 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
bsalomon@google.com85003222012-03-28 14:44:37 +000010#include "SkBlurMaskFilter.h"
11#include "SkColorPriv.h"
12#include "SkCanvas.h"
13#include "SkRandom.h"
14
reed76113a92015-02-02 12:55:02 -080015SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
reedd9adfe62015-02-01 19:01:04 -080016 if (!periodInSec) {
17 return 0;
18 }
reed76113a92015-02-02 12:55:02 -080019 double t = secs + phaseInSec;
reedd9adfe62015-02-01 19:01:04 -080020 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
21 amplitude = SK_ScalarHalf * amplitude;
22 return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
23}
24
bsalomon@google.com85003222012-03-28 14:44:37 +000025class AnimBlurView : public SampleView {
26public:
reedd9adfe62015-02-01 19:01:04 -080027 AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {}
bsalomon@google.com85003222012-03-28 14:44:37 +000028
29protected:
30 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070031 bool onQuery(SkEvent* evt) override {
bsalomon@google.com85003222012-03-28 14:44:37 +000032 if (SampleCode::TitleQ(*evt)) {
33 SampleCode::TitleR(evt, "AnimBlur");
34 return true;
35 }
36 return this->INHERITED::onQuery(evt);
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000040 static const SkBlurStyle gStyles[] = {
41 kNormal_SkBlurStyle,
42 kInner_SkBlurStyle,
43 kSolid_SkBlurStyle,
44 kOuter_SkBlurStyle,
bsalomon@google.com85003222012-03-28 14:44:37 +000045 };
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000046 SkRandom random;
bsalomon@google.com85003222012-03-28 14:44:37 +000047
robertphillips@google.com7460b372012-04-25 16:54:51 +000048 for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) {
bsalomon@google.com85003222012-03-28 14:44:37 +000049 SkPaint paint;
reedefdfd512016-04-04 10:02:58 -070050 paint.setMaskFilter(SkBlurMaskFilter::Make(gStyles[i],
51 fBlurSigma,
52 SkBlurMaskFilter::kHighQuality_BlurFlag));
bsalomon@google.com85003222012-03-28 14:44:37 +000053 paint.setColor(random.nextU() | 0xff000000);
54 canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1,
55 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1,
reedd9adfe62015-02-01 19:01:04 -080056 fCircleRadius, paint);
bsalomon@google.com85003222012-03-28 14:44:37 +000057 }
reedd9adfe62015-02-01 19:01:04 -080058 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080061 fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5);
62 fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3);
reedd9adfe62015-02-01 19:01:04 -080063 return true;
bsalomon@google.com85003222012-03-28 14:44:37 +000064 }
65
66private:
reedd9adfe62015-02-01 19:01:04 -080067 SkScalar fBlurSigma, fCircleRadius;
68
69 typedef SampleView INHERITED;
bsalomon@google.com85003222012-03-28 14:44:37 +000070};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static SkView* MyFactory() { return new AnimBlurView; }
75static SkViewRegister reg(MyFactory);