Open In App

How to create a rainbow disc using p5.js ?

In this article, we are going to see how we can create a rainbow disc using p5.js. p5.js is a JavaScript library that makes it easy to create interactive graphics, and it is well-suited for visualizations of all kinds, including rainbows. This article will show you how to create a rainbow using p5.js.

Approach:



Prerequisite: These are the list of all the functions used in the example code.

Note that the setup() and draw() functions are also used, but they are built-in functions in p5.js and are called automatically. setup() runs once before the first frame is drawn, while draw() runs continuously, once per frame, after the setup() function.



Example:  In this example we will create a rainbow palette, using the p5.js.




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    <script>
        let colors = [];
          
        function setup() {
          createCanvas(400, 400);
          colorMode(HSB);
          noStroke();
          for (let i = 0; i < 360; i++) {
            colors[i] = color(i, 100, 100);
          }
        }
          
        function draw() {
          translate(width / 2, height / 2);
          for (let i = 0; i < 360; i++) {
            fill(colors[i]);
            let angle = map(i, 0, 360, 0, TWO_PI);
            let x = 200 * cos(angle);
            let y = 200 * sin(angle);
            arc(0, 0, 400, 400, angle, angle + TWO_PI / 360);
          }
        }
    </script>
</head>
  
<body>
    <h1 style="color: Green;">GeeksforGeeks</h1>
</body>
  
</html>

Output:

 


Article Tags :