blob: 1da87578d0da3f3251cee7b2ccc7d8c553411e40 [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"
robertphillips651bb5f2016-04-08 13:35:14 -07009#include "SkAnimTimer.h"
jvanverth@google.com4ea28782013-09-19 15:32:22 +000010#include "SkCanvas.h"
11#include "SkPath.h"
12
13// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
14
reed6dc14aa2016-04-11 07:46:38 -070015static const int kWidth = 440;
16static const int kHeight = 440;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000017static const SkScalar kAngle = 0.305f;
robertphillips651bb5f2016-04-08 13:35:14 -070018static const int kMaxNumSteps = 140;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000019
20// Renders a string art shape.
21// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
22
23class StringArtGM : public skiagm::GM {
24public:
robertphillips651bb5f2016-04-08 13:35:14 -070025 StringArtGM() : fNumSteps(kMaxNumSteps) {}
jvanverth@google.com4ea28782013-09-19 15:32:22 +000026
27protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000030 return SkString("stringart");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000034 return SkISize::Make(kWidth, kHeight);
35 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 void onDraw(SkCanvas* canvas) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000038 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
jvanverth@google.comeb95d6a2013-09-19 17:21:39 +000039 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000040 SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
41 SkScalar length = 5;
42 SkScalar step = angle;
43
44 SkPath path;
45 path.moveTo(center);
46
robertphillips651bb5f2016-04-08 13:35:14 -070047 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000048 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
49 length*SkScalarSin(step) + center.fY);
50 path.lineTo(rp);
reed80ea19c2015-05-12 10:37:34 -070051 length += angle / SkScalarHalf(SK_ScalarPI);
jvanverth@google.com4ea28782013-09-19 15:32:22 +000052 step += angle;
53 }
jvanverth@google.com4ea28782013-09-19 15:32:22 +000054
55 SkPaint paint;
56 paint.setAntiAlias(true);
57 paint.setStyle(SkPaint::kStroke_Style);
caryclark12596012015-07-29 05:27:47 -070058 paint.setColor(sk_tool_utils::color_to_565(0xFF007700));
jvanverth@google.com4ea28782013-09-19 15:32:22 +000059
60 canvas->drawPath(path, paint);
61 }
62
robertphillips651bb5f2016-04-08 13:35:14 -070063 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 drawn 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
jvanverth@google.com4ea28782013-09-19 15:32:22 +000078private:
robertphillips651bb5f2016-04-08 13:35:14 -070079 int fNumSteps;
80
jvanverth@google.com4ea28782013-09-19 15:32:22 +000081 typedef GM INHERITED;
82};
83
84DEF_GM( return new StringArtGM; )