OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "SkPath.h" | 10 #include "SkPath.h" |
10 #include "SkDashPathEffect.h" | 11 #include "SkDashPathEffect.h" |
11 | 12 |
12 int dash1[] = { 1, 1 }; | 13 int dash1[] = { 1, 1 }; |
13 int dash2[] = { 1, 3 }; | 14 int dash2[] = { 1, 3 }; |
14 int dash3[] = { 1, 1, 3, 3 }; | 15 int dash3[] = { 1, 1, 3, 3 }; |
15 int dash4[] = { 1, 3, 2, 4 }; | 16 int dash4[] = { 1, 3, 2, 4 }; |
16 | 17 |
17 struct DashExample { | 18 struct DashExample { |
18 int* pattern; | 19 int* pattern; |
19 int length; | 20 int length; |
20 } dashExamples[] = { | 21 } dashExamples[] = { |
21 { dash1, SK_ARRAY_COUNT(dash1) }, | 22 { dash1, SK_ARRAY_COUNT(dash1) }, |
22 { dash2, SK_ARRAY_COUNT(dash2) }, | 23 { dash2, SK_ARRAY_COUNT(dash2) }, |
23 { dash3, SK_ARRAY_COUNT(dash3) }, | 24 { dash3, SK_ARRAY_COUNT(dash3) }, |
24 { dash4, SK_ARRAY_COUNT(dash4) } | 25 { dash4, SK_ARRAY_COUNT(dash4) } |
25 }; | 26 }; |
26 | 27 |
27 DEF_SIMPLE_GM(dashcircle, canvas, 900, 1200) { | 28 |
28 SkPaint refPaint; | 29 class DashCircleGM : public skiagm::GM { |
29 refPaint.setAntiAlias(true); | 30 public: |
30 refPaint.setColor(0xFFbf3f7f); | 31 DashCircleGM() : fRotation(0) { } |
31 refPaint.setStyle(SkPaint::kStroke_Style); | 32 |
32 refPaint.setStrokeWidth(1); | 33 protected: |
33 const SkScalar radius = 125; | 34 SkString onShortName() override { return SkString("dashcircle"); } |
34 SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radi
us + 20); | 35 |
35 SkPath circle; | 36 SkISize onISize() override { return SkISize::Make(900, 1200); } |
36 circle.addCircle(0, 0, radius); | 37 |
37 SkScalar circumference = radius * SK_ScalarPI * 2; | 38 void onDraw(SkCanvas* canvas) override { |
38 int wedges[] = { 6, 12, 36 }; | 39 SkPaint refPaint; |
39 canvas->translate(radius + 20, radius + 20); | 40 refPaint.setAntiAlias(true); |
40 for (int wedge : wedges) { | 41 refPaint.setColor(0xFFbf3f7f); |
41 SkScalar arcLength = 360.f / wedge; | 42 refPaint.setStyle(SkPaint::kStroke_Style); |
42 canvas->save(); | 43 refPaint.setStrokeWidth(1); |
43 for (const DashExample& dashExample : dashExamples) { | 44 const SkScalar radius = 125; |
44 SkPath refPath; | 45 SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20,
radius + 20); |
45 int dashUnits = 0; | 46 SkPath circle; |
46 for (int index = 0; index < dashExample.length; ++index) { | 47 circle.addCircle(0, 0, radius); |
47 dashUnits += dashExample.pattern[index]; | 48 SkScalar circumference = radius * SK_ScalarPI * 2; |
| 49 int wedges[] = { 6, 12, 36 }; |
| 50 canvas->translate(radius+20, radius+20); |
| 51 for (int wedge : wedges) { |
| 52 SkScalar arcLength = 360.f / wedge; |
| 53 canvas->save(); |
| 54 for (const DashExample& dashExample : dashExamples) { |
| 55 SkPath refPath; |
| 56 int dashUnits = 0; |
| 57 for (int index = 0; index < dashExample.length; ++index) { |
| 58 dashUnits += dashExample.pattern[index]; |
| 59 } |
| 60 SkScalar unitLength = arcLength / dashUnits; |
| 61 SkScalar angle = 0; |
| 62 for (int index = 0; index < wedge; ++index) { |
| 63 for (int i2 = 0; i2 < dashExample.length; i2 += 2) { |
| 64 SkScalar span = dashExample.pattern[i2] * unitLength; |
| 65 refPath.moveTo(0, 0); |
| 66 refPath.arcTo(oval, angle, span, false); |
| 67 refPath.close(); |
| 68 angle += span + (dashExample.pattern[i2 + 1]) * unitLeng
th; |
| 69 } |
| 70 } |
| 71 canvas->save(); |
| 72 canvas->rotate(fRotation); |
| 73 canvas->drawPath(refPath, refPaint); |
| 74 canvas->restore(); |
| 75 SkPaint p; |
| 76 p.setAntiAlias(true); |
| 77 p.setStyle(SkPaint::kStroke_Style); |
| 78 p.setStrokeWidth(10); |
| 79 SkScalar intervals[4]; |
| 80 int intervalCount = dashExample.length; |
| 81 SkScalar dashLength = circumference / wedge / dashUnits; |
| 82 for (int index = 0; index < dashExample.length; ++index) { |
| 83 intervals[index] = dashExample.pattern[index] * dashLength; |
| 84 } |
| 85 p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount,
0)); |
| 86 canvas->save(); |
| 87 canvas->rotate(fRotation); |
| 88 canvas->drawPath(circle, p); |
| 89 canvas->restore(); |
| 90 canvas->translate(0, radius * 2 + 50); |
48 } | 91 } |
49 SkScalar unitLength = arcLength / dashUnits; | 92 canvas->restore(); |
50 SkScalar angle = 0; | 93 canvas->translate(radius * 2 + 50, 0); |
51 for (int index = 0; index < wedge; ++index) { | |
52 for (int i2 = 0; i2 < dashExample.length; i2 += 2) { | |
53 SkScalar span = dashExample.pattern[i2] * unitLength; | |
54 refPath.moveTo(0, 0); | |
55 refPath.arcTo(oval, angle, span, false); | |
56 refPath.close(); | |
57 angle += span + (dashExample.pattern[i2 + 1]) * unitLength; | |
58 } | |
59 } | |
60 canvas->drawPath(refPath, refPaint); | |
61 SkPaint p; | |
62 p.setAntiAlias(true); | |
63 p.setStyle(SkPaint::kStroke_Style); | |
64 p.setStrokeWidth(10); | |
65 SkScalar intervals[4]; | |
66 int intervalCount = dashExample.length; | |
67 SkScalar dashLength = circumference / wedge / dashUnits; | |
68 for (int index = 0; index < dashExample.length; ++index) { | |
69 intervals[index] = dashExample.pattern[index] * dashLength; | |
70 } | |
71 p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0))
; | |
72 canvas->drawPath(circle, p); | |
73 canvas->translate(0, radius * 2 + 50); | |
74 } | 94 } |
75 canvas->restore(); | |
76 canvas->translate(radius * 2 + 50, 0); | |
77 } | 95 } |
78 } | 96 |
| 97 bool onAnimate(const SkAnimTimer& timer) override { |
| 98 static const SkScalar kDesiredDurationSecs = 100.0f; |
| 99 |
| 100 fRotation = timer.scaled(360.0f/kDesiredDurationSecs, 360.0f); |
| 101 return true; |
| 102 } |
| 103 |
| 104 private: |
| 105 SkScalar fRotation; |
| 106 |
| 107 typedef GM INHERITED; |
| 108 }; |
| 109 |
| 110 DEF_GM(return new DashCircleGM; ) |
OLD | NEW |