SampleAnimBlur.cpp 1.8 KB

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