如何使用 FabricJS 禁用画布的交互性?
在本文中,我们将学习如何在 FabricJS 中禁用画布的交互性。每个对象之上的交互层是 FabricJS 的独特功能之一。一旦我们初始化了画布,就可以选择对象、拖动它们或操作组选择。但是,所有这一切都可以通过将交互属性指定为 False 来撤消。
语法
new fabric.Canvas(element: HTMLElement|String, { interactive : Boolean }: Object)
参数
-
元素 – 此参数是 元素本身,可以使用 document.getElementById() 或 元素本身的 id 派生。 FabricJS 画布将在此元素上初始化。
-
选项(可选) – 此参数是一个对象,它提供对我们的画布进行额外的定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度等属性,以及许多其他属性,其中 Interactive 是我们可以决定是否需要交互式画布的属性。该属性的默认值为 True。
示例 1
启用交互属性时
启用交互性后,我们可以自由拖动对象、选择它们并根据需要操作它们。我们可以在下面的代码示例中看到 –
<!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>Disabling the interactivity of canvas</h2>
<p>Here you can drag the object and manipulate them freely as we have set the <b>interactive</b> property to True. </p>
<canvas id=canvas></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas(canvas, {
interactive: true,
});
// Creating an instance of the fabric.Rect class
var obj1 = new fabric.Rect({
left: 170,
top: 90,
width: 60,
height: 80,
fill: #966fd6,
angle: 90,
});
var obj2 = new fabric.Rect({
left: 200,
top: 120,
width: 60,
height: 80,
fill: #ffa343,
angle: 56,
});
// Adding it to the canvas
canvas.add(obj1);
canvas.add(obj2);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
示例 2
将交互键传递给类
让我们看一个代码示例,了解如何禁用画布的交互性。我们可以为交互属性分配一个 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>Disabling the interactivity of canvas</h2>
<p>Here you cannot select an area around the objects and manipulate them freely, as we have set the <b>interactive</b> property as False. </b>
</p>
<canvas id=canvas></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas(canvas, {
interactive: false,
});
// Creating an instance of the fabric.Rect class
var obj1 = new fabric.Rect({
left: 170,
top: 90,
width: 60,
height: 80,
fill: #966fd6,
angle: 90,
});
var obj2 = new fabric.Rect({
left: 200,
top: 120,
width: 60,
height: 80,
fill: #ffa343,
angle: 56,
});
// Adding it to the canvas
canvas.add(obj1);
canvas.add(obj2);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
以上就是如何使用 FabricJS 禁用画布的交互性?的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。



