• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 
5 REG_FIDDLE_ANIMATED(Octopus_Generator_Animated, 256, 256, false, 0, 4) {
6 #include <random>
7 
paintOctopus(float x,float y,int size_base,SkColor color,SkCanvas * canvas)8 void paintOctopus(float x, float y, int size_base, SkColor color, SkCanvas* canvas) {
9     // Set up the paint to draw the head and legs
10     SkPaint paint;
11     paint.setAntiAlias(true);
12     paint.setColor(color);
13     int radius = 3*size_base;
14     // Draw the big head
15     canvas->drawCircle(x, y, radius, paint);
16     // Draw 8 small legs
17     for (int leg = 0; leg < 8; ++leg) {
18       canvas->drawCircle(x - radius + (2*radius/7.5*leg),
19                          y + radius - pow(abs(4-leg), 2), size_base/2 + 2, paint);
20     }
21     // Make the color a bit lighter for the eyes
22     paint.setColor(SkColorSetRGB(std::min(255u, SkColorGetR(color) + 20),
23                                  std::min(255u, SkColorGetG(color) + 20),
24                                  std::min(255u, SkColorGetB(color) + 20)));
25     // Draw the left and right eye
26     canvas->drawCircle(x-size_base, y+size_base, size_base/2, paint);
27     canvas->drawCircle(x+size_base, y+size_base, size_base/2, paint);
28 }
29 
draw(SkCanvas * canvas)30 void draw(SkCanvas* canvas) {
31     // This is called many times, once per frame. To keep the octopods
32     // a consistent color, we depend on the RNG using the *same* seed
33     // for each of calls (i.e. don't reseed it based on time or something).
34     std::default_random_engine rng;
35     const auto randScalar = [&rng](float min, float max) -> float {
36         return std::uniform_real_distribution<float>(min, max)(rng);
37     };
38     const auto randInt = [&rng](int min, int max) -> int {
39         return std::uniform_int_distribution<int>(min, max)(rng);
40     };
41     const auto randOpaqueColor = [&rng]() -> SkColor {
42         return std::uniform_int_distribution<uint32_t>(0, 0xFFFFFF)(rng) | 0xFF000000;
43     };
44 
45     // Don't make this too big or rendering will time out.
46     constexpr int numOctopods = 20;
47     for (int i = 0; i < numOctopods; ++i) {
48       float x = randScalar(0, 256);
49       float y = randScalar(0, 256);
50       int s = randInt(6, 12);
51       SkColor c = randOpaqueColor();
52       // To make the octopods "swim" we have them move around a circle
53       // of a random radius
54       float radius = randScalar(0, 40);
55       // frame is in range [0, 1.0], so this starts at some fraction of 2pi
56       // (e.g. 1/2 pi) and goes until that fraction + 2pi (e.g. 5/2 pi)
57       float angle = (randScalar(0, 1) + frame) * 6.28319;
58       x += radius * cos(angle);
59       y += radius * sin(angle);
60       paintOctopus(x, y, s, c, canvas);
61     }
62 }
63 }  // END FIDDLE
64