在本文中,我们将学习如何使用 FabricJS 检查指定控件在 Line 中是否可见。Line 元素是 FabricJS 中提供的基本元素之一。它用于创建直线。因为线元素在几何上是一维的并且不包含内部,所以它们永远不会被填充。我们可以通过创建fabric.Line的实例来创建线条对象,指定线条的 x 和 y 坐标并将其添加到画布。为了检查指定控件在 Line 对象中是否可见,我们使用isControlVisible方法。
isControlVisible(controlKey: String): Boolean
controlKey - 此参数接受指定控制键的字符串。可能的值为“tl”、“tr”、“br”、“bl”、“ml”、“mt”、“mr”、“mb”、“mtr”。它们如下所列:
'tl' - 此属性接受启用或禁用左上角控件的布尔值。
'tr' - 此属性接受启用或禁用右上角控件的布尔值。
'br' - 此属性接受启用或禁用右下角控件的布尔值。
'bl' - 此属性接受启用或禁用左下角控件的布尔值。
'ml' - 此属性接受启用或禁用中间左控件的布尔值。
'mt' - 此属性接受启用或禁用中间顶部控件的布尔值。
'mr' - 此属性接受启用或禁用中间右控件的布尔值。
'mb' - 此属性接受启用或禁用中间底部控件的布尔值。
'mtr' - 此属性接受启用或禁用中间顶部旋转控件的布尔值。
让我们看一个代码示例,以查看使用isControlVisible方法时记录的输出。isControlVisible方法根据控制键是否可见返回真值或假值。在这种情况下,由于 bottomleft 控制键可见,因此返回真值。
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script xx_src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Using isControlVisible 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> <script> //启动画布实例 var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); //启动一个 Line 对象 var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); //将其添加到画布 canvas.add(line); //使用 isControlVisible 方法 console.log( "Is the bottom-left control key visible? : ", line.isControlVisible('bl') ); </script> </body> </html>
在这个例子中,我们使用了isControlVisible来检查 middle-top-rotate 控件是否可见。在这里,由于 middle-top-rotate 控制键不可见,因此返回一个假值。
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script xx_src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Using isControlVisible method for an invisible control</h2> <p> You can open console from dev tools and see that the logged output contains a false value </p> <canvas id="canvas"></canvas> <script> //启动画布实例 var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); //启动一个 Line 对象 var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); //将其添加到画布 canvas.add(line); //使“mtr”控件不可见 line.setControlVisible("mtr", false); //使用 isControlVisible 方法 console.log( "Is the middle-top-rotate control key visible? : ", line.isControlVisible("mtr") ); </script> </body> </html>