blob: ccc02d31013a494205d4c5fe1785f432b903dab1 [file] [log] [blame]
Hal Canary88000422020-01-15 11:51:12 -05001// 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.
3#include "tools/fiddle/examples.h"
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->getGrContext()) {
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