Friday, September 19, 2025
HomeLanguagesJavascriptp5.js torus() Function

p5.js torus() Function

The torus() function in p5.js is used to draw a torus with given torus radius and tube radius.

Syntax:

torus( radius, tubeRadius, detailX, detailY )

Parameters: This function accepts four parameters as mentioned above and described below:

  • radius: This parameter stores the radius of the torus.
  • tubeRadius: This parameter stores the radius of the tube.
  • detailX: This parameter stores the number of segments in x-dimension.
  • detailY: This parameter stores the number of segments in y-dimension.
  • Below programs illustrate the torus() function in p5.js:

    Example 1: This example uses torus() function to draw a torus with given torus radius and tube radius.




    function setup() {
          
        // Create Canvas of size 600*600
        createCanvas(600, 600, WEBGL);
    }
       
    function draw() {
          
        // Set background color
        background(200);
         
        // Set fill color of torus
        fill('green');
         
        // Call to torus function
        torus(90, 35, 12, 12);
    }

    
    

    Output:

    Example 2: This example uses torus() function to draw a torus with given torus radius and tube radius.




    function setup() {
          
        // Create Canvas of size 600*600
        createCanvas(600, 600, WEBGL);
    }
       
    function draw() {
          
        // Set background color
        background(200);
         
        // Set fill color of torus
        fill('yellow');
         
        // Rotate 
        rotateX(frameCount * 0.01);
        rotate(frameCount*0.05);
         
        // Call to torus function
        torus(90, 35);
    }

    
    

    Output:

    Reference: https://p5js.org/reference/#/p5/torus

    RELATED ARTICLES

    Most Popular

    Dominic
    32301 POSTS0 COMMENTS
    Milvus
    84 POSTS0 COMMENTS
    Nango Kala
    6666 POSTS0 COMMENTS
    Nicole Veronica
    11840 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11898 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6781 POSTS0 COMMENTS
    Ted Musemwa
    7056 POSTS0 COMMENTS
    Thapelo Manthata
    6739 POSTS0 COMMENTS
    Umr Jansen
    6745 POSTS0 COMMENTS