2013-09-19 15:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
#include "tools/timer/AnimTimer.h"
|
2013-09-19 15:32:22 +00:00
|
|
|
|
|
|
|
// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kWidth = 440;
|
|
|
|
constexpr int kHeight = 440;
|
|
|
|
constexpr SkScalar kAngle = 0.305f;
|
|
|
|
constexpr int kMaxNumSteps = 140;
|
2013-09-19 15:32:22 +00:00
|
|
|
|
|
|
|
// Renders a string art shape.
|
|
|
|
// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
|
|
|
|
|
|
|
|
class StringArtGM : public skiagm::GM {
|
|
|
|
public:
|
2016-04-08 20:35:14 +00:00
|
|
|
StringArtGM() : fNumSteps(kMaxNumSteps) {}
|
2013-09-19 15:32:22 +00:00
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-09-19 15:32:22 +00:00
|
|
|
return SkString("stringart");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-09-19 15:32:22 +00:00
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-09-19 15:32:22 +00:00
|
|
|
SkScalar angle = kAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI);
|
2013-09-19 17:21:39 +00:00
|
|
|
SkScalar size = SkIntToScalar(SkMin32(kWidth, kHeight));
|
2013-09-19 15:32:22 +00:00
|
|
|
SkPoint center = SkPoint::Make(SkScalarHalf(kWidth), SkScalarHalf(kHeight));
|
|
|
|
SkScalar length = 5;
|
|
|
|
SkScalar step = angle;
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(center);
|
|
|
|
|
2016-04-08 20:35:14 +00:00
|
|
|
for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
|
2013-09-19 15:32:22 +00:00
|
|
|
SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
|
|
|
|
length*SkScalarSin(step) + center.fY);
|
|
|
|
path.lineTo(rp);
|
2015-05-12 17:37:34 +00:00
|
|
|
length += angle / SkScalarHalf(SK_ScalarPI);
|
2013-09-19 15:32:22 +00:00
|
|
|
step += angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2019-03-20 16:12:10 +00:00
|
|
|
paint.setColor(ToolUtils::color_to_565(0xFF007700));
|
2013-09-19 15:32:22 +00:00
|
|
|
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kDesiredDurationSecs = 3.0f;
|
2016-04-08 20:35:14 +00:00
|
|
|
|
|
|
|
// Make the animation ping-pong back and forth but start in the fully drawn state
|
|
|
|
SkScalar fraction = 1.0f - timer.scaled(2.0f/kDesiredDurationSecs, 2.0f);
|
|
|
|
if (fraction <= 0.0f) {
|
|
|
|
fraction = -fraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
|
|
|
|
|
|
|
|
fNumSteps = (int) (fraction * kMaxNumSteps);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-19 15:32:22 +00:00
|
|
|
private:
|
2016-04-08 20:35:14 +00:00
|
|
|
int fNumSteps;
|
|
|
|
|
2013-09-19 15:32:22 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new StringArtGM; )
|
2018-08-13 20:03:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if 0
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/skottie/include/Skottie.h"
|
2018-08-13 20:03:25 +00:00
|
|
|
|
|
|
|
class SkottieGM : public skiagm::GM {
|
|
|
|
enum {
|
|
|
|
kWidth = 800,
|
|
|
|
kHeight = 600,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
N = 100,
|
|
|
|
};
|
|
|
|
skottie::Animation* fAnims[N];
|
|
|
|
SkRect fRects[N];
|
|
|
|
SkScalar fDur;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SkottieGM() {
|
|
|
|
sk_bzero(fAnims, sizeof(fAnims));
|
|
|
|
}
|
|
|
|
~SkottieGM() override {
|
|
|
|
for (auto anim : fAnims) {
|
|
|
|
SkSafeUnref(anim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("skottie"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
SkRandom rand;
|
|
|
|
auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json");
|
|
|
|
// for (;;) skottie::Animation::Make((const char*)data->data(), data->size());
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release();
|
|
|
|
SkScalar x = rand.nextF() * kWidth;
|
|
|
|
SkScalar y = rand.nextF() * kHeight;
|
|
|
|
fRects[i].setXYWH(x, y, 400, 400);
|
|
|
|
}
|
|
|
|
fDur = fAnims[0]->duration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
if (!fAnims[0]) {
|
|
|
|
this->init();
|
|
|
|
}
|
|
|
|
canvas->drawColor(0xFFBBBBBB);
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fAnims[0]->render(canvas, &fRects[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2018-08-13 20:03:25 +00:00
|
|
|
SkScalar time = (float)(fmod(timer.secs(), fDur) / fDur);
|
|
|
|
for (auto anim : fAnims) {
|
|
|
|
anim->seek(time);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new SkottieGM; )
|
|
|
|
#endif
|
|
|
|
|