| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkPath.h" | 10 #include "SkPath.h" |
| 11 | 11 |
| 12 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 | 12 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 |
| 13 | 13 |
| 14 static const int kWidth = 640; | 14 static const int kWidth = 640; |
| 15 static const int kHeight = 480; | 15 static const int kHeight = 480; |
| 16 static const SkScalar kAngle = 0.305f; | 16 static const SkScalar kAngle = 0.305f; |
| 17 static const int kMaxNumSteps = 140; |
| 17 | 18 |
| 18 // Renders a string art shape. | 19 // Renders a string art shape. |
| 19 // The particular shape rendered can be controlled by adjusting kAngle, from 0 t
o 1 | 20 // The particular shape rendered can be controlled by adjusting kAngle, from 0 t
o 1 |
| 20 | 21 |
| 21 class StringArtGM : public skiagm::GM { | 22 class StringArtGM : public skiagm::GM { |
| 22 public: | 23 public: |
| 23 StringArtGM() {} | 24 StringArtGM() : fNumSteps(kMaxNumSteps), fDelta(1) {} |
| 24 | 25 |
| 25 protected: | 26 protected: |
| 26 | 27 |
| 27 SkString onShortName() override { | 28 SkString onShortName() override { |
| 28 return SkString("stringart"); | 29 return SkString("stringart"); |
| 29 } | 30 } |
| 30 | 31 |
| 31 SkISize onISize() override { | 32 SkISize onISize() override { |
| 32 return SkISize::Make(kWidth, kHeight); | 33 return SkISize::Make(kWidth, kHeight); |
| 33 } | 34 } |
| 34 | 35 |
| 35 void onDraw(SkCanvas* canvas) override { | 36 void onDraw(SkCanvas* canvas) override { |
| 36 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); | 37 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); |
| 37 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); | 38 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); |
| 38 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeigh
t)); | 39 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeigh
t)); |
| 39 SkScalar length = 5; | 40 SkScalar length = 5; |
| 40 SkScalar step = angle; | 41 SkScalar step = angle; |
| 41 | 42 |
| 42 SkPath path; | 43 SkPath path; |
| 43 path.moveTo(center); | 44 path.moveTo(center); |
| 44 | 45 |
| 45 while (length < (SkScalarHalf(size) - 10.f)) | 46 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); +
+i) { |
| 46 { | |
| 47 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, | 47 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, |
| 48 length*SkScalarSin(step) + center.fY); | 48 length*SkScalarSin(step) + center.fY); |
| 49 path.lineTo(rp); | 49 path.lineTo(rp); |
| 50 length += angle / SkScalarHalf(SK_ScalarPI); | 50 length += angle / SkScalarHalf(SK_ScalarPI); |
| 51 step += angle; | 51 step += angle; |
| 52 } | 52 } |
| 53 path.close(); | |
| 54 | 53 |
| 55 SkPaint paint; | 54 SkPaint paint; |
| 56 paint.setAntiAlias(true); | 55 paint.setAntiAlias(true); |
| 57 paint.setStyle(SkPaint::kStroke_Style); | 56 paint.setStyle(SkPaint::kStroke_Style); |
| 58 paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); | 57 paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); |
| 59 | 58 |
| 60 canvas->drawPath(path, paint); | 59 canvas->drawPath(path, paint); |
| 61 } | 60 } |
| 62 | 61 |
| 62 bool onAnimate(const SkAnimTimer& timer) override { |
| 63 fNumSteps += fDelta; |
| 64 if (fNumSteps >= kMaxNumSteps) { |
| 65 fDelta = -1; |
| 66 } else if (fNumSteps <= 0) { |
| 67 fDelta = 1; |
| 68 } |
| 69 return true; |
| 70 } |
| 71 |
| 63 private: | 72 private: |
| 73 int fNumSteps; |
| 74 int fDelta; |
| 75 |
| 64 typedef GM INHERITED; | 76 typedef GM INHERITED; |
| 65 }; | 77 }; |
| 66 | 78 |
| 67 DEF_GM( return new StringArtGM; ) | 79 DEF_GM( return new StringArtGM; ) |
| OLD | NEW |