addarc.cpp 1.57 KB
Newer Older
reed's avatar
reed committed
1 2 3 4 5 6 7 8
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
9
#include "SkAnimTimer.h"
reed's avatar
reed committed
10 11 12 13
#include "SkCanvas.h"
#include "SkRandom.h"

class AddArcGM : public skiagm::GM {
reed's avatar
reed committed
14 15 16
public:
    AddArcGM() : fRotate(0) {}

reed's avatar
reed committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
protected:
    SkString onShortName() SK_OVERRIDE { return SkString("addarc"); }

    SkISize onISize() SK_OVERRIDE { return SkISize::Make(1040, 1040); }

    void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        canvas->translate(20, 20);

        SkRect r = SkRect::MakeWH(1000, 1000);

        SkPaint paint;
        paint.setAntiAlias(true);
        paint.setStyle(SkPaint::kStroke_Style);
        paint.setStrokeWidth(15);

        const SkScalar inset = paint.getStrokeWidth() + 4;
        const SkScalar sweepAngle = 345;
        SkRandom rand;

reed's avatar
reed committed
36
        SkScalar sign = 1;
reed's avatar
reed committed
37 38 39 40
        while (r.width() > paint.getStrokeWidth() * 3) {
            paint.setColor(rand.nextU() | (0xFF << 24));
            SkScalar startAngle = rand.nextUScalar1() * 360;

reed's avatar
reed committed
41 42 43
            SkScalar speed = SkScalarSqrt(16 / r.width()) * 0.5f;
            startAngle += fRotate * 360 * speed * sign;

reed's avatar
reed committed
44 45 46 47 48
            SkPath path;
            path.addArc(r, startAngle, sweepAngle);
            canvas->drawPath(path, paint);

            r.inset(inset, inset);
reed's avatar
reed committed
49
            sign = -sign;
reed's avatar
reed committed
50 51 52
        }
    }

53 54
    bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
        fRotate = timer.scaled(1, 360);
reed's avatar
reed committed
55 56 57
        return true;
    }

reed's avatar
reed committed
58
private:
reed's avatar
reed committed
59
    SkScalar fRotate;
reed's avatar
reed committed
60 61 62
    typedef skiagm::GM INHERITED;
};
DEF_GM( return new AddArcGM; )