Debian Codegraph @

gm/stringart.cpp

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