jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 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.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | |
| 12 | // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 |
| 13 | |
jvanverth@google.com | eb95d6a | 2013-09-19 17:21:39 +0000 | [diff] [blame] | 14 | static const int kWidth = 640; |
| 15 | static const int kHeight = 480; |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 16 | static const SkScalar kAngle = 0.305f; |
| 17 | |
| 18 | // Renders a string art shape. |
| 19 | // The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1 |
| 20 | |
| 21 | class StringArtGM : public skiagm::GM { |
| 22 | public: |
| 23 | StringArtGM() {} |
| 24 | |
| 25 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkString onShortName() override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 28 | return SkString("stringart"); |
| 29 | } |
| 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkISize onISize() override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 32 | return SkISize::Make(kWidth, kHeight); |
| 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | void onDraw(SkCanvas* canvas) override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 36 | SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); |
jvanverth@google.com | eb95d6a | 2013-09-19 17:21:39 +0000 | [diff] [blame] | 37 | SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 38 | SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight)); |
| 39 | SkScalar length = 5; |
| 40 | SkScalar step = angle; |
| 41 | |
| 42 | SkPath path; |
| 43 | path.moveTo(center); |
| 44 | |
| 45 | while (length < (SkScalarHalf(size) - 10.f)) |
| 46 | { |
| 47 | SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, |
| 48 | length*SkScalarSin(step) + center.fY); |
| 49 | path.lineTo(rp); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 50 | length += angle / SkScalarHalf(SK_ScalarPI); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 51 | step += angle; |
| 52 | } |
| 53 | path.close(); |
| 54 | |
| 55 | SkPaint paint; |
| 56 | paint.setAntiAlias(true); |
| 57 | paint.setStyle(SkPaint::kStroke_Style); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 58 | paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 59 | |
| 60 | canvas->drawPath(path, paint); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | typedef GM INHERITED; |
| 65 | }; |
| 66 | |
| 67 | DEF_GM( return new StringArtGM; ) |