如何使用 FabricJS 锁定矩形的水平缩放?
在本教程中,我们将学习如何使用 FabricJS 锁定矩形的水平缩放。正如我们可以在画布上指定矩形对象的 位置、颜色、不透明度和尺寸一样,我们还可以指定是否要阻止对象的水平缩放。这可以通过使用 `lockScalingX` 属性来实现。
语法
new fabric.Rect({ lockScalingX : Boolean }: Object)参数
选项(可选) - 此参数是一个对象,它为我们的矩形提供了额外的自定义功能。使用此参数,可以更改与 `lockScalingX` 属性相关的对象的许多属性,例如颜色、光标、描边宽度等等。
选项键
lockScalingX - 此属性接受一个布尔值。如果我们将其赋值为 `true`,则对象的水平缩放将被锁定。
示例 1
画布中矩形对象的默认行为
让我们来看一个代码示例,了解在不使用 `lockScalingX` 属性时矩形对象的默认行为。可以在水平和垂直方向上缩放对象。
<!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>You can scale the rectangle in horizontal and vertical directions to verify that scaling in both directions is possible.</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
将 `lockScalingX` 作为键传递,值为 `true`
在这个例子中,我们将看到如何使用 `lockScalingX` 属性来阻止矩形对象水平缩放。我们可以看到,虽然可以垂直缩放矩形对象,但我们不允许进行水平缩放。
<!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 lockScalingX as key with a True value</h2>
<p>You can see a not-allowed cursor if you try to scale the rectangle in the
horizontal direction.</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,
lockScalingX: 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