當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js fract()用法及代碼示例

p5.j​​s中的fract()函數用於查找數字的小數部分。可以用數學形式表示為{x},其中“ x”是數字。

用法:

fract( num )

參數:該函數接受如上所述和以下描述的單個參數。



  • num:這是要找出其中一部分的數字。

返回值:它返回一個帶有給定數字小數部分的數字。

以下示例說明了p5.js中的fract()函數:

例:

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); 
}

輸出:

fract-slider-example

在線編輯: https://editor.p5js.org/

環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

參考: https://p5js.org/reference/#/p5/fract




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | fract() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。