stringart.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2013 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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkPath.h"
  11. #include "include/core/SkPoint.h"
  12. #include "include/core/SkScalar.h"
  13. #include "include/core/SkSize.h"
  14. #include "include/core/SkString.h"
  15. #include "include/core/SkTypes.h"
  16. #include "tools/ToolUtils.h"
  17. #include "tools/timer/TimeUtils.h"
  18. // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
  19. constexpr int kWidth = 440;
  20. constexpr int kHeight = 440;
  21. constexpr SkScalar kAngle = 0.305f;
  22. constexpr int kMaxNumSteps = 140;
  23. // Renders a string art shape.
  24. // The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
  25. class StringArtGM : public skiagm::GM {
  26. public:
  27. StringArtGM() : fNumSteps(kMaxNumSteps) {}
  28. protected:
  29. SkString onShortName() override {
  30. return SkString("stringart");
  31. }
  32. SkISize onISize() override {
  33. return SkISize::Make(kWidth, kHeight);
  34. }
  35. void onDraw(SkCanvas* canvas) override {
  36. SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
  37. SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight));
  38. SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
  39. SkScalar length = 5;
  40. SkScalar step = angle;
  41. SkPath path;
  42. path.moveTo(center);
  43. for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
  44. SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
  45. length*SkScalarSin(step) + center.fY);
  46. path.lineTo(rp);
  47. length += angle / SkScalarHalf(SK_ScalarPI);
  48. step += angle;
  49. }
  50. SkPaint paint;
  51. paint.setAntiAlias(true);
  52. paint.setStyle(SkPaint::kStroke_Style);
  53. paint.setColor(ToolUtils::color_to_565(0xFF007700));
  54. canvas->drawPath(path, paint);
  55. }
  56. bool onAnimate(double nanos) override {
  57. constexpr SkScalar kDesiredDurationSecs = 3.0f;
  58. // Make the animation ping-pong back and forth but start in the fully drawn state
  59. SkScalar fraction = 1.0f - TimeUtils::Scaled(1e-9 * nanos, 2.0f/kDesiredDurationSecs, 2.0f);
  60. if (fraction <= 0.0f) {
  61. fraction = -fraction;
  62. }
  63. SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
  64. fNumSteps = (int) (fraction * kMaxNumSteps);
  65. return true;
  66. }
  67. private:
  68. int fNumSteps;
  69. typedef GM INHERITED;
  70. };
  71. DEF_GM( return new StringArtGM; )
  72. /////////////////////////////////////////////////////////////////////////////////////////////////
  73. #if 0
  74. #include "modules/skottie/include/Skottie.h"
  75. class SkottieGM : public skiagm::GM {
  76. enum {
  77. kWidth = 800,
  78. kHeight = 600,
  79. };
  80. enum {
  81. N = 100,
  82. };
  83. skottie::Animation* fAnims[N];
  84. SkRect fRects[N];
  85. SkScalar fDur;
  86. public:
  87. SkottieGM() {
  88. sk_bzero(fAnims, sizeof(fAnims));
  89. }
  90. ~SkottieGM() override {
  91. for (auto anim : fAnims) {
  92. SkSafeUnref(anim);
  93. }
  94. }
  95. protected:
  96. SkString onShortName() override { return SkString("skottie"); }
  97. SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
  98. void init() {
  99. SkRandom rand;
  100. auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json");
  101. // for (;;) skottie::Animation::Make((const char*)data->data(), data->size());
  102. for (int i = 0; i < N; ++i) {
  103. fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release();
  104. SkScalar x = rand.nextF() * kWidth;
  105. SkScalar y = rand.nextF() * kHeight;
  106. fRects[i].setXYWH(x, y, 400, 400);
  107. }
  108. fDur = fAnims[0]->duration();
  109. }
  110. void onDraw(SkCanvas* canvas) override {
  111. if (!fAnims[0]) {
  112. this->init();
  113. }
  114. canvas->drawColor(0xFFBBBBBB);
  115. for (int i = 0; i < N; ++i) {
  116. fAnims[0]->render(canvas, &fRects[i]);
  117. }
  118. }
  119. bool onAnimate(double nanos) override {
  120. SkScalar time = (float)(fmod(1e-9 * nanos, fDur) / fDur);
  121. for (auto anim : fAnims) {
  122. anim->seek(time);
  123. }
  124. return true;
  125. }
  126. private:
  127. typedef GM INHERITED;
  128. };
  129. DEF_GM( return new SkottieGM; )
  130. #endif