在本教程中,我们将学习如何在 fabricjs 中识别 image 实例的类型。我们可以通过创建fabric.image的实例来创建一个image对象。由于它是 fabricjs 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了识别 image 实例的类型,我们使用 istype 方法。
语法istype(type: string): boolean
参数 type - 此参数接受一个string,它指定我们要检查的类型。
使用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=display: none /> <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=display: none /> <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>
以上就是如何使用fabricjs识别image实例的类型?的详细内容。