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.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE_ANIMATED(pong, 256, 256, false, 0, 10) {
5 static 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));
13 void 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);
19 p.setColor(SK_ColorWHITE);
22 float bX = ballX * 472 + 20;
23 float bY = ballY * 200 + 28;
25 if (canvas->recordingContext()) {
26 canvas->drawRect(SkRect::MakeXYWH(236, bY - 15, 10, 30), p);
29 canvas->drawRect(SkRect::MakeXYWH(10, bY - 15, 10, 30), p);
31 canvas->drawCircle(bX, bY, 5, p);