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


Fabric.js toArray()用法及代碼示例


Fabric.js中的toArray()方法用於將指定的array-like對象轉換為數組。這可用於將參數列表或NodeList轉換為數組。

用法:

toArray( arrayLike )

參數:此方法接受如上所述和以下描述的單個參數:

  • arrayLike:此參數保存指定的array-like個對象。

返回值:此方法返回轉換後的數組。

範例1:



HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <!-- Adding the FabricJS library -->
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
    </script> 
</head> 
  
<body> 
    <script type="text/javascript"> 
      
        // Calling toArray() function over 
        // some specified array-like objects 
        console.log(fabric.util.toArray(1)); 
        console.log(fabric.util.toArray(1, 2)); 
        console.log(fabric.util.toArray([1, 2, 3, 4])); 
        console.log(fabric.util.toArray("a")); 
        console.log(fabric.util.toArray("a", "b")); 
        console.log(fabric.util.toArray(["a", "b", "c"])); 
    </script> 
</body> 
  
</html>

輸出:

[]
[]
[1,2,3,4]
["a"]
["a"]
["a","b","c"]

範例2:

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <!-- Adding the FabricJS library -->
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
    </script> 
</head> 
  
<body> 
    <script type="text/javascript"> 
      
        // Specifing some array-like objects 
        var number1 = 0; 
        var number2 = 123; 
        var number3 = (1, 2); 
        var number4 = [1, 2, 3, 4]; 
  
        // Calling toArray() function over 
        // the above specified array-like objects 
        console.log(fabric.util.toArray(number1)); 
        console.log(fabric.util.toArray(number2)); 
        console.log(fabric.util.toArray(number3)); 
        console.log(fabric.util.toArray(number4)); 
    </script> 
</body> 
  
</html>

輸出:

[]
[]
[]
[1,2,3,4]




相關用法


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