• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SampleCode.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 SampleView {
19 public:
StringArtView()20     StringArtView() : fAngle(0.305f) {}
21 
22 protected:
23     // overrides from SkEventSink
onQuery(SkEvent * evt)24     bool onQuery(SkEvent* evt) override {
25         if (SampleCode::TitleQ(*evt)) {
26             SampleCode::TitleR(evt, "StringArt");
27             return true;
28         }
29         return this->INHERITED::onQuery(evt);
30     }
31 
onDrawContent(SkCanvas * canvas)32     void onDrawContent(SkCanvas* canvas) override {
33         SkScalar angle = fAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
34 
35         SkPoint center = SkPoint::Make(SkScalarHalf(this->width()), SkScalarHalf(this->height()));
36         SkScalar length = 5;
37         SkScalar step = angle;
38 
39         SkPath path;
40         path.moveTo(center);
41 
42         while (length < (SkScalarHalf(SkMinScalar(this->width(), this->height())) - 10.f))
43         {
44             SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
45                                        length*SkScalarSin(step) + center.fY);
46             path.lineTo(rp);
47             length += angle / SkScalarHalf(SK_ScalarPI);
48             step += angle;
49         }
50         path.close();
51 
52         SkPaint paint;
53         paint.setAntiAlias(true);
54         paint.setStyle(SkPaint::kStroke_Style);
55         paint.setColor(0xFF007700);
56 
57         canvas->drawPath(path, paint);
58     }
59 
onFindClickHandler(SkScalar x,SkScalar y,unsigned)60     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
61         fAngle = x/width();
62         this->inval(nullptr);
63         return nullptr;
64     }
65 private:
66 
67     SkScalar fAngle;
68     typedef SampleView INHERITED;
69 };
70 
71 //////////////////////////////////////////////////////////////////////////////
72 
MyFactory()73 static SkView* MyFactory() { return new StringArtView; }
74 static SkViewRegister reg(MyFactory);
75