Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2059)

Side by Side Diff: gm/dashcircle.cpp

Issue 1870133003: Make some GMs animate (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix bugs Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gm/lighting.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkPath.h" 9 #include "SkPath.h"
10 #include "SkDashPathEffect.h" 10 #include "SkDashPathEffect.h"
11 11
12 int dash1[] = { 1, 1 }; 12 int dash1[] = { 1, 1 };
13 int dash2[] = { 1, 3 }; 13 int dash2[] = { 1, 3 };
14 int dash3[] = { 1, 1, 3, 3 }; 14 int dash3[] = { 1, 1, 3, 3 };
15 int dash4[] = { 1, 3, 2, 4 }; 15 int dash4[] = { 1, 3, 2, 4 };
16 16
17 struct DashExample { 17 struct DashExample {
18 int* pattern; 18 int* pattern;
19 int length; 19 int length;
20 } dashExamples[] = { 20 } dashExamples[] = {
21 { dash1, SK_ARRAY_COUNT(dash1) }, 21 { dash1, SK_ARRAY_COUNT(dash1) },
22 { dash2, SK_ARRAY_COUNT(dash2) }, 22 { dash2, SK_ARRAY_COUNT(dash2) },
23 { dash3, SK_ARRAY_COUNT(dash3) }, 23 { dash3, SK_ARRAY_COUNT(dash3) },
24 { dash4, SK_ARRAY_COUNT(dash4) } 24 { dash4, SK_ARRAY_COUNT(dash4) }
25 }; 25 };
26 26
27 DEF_SIMPLE_GM(dashcircle, canvas, 900, 1200) { 27
28 SkPaint refPaint; 28 class DashCircleGM : public skiagm::GM {
29 refPaint.setAntiAlias(true); 29 public:
30 refPaint.setColor(0xFFbf3f7f); 30 DashCircleGM() : fRotation(0) { }
31 refPaint.setStyle(SkPaint::kStroke_Style); 31
32 refPaint.setStrokeWidth(1); 32 protected:
33 const SkScalar radius = 125; 33 SkString onShortName() override { return SkString("dashcircle"); }
34 SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radi us + 20); 34
35 SkPath circle; 35 SkISize onISize() override { return SkISize::Make(900, 1200); }
36 circle.addCircle(0, 0, radius); 36
37 SkScalar circumference = radius * SK_ScalarPI * 2; 37 void onDraw(SkCanvas* canvas) override {
38 int wedges[] = { 6, 12, 36 }; 38 SkPaint refPaint;
39 canvas->translate(radius + 20, radius + 20); 39 refPaint.setAntiAlias(true);
40 for (int wedge : wedges) { 40 refPaint.setColor(0xFFbf3f7f);
41 SkScalar arcLength = 360.f / wedge; 41 refPaint.setStyle(SkPaint::kStroke_Style);
42 canvas->save(); 42 refPaint.setStrokeWidth(1);
43 for (const DashExample& dashExample : dashExamples) { 43 const SkScalar radius = 125;
44 SkPath refPath; 44 SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radius + 20);
45 int dashUnits = 0; 45 SkPath circle;
46 for (int index = 0; index < dashExample.length; ++index) { 46 circle.addCircle(0, 0, radius);
47 dashUnits += dashExample.pattern[index]; 47 SkScalar circumference = radius * SK_ScalarPI * 2;
48 int wedges[] = { 6, 12, 36 };
49 canvas->translate(radius+20, radius+20);
50 for (int wedge : wedges) {
51 SkScalar arcLength = 360.f / wedge;
52 canvas->save();
53 for (const DashExample& dashExample : dashExamples) {
54 SkPath refPath;
55 int dashUnits = 0;
56 for (int index = 0; index < dashExample.length; ++index) {
57 dashUnits += dashExample.pattern[index];
58 }
59 SkScalar unitLength = arcLength / dashUnits;
60 SkScalar angle = 0;
61 for (int index = 0; index < wedge; ++index) {
62 for (int i2 = 0; i2 < dashExample.length; i2 += 2) {
63 SkScalar span = dashExample.pattern[i2] * unitLength;
64 refPath.moveTo(0, 0);
65 refPath.arcTo(oval, angle, span, false);
66 refPath.close();
67 angle += span + (dashExample.pattern[i2 + 1]) * unitLeng th;
68 }
69 }
70 canvas->save();
71 canvas->rotate(SkIntToScalar(fRotation));
72 canvas->drawPath(refPath, refPaint);
73 canvas->restore();
74 SkPaint p;
75 p.setAntiAlias(true);
76 p.setStyle(SkPaint::kStroke_Style);
77 p.setStrokeWidth(10);
78 SkScalar intervals[4];
79 int intervalCount = dashExample.length;
80 SkScalar dashLength = circumference / wedge / dashUnits;
81 for (int index = 0; index < dashExample.length; ++index) {
82 intervals[index] = dashExample.pattern[index] * dashLength;
83 }
84 p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
85 canvas->save();
86 canvas->rotate(SkIntToScalar(fRotation));
87 canvas->drawPath(circle, p);
88 canvas->restore();
89 canvas->translate(0, radius * 2 + 50);
48 } 90 }
49 SkScalar unitLength = arcLength / dashUnits; 91 canvas->restore();
50 SkScalar angle = 0; 92 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 } 93 }
75 canvas->restore();
76 canvas->translate(radius * 2 + 50, 0);
77 } 94 }
78 } 95
96 bool onAnimate(const SkAnimTimer& timer) override {
97 fRotation = (fRotation + 2) % 360;
jvanverth1 2016/04/08 19:10:56 I suppose it's not a big deal, but the animation s
robertphillips 2016/04/08 20:14:58 Doh - fixed.
98 return true;
99 }
100
101 private:
102 int fRotation;
103
104 typedef GM INHERITED;
105 };
106
107 DEF_GM(return new DashCircleGM; )
OLDNEW
« no previous file with comments | « no previous file | gm/lighting.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698