Open In App

How to create a Rotating Spiral Animation Effect using p5.js ?

In this article, we are going to see how to create a Rotating Spiral animation effect using p5.js.

p5.js is a JavaScript library that helps in creating graphics that users can interact with and is easy to use and helps in visualizing different kinds of graphics and animations. This article will show you how to create a Rotating Spiral animation using p5.js.



Approach: This code creates an animation of multiple spirals rotating around a common center in P5.js by following these steps:

Finally, the angle is incremented by the speed value to rotate the spirals.



Used Functions:

Example:  In this example we are going to create a Rotating Spiral animation, using p5.js




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
        content="IE=edge">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Animation of Rotating Spiral</title>
    <script src=
    </script>    
</head>
<body>
    <h2 style="color:green;">GeeksforGeeks</h2>
  
    <script>
        let angle = 0;
        let speed = 0.05;
        let numArms = 5;
  
        function setup() {
            createCanvas(250, 250);
            noFill();
        }
  
        function draw() {
            background(100);
            translate(width / 2, height / 2);
            for (let i = 0; i < numArms; i++) {
                rotate(TWO_PI / numArms);
                spiralArm(100);
            }
            angle += speed;
        }
  
        function spiralArm(len) {
            stroke(0);
            strokeWeight(2);
            beginShape();
            for (let i = 0; i < 200; i++) {
                let r = map(i, 0, 200, 0, len);
                let theta = map(i, 0, 200, 0, TWO_PI) + angle;
                let x = r * cos(theta);
                let y = r * sin(theta);
                vertex(x, y);
            }
            endShape();
        }
    </script>
</body>
</html>

Output:

 


Article Tags :