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 "sk_tool_utils.h" 10 #include "SkAnimTimer.h" 11 #include "SkCanvas.h" 12 #include "SkPath.h" 13 14 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 15 16 constexpr int kWidth = 440; 17 constexpr int kHeight = 440; 18 constexpr SkScalar kAngle = 0.305f; 19 constexpr int kMaxNumSteps = 140; 20 21 // Renders a string art shape. 22 // The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1 23 24 class StringArtGM : public skiagm::GM { 25 public: 26 StringArtGM() : fNumSteps(kMaxNumSteps) {} 27 28 protected: 29 30 SkString onShortName() override { 31 return SkString("stringart"); 32 } 33 34 SkISize onISize() override { 35 return SkISize::Make(kWidth, kHeight); 36 } 37 38 void onDraw(SkCanvas* canvas) override { 39 SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); 40 SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight)); 41 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 48 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) { 49 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, 50 length*SkScalarSin(step) + center.fY); 51 path.lineTo(rp); 52 length += angle / SkScalarHalf(SK_ScalarPI); 53 step += angle; 54 } 55 56 SkPaint paint; 57 paint.setAntiAlias(true); 58 paint.setStyle(SkPaint::kStroke_Style); 59 paint.setColor(sk_tool_utils::color_to_565(0xFF007700)); 60 61 canvas->drawPath(path, paint); 62 } 63 64 bool onAnimate(const SkAnimTimer& timer) override { 65 constexpr SkScalar kDesiredDurationSecs = 3.0f; 66 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 79 private: 80 int fNumSteps; 81 82 typedef GM INHERITED; 83 }; 84 85 DEF_GM( return new StringArtGM; ) 86