skia2/samplecode/SampleStringArt.cpp
reed 2629697933 Revert of stop calling SkScalarDiv (patchset #4 id:60001 of https://codereview.chromium.org/1135053002/)
Reason for revert:
need to wait for Blink roll (and patch android)

Original issue's description:
> stop calling SkScalarDiv
>
> BUG=skia:
> TBR=
>
> Committed: https://skia.googlesource.com/skia/+/67d71c898249a7af3523b16c6a69895a63bfae0a

TBR=
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review URL: https://codereview.chromium.org/1138263002
2015-05-12 10:28:06 -07:00

73 lines
2.1 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SampleCode.h"
#include "SkCanvas.h"
// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
// Renders a string art shape.
// The particular shape rendered can be controlled by clicking horizontally, thereby
// generating an angle from 0 to 1.
class StringArtView : public SampleView {
public:
StringArtView() : fAngle(0.305f) {}
protected:
// overrides from SkEventSink
bool onQuery(SkEvent* evt) override {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "StringArt");
return true;
}
return this->INHERITED::onQuery(evt);
}
void onDrawContent(SkCanvas* canvas) override {
SkScalar angle = fAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
SkPoint center = SkPoint::Make(SkScalarHalf(this->width()), SkScalarHalf(this->height()));
SkScalar length = 5;
SkScalar step = angle;
SkPath path;
path.moveTo(center);
while (length < (SkScalarHalf(SkMinScalar(this->width(), this->height())) - 10.f))
{
SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
length*SkScalarSin(step) + center.fY);
path.lineTo(rp);
length += SkScalarDiv(angle, SkScalarHalf(SK_ScalarPI));
step += angle;
}
path.close();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(0xFF007700);
canvas->drawPath(path, paint);
}
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
fAngle = x/width();
this->inval(NULL);
return NULL;
}
private:
SkScalar fAngle;
typedef SampleView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new StringArtView; }
static SkViewRegister reg(MyFactory);