| 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)); |
| 11 |
} |
| 12 |
|
| 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); |
| 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 |