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:
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