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:
javascript
Output:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5/fract
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.
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);
}
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5/fract