SampleMegaStroke.cpp 2.2 KB

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