1 /* 2 * Copyright 2016 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 "include/core/SkCanvas.h" 9 #include "include/core/SkPath.h" 10 #include "include/utils/SkRandom.h" 11 #include "samplecode/Sample.h" 12 13 class MegaStrokeView : public Sample { 14 public: MegaStrokeView()15 MegaStrokeView() { 16 fClip.setLTRB(0, 0, 950, 600); 17 fAngle = 0; 18 fPlusMinus = 0; 19 SkRandom rand; 20 fMegaPath.reset(); 21 for (int index = 0; index < 921; ++index) { 22 for (int segs = 0; segs < 40; ++segs) { 23 fMegaPath.lineTo(SkIntToScalar(index), SkIntToScalar(rand.nextRangeU(500, 600))); 24 } 25 } 26 } 27 28 protected: name()29 SkString name() override { return SkString("MegaStroke"); } 30 onChar(SkUnichar uni)31 bool onChar(SkUnichar uni) override { 32 fClip.setLTRB(0, 0, 950, 600); 33 return true; 34 } 35 onDrawBackground(SkCanvas * canvas)36 void onDrawBackground(SkCanvas* canvas) override { 37 } 38 onDrawContent(SkCanvas * canvas)39 void onDrawContent(SkCanvas* canvas) override { 40 SkPaint paint; 41 paint.setAntiAlias(true); 42 paint.setARGB(255,255,153,0); 43 paint.setStyle(SkPaint::kStroke_Style); 44 paint.setStrokeWidth(1); 45 46 canvas->save(); 47 canvas->clipRect(fClip); 48 canvas->clear(SK_ColorWHITE); 49 canvas->drawPath(fMegaPath, paint); 50 canvas->restore(); 51 52 SkPaint divSimPaint; 53 divSimPaint.setColor(SK_ColorBLUE); 54 SkScalar x = SkScalarSin(fAngle * SK_ScalarPI / 180) * 200 + 250; 55 SkScalar y = SkScalarCos(fAngle * SK_ScalarPI / 180) * 200 + 250; 56 57 if ((fPlusMinus ^= 1)) { 58 fAngle += 5; 59 } else { 60 fAngle -= 5; 61 } 62 SkRect divSim = SkRect::MakeXYWH(x, y, 100, 100); 63 divSim.outset(30, 30); 64 canvas->drawRect(divSim, divSimPaint); 65 fClip = divSim; 66 } 67 onSizeChange()68 void onSizeChange() override { 69 fClip.setWH(950, 600); 70 } 71 onAnimate(double)72 bool onAnimate(double /*nanos*/) override { return true; } 73 74 private: 75 SkPath fMegaPath; 76 SkRect fClip; 77 int fAngle; 78 int fPlusMinus; 79 typedef Sample INHERITED; 80 }; 81 82 ////////////////////////////////////////////////////////////////////////////// 83 84 DEF_SAMPLE( return new MegaStrokeView(); ) 85