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 "include/core/SkCanvas.h"
9 #include "include/core/SkPath.h"
10 #include "samplecode/Sample.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:
name()23     SkString name() override { return SkString("StringArt"); }
24 
onDrawContent(SkCanvas * canvas)25     void onDrawContent(SkCanvas* canvas) override {
26         SkScalar angle = fAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
27 
28         SkPoint center = SkPoint::Make(SkScalarHalf(this->width()), SkScalarHalf(this->height()));
29         SkScalar length = 5;
30         SkScalar step = angle;
31 
32         SkPath path;
33         path.moveTo(center);
34 
35         while (length < (SkScalarHalf(std::min(this->width(), this->height())) - 10.f))
36         {
37             SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
38                                        length*SkScalarSin(step) + center.fY);
39             path.lineTo(rp);
40             length += angle / SkScalarHalf(SK_ScalarPI);
41             step += angle;
42         }
43         path.close();
44 
45         SkPaint paint;
46         paint.setAntiAlias(true);
47         paint.setStyle(SkPaint::kStroke_Style);
48         paint.setColor(0xFF007700);
49 
50         canvas->drawPath(path, paint);
51     }
52 
onFindClickHandler(SkScalar x,SkScalar y,skui::ModifierKey)53     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
54         fAngle = x/width();
55         return nullptr;
56     }
57 private:
58 
59     SkScalar fAngle;
60     using INHERITED = Sample;
61 };
62 
63 //////////////////////////////////////////////////////////////////////////////
64 
65 DEF_SAMPLE( return new StringArtView(); )
66