bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 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 | */ |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 7 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkRandom.h" |
| 14 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 15 | SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) { |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 16 | if (!periodInSec) { |
| 17 | return 0; |
| 18 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 19 | double t = secs + phaseInSec; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 20 | t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec; |
| 21 | amplitude = SK_ScalarHalf * amplitude; |
| 22 | return amplitude * SkDoubleToScalar(sin(t)) + amplitude; |
| 23 | } |
| 24 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 25 | class AnimBlurView : public SampleView { |
| 26 | public: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 27 | AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {} |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 28 | |
| 29 | protected: |
| 30 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 32 | if (SampleCode::TitleQ(*evt)) { |
| 33 | SampleCode::TitleR(evt, "AnimBlur"); |
| 34 | return true; |
| 35 | } |
| 36 | return this->INHERITED::onQuery(evt); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 40 | static const SkBlurStyle gStyles[] = { |
| 41 | kNormal_SkBlurStyle, |
| 42 | kInner_SkBlurStyle, |
| 43 | kSolid_SkBlurStyle, |
| 44 | kOuter_SkBlurStyle, |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 45 | }; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 46 | SkRandom random; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 47 | |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 48 | for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) { |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 49 | SkPaint paint; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 50 | paint.setMaskFilter(SkBlurMaskFilter::Make(gStyles[i], |
| 51 | fBlurSigma, |
| 52 | SkBlurMaskFilter::kHighQuality_BlurFlag)); |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 53 | 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, |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 56 | fCircleRadius, paint); |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 57 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 58 | } |
| 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 61 | fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5); |
| 62 | fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 63 | return true; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 67 | SkScalar fBlurSigma, fCircleRadius; |
| 68 | |
| 69 | typedef SampleView INHERITED; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static SkView* MyFactory() { return new AnimBlurView; } |
| 75 | static SkViewRegister reg(MyFactory); |