使用 FabricJS 缩放矩形时如何锁定翻转?
在本教程中,我们将学习如何使用 FabricJS 在缩放矩形时锁定翻转。就像我们可以指定画布中矩形对象的位 置、颜色、不透明度和尺寸一样,我们还可以指定是否要在缩放期间停止翻转对象。这可以通过使用 lockScalingFlip 属性来实现。
语法
new fabric.Rect({ lockScalingFlip : Boolean }: Object)参数
选项(可选)- 此参数是一个对象,它为我们的矩形提供了额外的自定义功能。使用此参数,可以更改与对象的许多属性相关的属性,例如颜色、光标、笔划宽度以及 lockScalingFlip 属性。
选项键
lockScalingFlip - 此属性接受布尔值。如果我们将其赋值为“true”,则不允许对象在缩放期间翻转。
示例 1
画布中矩形对象的默认行为
让我们看一个代码示例,了解在不使用 lockScalingFlip 属性时矩形对象的默认行为。
<!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>Default behaviour of a Rectangle object in the canvas</h2>
<p>Select a corner and drag the rectangle diagonally to scale it down and then flip it with further dragging.</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 a rectangle object
var rect = new fabric.Rect({
left: 155,
top: 90,
width: 170,
height: 70,
fill: "white",
padding: 9,
stroke: "#483d8b",
strokeWidth: 5,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>示例 2
将 lockScalingFlip 作为键传递,值为“true”
在这个例子中,我们将看到如何通过使用 lockScalingFlip 属性来阻止矩形对象在缩放时翻转。正如我们所看到的,即使我们尝试翻转矩形对象,它也不再允许。
<!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>Passing lockScalingFlip as key with a True value</h2>
<p>You will no longer be able to flip the rectangle while scaling it.</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 a rectangle object
var rect = new fabric.Rect({
left: 155,
top: 90,
width: 170,
height: 70,
fill: "white",
padding: 9,
stroke: "#483d8b",
strokeWidth: 5,
lockScalingFlip: true,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP