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 "Sample.h" 9 #include "SkCanvas.h" 10 #include "SkPath.h" 11 12 // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 13 14 // Renders a string art shape. 15 // The particular shape rendered can be controlled by clicking horizontally, thereby 16 // generating an angle from 0 to 1. 17 18 class StringArtView : public Sample { 19 public: StringArtView()20 StringArtView() : fAngle(0.305f) {} 21 22 protected: onQuery(Sample::Event * evt)23 bool onQuery(Sample::Event* evt) override { 24 if (Sample::TitleQ(*evt)) { 25 Sample::TitleR(evt, "StringArt"); 26 return true; 27 } 28 return this->INHERITED::onQuery(evt); 29 } 30 onDrawContent(SkCanvas * canvas)31 void onDrawContent(SkCanvas* canvas) override { 32 SkScalar angle = fAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); 33 34 SkPoint center = SkPoint::Make(SkScalarHalf(this->width()), SkScalarHalf(this->height())); 35 SkScalar length = 5; 36 SkScalar step = angle; 37 38 SkPath path; 39 path.moveTo(center); 40 41 while (length < (SkScalarHalf(SkMinScalar(this->width(), this->height())) - 10.f)) 42 { 43 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, 44 length*SkScalarSin(step) + center.fY); 45 path.lineTo(rp); 46 length += angle / SkScalarHalf(SK_ScalarPI); 47 step += angle; 48 } 49 path.close(); 50 51 SkPaint paint; 52 paint.setAntiAlias(true); 53 paint.setStyle(SkPaint::kStroke_Style); 54 paint.setColor(0xFF007700); 55 56 canvas->drawPath(path, paint); 57 } 58 onFindClickHandler(SkScalar x,SkScalar y,unsigned)59 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { 60 fAngle = x/width(); 61 return nullptr; 62 } 63 private: 64 65 SkScalar fAngle; 66 typedef Sample INHERITED; 67 }; 68 69 ////////////////////////////////////////////////////////////////////////////// 70 71 DEF_SAMPLE( return new StringArtView(); ) 72