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 "SkAnimTimer.h" |
9 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
10 #include "SkPath.h" | 11 #include "SkPath.h" |
11 | 12 |
12 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 | 13 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 |
13 | 14 |
14 static const int kWidth = 640; | 15 static const int kWidth = 640; |
15 static const int kHeight = 480; | 16 static const int kHeight = 480; |
16 static const SkScalar kAngle = 0.305f; | 17 static const SkScalar kAngle = 0.305f; |
| 18 static const int kMaxNumSteps = 140; |
17 | 19 |
18 // Renders a string art shape. | 20 // Renders a string art shape. |
19 // The particular shape rendered can be controlled by adjusting kAngle, from 0 t
o 1 | 21 // The particular shape rendered can be controlled by adjusting kAngle, from 0 t
o 1 |
20 | 22 |
21 class StringArtGM : public skiagm::GM { | 23 class StringArtGM : public skiagm::GM { |
22 public: | 24 public: |
23 StringArtGM() {} | 25 StringArtGM() : fNumSteps(kMaxNumSteps) {} |
24 | 26 |
25 protected: | 27 protected: |
26 | 28 |
27 SkString onShortName() override { | 29 SkString onShortName() override { |
28 return SkString("stringart"); | 30 return SkString("stringart"); |
29 } | 31 } |
30 | 32 |
31 SkISize onISize() override { | 33 SkISize onISize() override { |
32 return SkISize::Make(kWidth, kHeight); | 34 return SkISize::Make(kWidth, kHeight); |
33 } | 35 } |
34 | 36 |
35 void onDraw(SkCanvas* canvas) override { | 37 void onDraw(SkCanvas* canvas) override { |
36 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); | 38 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); |
37 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); | 39 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); |
38 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeigh
t)); | 40 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeigh
t)); |
39 SkScalar length = 5; | 41 SkScalar length = 5; |
40 SkScalar step = angle; | 42 SkScalar step = angle; |
41 | 43 |
42 SkPath path; | 44 SkPath path; |
43 path.moveTo(center); | 45 path.moveTo(center); |
44 | 46 |
45 while (length < (SkScalarHalf(size) - 10.f)) | 47 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); +
+i) { |
46 { | |
47 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, | 48 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, |
48 length*SkScalarSin(step) + center.fY); | 49 length*SkScalarSin(step) + center.fY); |
49 path.lineTo(rp); | 50 path.lineTo(rp); |
50 length += angle / SkScalarHalf(SK_ScalarPI); | 51 length += angle / SkScalarHalf(SK_ScalarPI); |
51 step += angle; | 52 step += angle; |
52 } | 53 } |
53 path.close(); | |
54 | 54 |
55 SkPaint paint; | 55 SkPaint paint; |
56 paint.setAntiAlias(true); | 56 paint.setAntiAlias(true); |
57 paint.setStyle(SkPaint::kStroke_Style); | 57 paint.setStyle(SkPaint::kStroke_Style); |
58 paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); | 58 paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); |
59 | 59 |
60 canvas->drawPath(path, paint); | 60 canvas->drawPath(path, paint); |
61 } | 61 } |
62 | 62 |
| 63 bool onAnimate(const SkAnimTimer& timer) override { |
| 64 static const SkScalar kDesiredDurationSecs = 3.0f; |
| 65 |
| 66 // Make the animation ping-pong back and forth but start in the fully dr
awn state |
| 67 SkScalar fraction = 1.0f - timer.scaled(2.0f/kDesiredDurationSecs, 2.0f)
; |
| 68 if (fraction <= 0.0f) { |
| 69 fraction = -fraction; |
| 70 } |
| 71 |
| 72 SkASSERT(fraction >= 0.0f && fraction <= 1.0f); |
| 73 |
| 74 fNumSteps = (int) (fraction * kMaxNumSteps); |
| 75 return true; |
| 76 } |
| 77 |
63 private: | 78 private: |
| 79 int fNumSteps; |
| 80 |
64 typedef GM INHERITED; | 81 typedef GM INHERITED; |
65 }; | 82 }; |
66 | 83 |
67 DEF_GM( return new StringArtGM; ) | 84 DEF_GM( return new StringArtGM; ) |
OLD | NEW |