
在本教程中,我们将学习如何在 FabricJS 中识别 Image 实例的类型。我们可以通过创建fabric.Image的实例来创建一个Image对象。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了识别 Image 实例的类型,我们使用 isType 方法。
语法
isType(type: String): Boolean
参数
type - 此参数接受一个String,它指定我们要检查的类型。
PHP高级开发技巧与范例下载PHP是一种功能强大的网络程序设计语言,而且易学易用,移植性和可扩展性也都非常优秀,本书将为读者详细介绍PHP编程。 全书分为预备篇、开始篇和加速篇三大部分,共9章。预备篇主要介绍一些学习PHP语言的预备知识以及PHP运行平台的架设;开始篇则较为详细地向读者介绍PKP语言的基本语法和常用函数,以及用PHP如何对MySQL数据库进行操作;加速篇则通过对典型实例的介绍来使读者全面掌握PHP。 本书
使用isType方法
示例
让我们看一个代码示例,看看使用 isType 方法时记录的输出。 isType 方法返回 true 或 false 值,具体取决于实例的类型是否与我们要检查的类型匹配。在这种情况下,由于类型匹配,因此返回 true 值。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Using isType method</h2>
<p>
You can open console from dev tools and see that the logged output contains a true value
</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="max-width:90%" / alt="如何使用FabricJS识别Image实例的类型?" >
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
});
// Add it to the canvas
canvas.add(image);
// Using isType method
console.log(
"Is the specified type identical to an image instance? : ",
image.isType("image")
);
</script>
</body>
</html>
使用具有不同值的 isType 方法
示例
在此示例中,我们使用 isType 来检查指定的圆圈类型是否与图像实例相同。此处,由于它们不相同,因此返回错误值。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Using isType method with a different value</h2>
<p>
You can open console from dev tools and see that the logged output contains a false value
</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="max-width:90%" / alt="如何使用FabricJS识别Image实例的类型?" >
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
});
// Add it to the canvas
canvas.add(image);
// Using isType method
console.log(
"Is the specified type identical to an image instance? : ",
image.isType("circle")
);
</script>
</body>
</html>









