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