Open In App

p5.js fract() Function

Last Updated : 23 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads