您好,欢迎访问一九零五行业门户网

如何使用 FabricJS 检查 IText 对象是否已填充?

在本教程中,我们将学习如何使用 fabricjs 检查 itext 对象是否已填充。 itext 类是在 fabricjs 版本 1.4 中引入的,它扩展了 fabric.text 并用于创建 itext 实例。 itext 实例使我们可以自由选择、剪切、粘贴或添加新文本,而无需额外配置。还有各种支持的按键组合和鼠标/触摸组合使文本具有交互性,而 text 中未提供这些组合。
然而,基于 itext 的 textbox 允许我们调整文本矩形的大小并自动换行。对于 itext 来说情况并非如此,因为高度不会根据换行进行调整。我们可以通过使用各种属性来操作 itext 对象。同样,我们可以使用 hasfill 方法检查 itext 对象是否已填充。
语法hasfill()
示例 1使用透明填充时使用hasfill方法
让我们看一个代码示例,以查看当 hasfill 方法与透明填充一起使用时记录的输出。如果对象具有填充颜色,则 hasfill 方法返回真值。在本例中,我们将填充颜色设置为“透明”。因此,记录的输出将为 false。
<!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 hasfill method when transparent fill is used</h2> <p>you can open console from dev tools and see that the logged output is false</p> <canvas id=canvas></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas(canvas); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); // initiate an itext object var itext = new fabric.itext( add sample text here.lorem ipsum dolor sit amet,{ width: 300, left: 60, top: 70, fill: transparent, fontstyle: bold, backgroundcolor: #f8f4ff, stroke: black, } ); // add it to the canvas canvas.add(itext); // using the hasfill method console.log(the value is: , itext.hasfill()); </script></body></html>
示例 2使用 hasfill 方法
让我们看一个代码示例,以查看使用 hasfill 方法时记录的输出。在这种情况下,由于 itext 对象具有填充颜色,因此将返回真值。
<!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 the hasfill method</h2> <p>you can open console from dev tools and see that the value is being displayed in the console</p> <canvas id=canvas></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas(canvas); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); // initiate an itext object var itext = new fabric.itext( add sample text herelorem ipsum dolor sit amet,{ width: 300, left: 60, top: 70, fill: #b666d2, fontstyle: bold, backgroundcolor: #f8f4ff, stroke: black, } ); // using the hasfill method console.log(the value is: , itext.hasfill()); // add it to the canvas canvas.add(itext); </script></body></html>
以上就是如何使用 fabricjs 检查 itext 对象是否已填充?的详细内容。
其它类似信息

推荐信息