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 14 static const int kWidth = 640; 15 static const int kHeight = 480; 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: 26 virtual uint32_t onGetFlags() const SK_OVERRIDE { 27 return kSkipTiled_Flag; 28 } 29 30 virtual SkString onShortName() { 31 return SkString("stringart"); 32 } 33 34 virtual SkISize onISize() { 35 return SkISize::Make(kWidth, kHeight); 36 } 37 38 virtual void onDraw(SkCanvas* canvas) { 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 while (length < (SkScalarHalf(size) - 10.f)) 49 { 50 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, 51 length*SkScalarSin(step) + center.fY); 52 path.lineTo(rp); 53 length += SkScalarDiv(angle, SkScalarHalf(SK_ScalarPI)); 54 step += angle; 55 } 56 path.close(); 57 58 SkPaint paint; 59 paint.setAntiAlias(true); 60 paint.setStyle(SkPaint::kStroke_Style); 61 paint.setColor(0xFF007700); 62 63 canvas->drawPath(path, paint); 64 } 65 66 private: 67 typedef GM INHERITED; 68 }; 69 70 DEF_GM( return new StringArtGM; ) 71