Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
pong2.cpp
Go to the documentation of this file.
1// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4REG_FIDDLE_ANIMATED(pong2, 512, 256, false, 0, 10) {
5static SkScalar PingPong(double t, SkScalar period, SkScalar phase,
6 SkScalar ends, SkScalar mid) {
7 double value = ::fmod(t + phase, period);
8 double half = period / 2.0;
9 double diff = ::fabs(value - half);
10 return SkDoubleToScalar(ends + (1.0 - diff / half) * (mid - ends));
11}
12
13void draw(SkCanvas* canvas) {
14 float bX = PingPong(frame * duration, 2.5f, 0.0f, 0, 1) * 472 + 20;
15 float bY = PingPong(frame * duration, 2.0f, 0.4f, 0, 1) * 200 + 28;
16
17 SkPaint p;
18 p.setColor(SK_ColorWHITE);
19 p.setAntiAlias(true);
20
21 canvas->clear(SK_ColorBLACK);
22 canvas->drawRect(SkRect::MakeXYWH(492, bY - 15, 10, 30), p);
23 canvas->drawRect(SkRect::MakeXYWH(10, bY - 15, 10, 30), p);
24 canvas->drawCircle(bX, bY, 5, p);
25
26 const float intervals[] = { 12, 6 };
27 p.setStrokeWidth(5);
28 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
29 canvas->drawLine({256,0}, {256, 256}, p);
30}
31} // END FIDDLE
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkDoubleToScalar(x)
Definition SkScalar.h:64
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
double duration
Definition examples.cpp:30
double frame
Definition examples.cpp:31
#define REG_FIDDLE_ANIMATED(NAME, W, H, T, I, DURATION)
Definition examples.h:56
float SkScalar
Definition extension.cpp:12
uint8_t value
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659