blob: 73a214d758b62eef2d06adce95d2cca5eef983cb [file] [log] [blame]
jvanverth@google.com4ea28782013-09-19 15:32:22 +00001/*
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.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips651bb5f2016-04-08 13:35:14 -070010#include "SkAnimTimer.h"
jvanverth@google.com4ea28782013-09-19 15:32:22 +000011#include "SkCanvas.h"
12#include "SkPath.h"
13
14// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
15
mtkleindbfd7ab2016-09-01 11:24:54 -070016constexpr int kWidth = 440;
17constexpr int kHeight = 440;
18constexpr SkScalar kAngle = 0.305f;
19constexpr int kMaxNumSteps = 140;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000020
21// Renders a string art shape.
22// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
23
24class StringArtGM : public skiagm::GM {
25public:
robertphillips651bb5f2016-04-08 13:35:14 -070026 StringArtGM() : fNumSteps(kMaxNumSteps) {}
jvanverth@google.com4ea28782013-09-19 15:32:22 +000027
28protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000029
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000031 return SkString("stringart");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000035 return SkISize::Make(kWidth, kHeight);
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000039 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
jvanverth@google.comeb95d6a2013-09-19 17:21:39 +000040 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000041 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
42 SkScalar length = 5;
43 SkScalar step = angle;
44
45 SkPath path;
46 path.moveTo(center);
47
robertphillips651bb5f2016-04-08 13:35:14 -070048 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000049 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
50 length*SkScalarSin(step) + center.fY);
51 path.lineTo(rp);
reed80ea19c2015-05-12 10:37:34 -070052 length += angle / SkScalarHalf(SK_ScalarPI);
jvanverth@google.com4ea28782013-09-19 15:32:22 +000053 step += angle;
54 }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000055
56 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setStyle(SkPaint::kStroke_Style);
caryclark12596012015-07-29 05:27:47 -070059 paint.setColor(sk_tool_utils::color_to_565(0xFF007700));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000060
61 canvas->drawPath(path, paint);
62 }
63
robertphillips651bb5f2016-04-08 13:35:14 -070064 bool onAnimate(const SkAnimTimer& timer) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070065 constexpr SkScalar kDesiredDurationSecs = 3.0f;
robertphillips651bb5f2016-04-08 13:35:14 -070066
67 // Make the animation ping-pong back and forth but start in the fully drawn state
68 SkScalar fraction = 1.0f - timer.scaled(2.0f/kDesiredDurationSecs, 2.0f);
69 if (fraction <= 0.0f) {
70 fraction = -fraction;
71 }
72
73 SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
74
75 fNumSteps = (int) (fraction * kMaxNumSteps);
76 return true;
77 }
78
jvanverth@google.com4ea28782013-09-19 15:32:22 +000079private:
robertphillips651bb5f2016-04-08 13:35:14 -070080 int fNumSteps;
81
jvanverth@google.com4ea28782013-09-19 15:32:22 +000082 typedef GM INHERITED;
83};
84
85DEF_GM( return new StringArtGM; )