HomeLanguagesJavascriptp5.js fract() Function

p5.js fract() Function

The fract() function in p5.js is used to find the fractional part of a number. It can be mathematically represented as {x} where ‘x’ is the number.

Syntax:

fract( num )

Parameters: This function accepts single parameter as mentioned above and described below.

  • num: This is a number of which the fractional part is to be found out.

Return Value: It returns a number with the fractional part of the given number.

Below example illustrates the fract() function in p5.js:

Example:




function setup() {
  createCanvas(600, 200);
  textSize(22);
   
  text("Move the slider to observe the effects"+
       " of the fract() function", 20, 30);
   
  sliderElem = createSlider(-5, 5, 0, 0.0001);
  sliderElem.position(20, 50);
}
   
function draw() {
  clear();
  text("Move the slider to observe the"+
       " effects of the fract() function", 20, 30);
   
  sliderVal = sliderElem.value();
   
  // Find the fractional part of the number
  fractionalVal = fract(sliderVal);
   
  text("The slider value is: " + sliderVal, 20, 100);
  text("The fractional value is: " + fractionalVal, 20, 120);
}


Output:

fract-slider-example

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32736 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7116 POSTS0 COMMENTS
Nicole Veronica
12250 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12333 POSTS0 COMMENTS
Shaida Kate Naidoo
7228 POSTS0 COMMENTS
Ted Musemwa
7488 POSTS0 COMMENTS
Thapelo Manthata
7208 POSTS0 COMMENTS
Umr Jansen
7180 POSTS0 COMMENTS