Flutter Engine
The Flutter Engine
pong.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(pong, 256, 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 canvas->clear(SK_ColorBLACK);
15 float ballX = PingPong(frame * duration, 2.5f, 0.0f, 0.0f, 1.0f);
16 float ballY = PingPong(frame * duration, 2.0f, 0.4f, 0.0f, 1.0f);
17
18 SkPaint p;
19 p.setColor(SK_ColorWHITE);
20 p.setAntiAlias(true);
21
22 float bX = ballX * 472 + 20;
23 float bY = ballY * 200 + 28;
24
25 if (canvas->recordingContext()) {
26 canvas->drawRect(SkRect::MakeXYWH(236, bY - 15, 10, 30), p);
27 bX -= 256;
28 } else {
29 canvas->drawRect(SkRect::MakeXYWH(10, bY - 15, 10, 30), p);
30 }
31 canvas->drawCircle(bX, bY, 5, p);
32}
33} // 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)
Definition: SkCanvas.cpp:1673
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
double duration
Definition: examples.cpp:30
double frame
Definition: examples.cpp:31
float SkScalar
Definition: extension.cpp:12
uint8_t value
static float PingPong(double time, float period, float phase, float ends, float mid)
Definition: TimeUtils.h:37
REG_FIDDLE_ANIMATED(pong, 256, 256, false, 0, 10)
Definition: pong.cpp:4
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659