blob: be9d6505fad438e9204a1724de7cede5846ce4da [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkFont.h"
10#include "include/core/SkString.h"
11#include "include/effects/SkGradientShader.h"
12#include "samplecode/Sample.h"
13#include "tools/timer/AnimTimer.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000014
15static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
16 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000017 SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000018
19 SkScalar l = rect.fLeft;
20 SkScalar t = rect.fTop;
21 SkScalar w = rect.width();
22 SkScalar h = rect.height();
23
24 SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5));
25
26 SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 };
27 SkPoint c1 = { l + 3 * w / 5, t + h / 2 };
28 SkScalar r0 = w / 5;
29 SkScalar r1 = 2 * w / 5;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000030 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -080031 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
32 pos, SK_ARRAY_COUNT(pos),
Mike Reedfae8fce2019-04-03 10:27:45 -040033 SkTileMode::kClamp));
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000034 canvas->drawRect(rect, paint);
35}
36
37
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040038class DegenerateTwoPtRadialsView : public Sample {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000039public:
40 DegenerateTwoPtRadialsView() {
41 fTime = 0;
42 this->setBGColor(0xFFDDDDDD);
43 }
44
45protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040046 bool onQuery(Sample::Event* evt) override {
47 if (Sample::TitleQ(*evt)) {
48 Sample::TitleR(evt, "DegenerateTwoPtRadials");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000049 return true;
50 }
51 return this->INHERITED::onQuery(evt);
52 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000055 SkScalar delta = fTime / 15.f;
reed@google.come1ca7052013-12-17 19:22:07 +000056 int intPart = SkScalarFloorToInt(delta);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000057 delta = delta - SK_Scalar1 * intPart;
58 if (intPart % 2) {
59 delta = SK_Scalar1 - delta;
60 }
61 delta -= SK_ScalarHalf;
62 static const int DELTA_SCALE = 500;
63 delta /= DELTA_SCALE;
64
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000065 SkScalar w = SK_Scalar1 * 500;
66 SkScalar h = SK_Scalar1 * 500;
67 SkScalar l = SK_Scalar1 * 100;
68 SkScalar t = SK_Scalar1 * 100;
69 draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta);
bungeman@google.comfab44db2013-10-11 18:50:45 +000070 SkString txt;
71 txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
Hal Canary89a644b2019-01-07 09:36:09 -050072 canvas->drawString(txt, l + w / 2 + w * DELTA_SCALE * delta, t + h + SK_Scalar1 * 10,
73 SkFont(), SkPaint());
reedd9adfe62015-02-01 19:01:04 -080074 }
75
Mike Kleincd5104e2019-03-20 11:55:08 -050076 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080077 fTime = SkDoubleToScalar(timer.secs() / 15);
reedd9adfe62015-02-01 19:01:04 -080078 return true;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000079 }
80
81private:
82 SkScalar fTime;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040083 typedef Sample INHERITED;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000084};
85
86//////////////////////////////////////////////////////////////////////////////
87
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040088DEF_SAMPLE( return new DegenerateTwoPtRadialsView(); )