• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #include "Sample.h"
9 #include "SkAnimTimer.h"
10 #include "SkCanvas.h"
11 #include "SkFont.h"
12 #include "SkGradientShader.h"
13 #include "SkString.h"
14 
draw_gradient2(SkCanvas * canvas,const SkRect & rect,SkScalar delta)15 static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
16     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
17     SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
18 
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;
30     SkPaint paint;
31     paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
32                                                           pos, SK_ARRAY_COUNT(pos),
33                                                           SkShader::kClamp_TileMode));
34     canvas->drawRect(rect, paint);
35 }
36 
37 
38 class DegenerateTwoPtRadialsView : public Sample {
39 public:
DegenerateTwoPtRadialsView()40     DegenerateTwoPtRadialsView() {
41         fTime = 0;
42         this->setBGColor(0xFFDDDDDD);
43     }
44 
45 protected:
onQuery(Sample::Event * evt)46     bool onQuery(Sample::Event* evt) override {
47         if (Sample::TitleQ(*evt)) {
48             Sample::TitleR(evt, "DegenerateTwoPtRadials");
49             return true;
50         }
51         return this->INHERITED::onQuery(evt);
52     }
53 
onDrawContent(SkCanvas * canvas)54     void onDrawContent(SkCanvas* canvas) override {
55         SkScalar delta = fTime / 15.f;
56         int intPart = SkScalarFloorToInt(delta);
57         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 
65         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);
70         SkString txt;
71         txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
72         canvas->drawString(txt, l + w / 2 + w * DELTA_SCALE * delta, t + h + SK_Scalar1 * 10,
73                            SkFont(), SkPaint());
74     }
75 
onAnimate(const SkAnimTimer & timer)76     bool onAnimate(const SkAnimTimer& timer) override {
77         fTime = SkDoubleToScalar(timer.secs() / 15);
78         return true;
79     }
80 
81 private:
82     SkScalar           fTime;
83     typedef Sample INHERITED;
84 };
85 
86 //////////////////////////////////////////////////////////////////////////////
87 
88 DEF_SAMPLE( return new DegenerateTwoPtRadialsView(); )
89