blob: 74f3bd800af8190f6291e54a4809e8a5e6fe05a8 [file] [log] [blame]
jvanverth@google.com4ea28782013-09-19 15:32:22 +00001/*
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 */
bungemand3ebb482015-08-05 13:57:49 -07007
jvanverth@google.com4ea28782013-09-19 15:32:22 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
jvanverth@google.com4ea28782013-09-19 15:32:22 +000011
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
18class StringArtView : public SampleView {
19public:
20 StringArtView() : fAngle(0.305f) {}
21
22protected:
23 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070024 bool onQuery(SkEvent* evt) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000025 if (SampleCode::TitleQ(*evt)) {
26 SampleCode::TitleR(evt, "StringArt");
27 return true;
28 }
29 return this->INHERITED::onQuery(evt);
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 void onDrawContent(SkCanvas* canvas) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000033 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
jvanverth@google.com417446a2013-09-19 16:32:49 +000042 while (length < (SkScalarHalf(SkMinScalar(this->width(), this->height())) - 10.f))
jvanverth@google.com4ea28782013-09-19 15:32:22 +000043 {
44 SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
45 length*SkScalarSin(step) + center.fY);
46 path.lineTo(rp);
reed80ea19c2015-05-12 10:37:34 -070047 length += angle / SkScalarHalf(SK_ScalarPI);
jvanverth@google.com4ea28782013-09-19 15:32:22 +000048 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
mtklein36352bf2015-03-25 18:17:31 -070060 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
jvanverth@google.com4ea28782013-09-19 15:32:22 +000061 fAngle = x/width();
halcanary96fcdcc2015-08-27 07:41:13 -070062 this->inval(nullptr);
63 return nullptr;
jvanverth@google.com4ea28782013-09-19 15:32:22 +000064 }
65private:
66
67 SkScalar fAngle;
68 typedef SampleView INHERITED;
69};
70
71//////////////////////////////////////////////////////////////////////////////
72
73static SkView* MyFactory() { return new StringArtView; }
74static SkViewRegister reg(MyFactory);