当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Fabric.js add()用法及代码示例


add()方法用于将某些指定的对象添加到新集合中,并返回新创建的集合。

用法:

add(...object)

参数:此方法接受如上所述和以下描述的参数:

  • object:此参数保存要添加到集合中的对象。

返回值:此方法返回新创建的集合。

范例1:



Javascript

<!DOCTYPE html>
 
<html>
 
<head>
   <script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js" >
   </script>
 
   <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js.map">
   </script>
 
   <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.js">
   </script>
</head>
 
<body>
   <script type="text/javascript">
    
       // Calling add() function to add its 
       // parameters into a collection of objects
       console.log(fabric.Collection.add(1, 2, 3));
       console.log(fabric.Collection.add("a", "b", "c"));
       console.log(fabric.Collection.add("gfg"));
   </script>
 
</body>
 
</html>

输出:

{"_objects":[1,2,3]}
{"_objects":[1,2,3,"a","b","c"]}
{"_objects":[1,2,3,"a","b","c","gfg"]}

范例2:

Javascript

<!DOCTYPE html>
 
<html>
 
<head>
   <script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js" >
   </script>
 
   <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js.map">
   </script>
 
   <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.js">
   </script>
</head>
 
<body>
   <script type="text/javascript">
    
       // Specifying some objects
       var obj1 = {name:"GFG"};
       var obj2 = {Department:"Computer Science"};
       console.log(fabric.Collection.add(obj1, obj2));
   </script>
 
</body>
 
</html>

输出:

{"_objects":[{"name":"GFG"},{"Department":"Computer Science"}]}

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Fabric.js add() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。