Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
stringart.cpp
Go to the documentation of this file.
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 "gm/gm.h"
11#include "include/core/SkPath.h"
14#include "include/core/SkSize.h"
17#include "tools/ToolUtils.h"
19
20// Reproduces https://code.google.com/p/chromium/issues/detail?id=279014
21
22constexpr int kWidth = 440;
23constexpr int kHeight = 440;
24constexpr SkScalar kAngle = 0.305f;
25constexpr int kMaxNumSteps = 140;
26
27// Renders a string art shape.
28// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
29
30class StringArtGM : public skiagm::GM {
31public:
32 StringArtGM() : fNumSteps(kMaxNumSteps) {}
33
34protected:
35 SkString getName() const override { return SkString("stringart"); }
36
37 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
38
39 void onDraw(SkCanvas* canvas) override {
41 SkScalar size = SkIntToScalar(std::min(kWidth, kHeight));
43 SkScalar length = 5;
44 SkScalar step = angle;
45
46 SkPath path;
47 path.moveTo(center);
48
49 for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
52 path.lineTo(rp);
54 step += angle;
55 }
56
58 paint.setAntiAlias(true);
60 paint.setColor(ToolUtils::color_to_565(0xFF007700));
61
62 canvas->drawPath(path, paint);
63 }
64
65 bool onAnimate(double nanos) override {
66 constexpr SkScalar kDesiredDurationSecs = 3.0f;
67
68 // Make the animation ping-pong back and forth but start in the fully drawn state
69 SkScalar fraction = 1.0f - TimeUtils::Scaled(1e-9 * nanos, 2.0f/kDesiredDurationSecs, 2.0f);
70 if (fraction <= 0.0f) {
71 fraction = -fraction;
72 }
73
74 SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
75
76 fNumSteps = (int) (fraction * kMaxNumSteps);
77 return true;
78 }
79
80private:
81 int fNumSteps;
82
83 using INHERITED = GM;
84};
85
86DEF_GM( return new StringArtGM; )
87
88/////////////////////////////////////////////////////////////////////////////////////////////////
89
90#if 0
91#include "modules/skottie/include/Skottie.h"
92
93class SkottieGM : public skiagm::GM {
94 enum {
95 kWidth = 800,
96 kHeight = 600,
97 };
98
99 enum {
100 N = 100,
101 };
102 skottie::Animation* fAnims[N];
103 SkRect fRects[N];
104 SkScalar fDur;
105
106public:
107 SkottieGM() {
108 sk_bzero(fAnims, sizeof(fAnims));
109 }
110 ~SkottieGM() override {
111 for (auto anim : fAnims) {
112 SkSafeUnref(anim);
113 }
114 }
115
116protected:
117
118 SkString getName() const override { return SkString("skottie"); }
119
120 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
121
122 void init() {
123 SkRandom rand;
124 auto data = SkData::MakeFromFileName("/Users/reed/Downloads/maps_pinlet.json");
125 // for (;;) skottie::Animation::Make((const char*)data->data(), data->size());
126 for (int i = 0; i < N; ++i) {
127 fAnims[i] = skottie::Animation::Make((const char*)data->data(), data->size()).release();
128 SkScalar x = rand.nextF() * kWidth;
129 SkScalar y = rand.nextF() * kHeight;
130 fRects[i].setXYWH(x, y, 400, 400);
131 }
132 fDur = fAnims[0]->duration();
133 }
134
135 void onDraw(SkCanvas* canvas) override {
136 if (!fAnims[0]) {
137 this->init();
138 }
139 canvas->drawColor(0xFFBBBBBB);
140 for (int i = 0; i < N; ++i) {
141 fAnims[0]->render(canvas, &fRects[i]);
142 }
143 }
144
145 bool onAnimate(double nanos) override {
146 SkScalar time = (float)(fmod(1e-9 * nanos, fDur) / fDur);
147 for (auto anim : fAnims) {
148 anim->seek(time);
149 }
150 return true;
151 }
152
153private:
154 using INHERITED = GM;
155};
156DEF_GM( return new SkottieGM; )
157#endif
158
static int step(int x, SkScalar min, SkScalar max)
Definition BlurTest.cpp:215
#define SkASSERT(cond)
Definition SkAssert.h:116
static void sk_bzero(void *buffer, size_t size)
Definition SkMalloc.h:105
#define INHERITED(method,...)
static void SkSafeUnref(T *obj)
Definition SkRefCnt.h:149
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SkScalarHalf(a)
Definition SkScalar.h:75
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarCos(radians)
Definition SkScalar.h:46
#define SK_ScalarPI
Definition SkScalar.h:21
static SkScalar center(float pos0, float pos1)
Type::kYUV Type::kRGBA() int(0.7 *637)
#define N
Definition beziers.cpp:19
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
void drawPath(const SkPath &path, const SkPaint &paint)
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition SkData.cpp:148
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
float nextF()
Definition SkRandom.h:55
bool onAnimate(double nanos) override
Definition stringart.cpp:65
SkISize getISize() override
Definition stringart.cpp:37
SkString getName() const override
Definition stringart.cpp:35
void onDraw(SkCanvas *canvas) override
Definition stringart.cpp:39
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
double duration() const
Definition Skottie.h:268
static sk_sp< Animation > Make(const char *data, size_t length)
Definition Skottie.cpp:534
void render(SkCanvas *canvas, const SkRect *dst=nullptr) const
Definition Skottie.cpp:482
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
size_t length
double y
double x
static float Scaled(float time, float speed, float period=0)
Definition TimeUtils.h:27
SkColor color_to_565(SkColor color)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
init(device_serial, adb_binary)
Definition _adb_path.py:12
constexpr SkScalar kAngle
Definition stringart.cpp:24
constexpr int kWidth
Definition stringart.cpp:22
constexpr int kMaxNumSteps
Definition stringart.cpp:25
constexpr int kHeight
Definition stringart.cpp:23
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkPoint Make(float x, float y)