xref: /third_party/skia/docs/examples/pong2.cpp (revision cb93a386)
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"
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, SK_ARRAY_COUNT(intervals), 0));
29  canvas->drawLine({256,0}, {256, 256}, p);
30}
31}  // END FIDDLE
32