blob: b9044c1ee9e803c50d03af85890f9d51b552f102 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000010#include "SkCanvas.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000011#include "SkGradientShader.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000012#include "SkString.h"
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000013
14static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
15 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000016 SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000017
18 SkScalar l = rect.fLeft;
19 SkScalar t = rect.fTop;
20 SkScalar w = rect.width();
21 SkScalar h = rect.height();
22
23 SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5));
24
25 SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 };
26 SkPoint c1 = { l + 3 * w / 5, t + h / 2 };
27 SkScalar r0 = w / 5;
28 SkScalar r1 = 2 * w / 5;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000029 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -080030 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
31 pos, SK_ARRAY_COUNT(pos),
32 SkShader::kClamp_TileMode));
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000033 canvas->drawRect(rect, paint);
34}
35
36
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class DegenerateTwoPtRadialsView : public Sample {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000038public:
39 DegenerateTwoPtRadialsView() {
40 fTime = 0;
41 this->setBGColor(0xFFDDDDDD);
42 }
43
44protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040045 bool onQuery(Sample::Event* evt) override {
46 if (Sample::TitleQ(*evt)) {
47 Sample::TitleR(evt, "DegenerateTwoPtRadials");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000048 return true;
49 }
50 return this->INHERITED::onQuery(evt);
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000054 SkScalar delta = fTime / 15.f;
reed@google.come1ca7052013-12-17 19:22:07 +000055 int intPart = SkScalarFloorToInt(delta);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000056 delta = delta - SK_Scalar1 * intPart;
57 if (intPart % 2) {
58 delta = SK_Scalar1 - delta;
59 }
60 delta -= SK_ScalarHalf;
61 static const int DELTA_SCALE = 500;
62 delta /= DELTA_SCALE;
63
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000064 SkScalar w = SK_Scalar1 * 500;
65 SkScalar h = SK_Scalar1 * 500;
66 SkScalar l = SK_Scalar1 * 100;
67 SkScalar t = SK_Scalar1 * 100;
68 draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta);
bungeman@google.comfab44db2013-10-11 18:50:45 +000069 SkString txt;
70 txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000071 SkPaint paint;
72 paint.setAntiAlias(true);
73 paint.setColor(SK_ColorBLACK);
Cary Clark2a475ea2017-04-28 15:35:12 -040074 canvas->drawString(txt, l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint);
reedd9adfe62015-02-01 19:01:04 -080075 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080078 fTime = SkDoubleToScalar(timer.secs() / 15);
reedd9adfe62015-02-01 19:01:04 -080079 return true;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000080 }
81
82private:
83 SkScalar fTime;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040084 typedef Sample INHERITED;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000085};
86
87//////////////////////////////////////////////////////////////////////////////
88
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040089DEF_SAMPLE( return new DegenerateTwoPtRadialsView(); )