如何使用 FabricJS 设置圆形的最小允许缩放值?
在本教程中,我们将学习如何使用 FabricJS 设置圆形的最小允许缩放比例。圆形是 FabricJS 提供的各种形状之一。为了创建圆形,我们必须创建一个 `fabric.Circle` 类的实例并将其添加到画布上。我们可以通过添加填充颜色、消除边框甚至更改其尺寸来自定义圆形对象。同样,我们也可以使用 `minScaleLimit` 属性设置其最小允许缩放比例。
语法
new fabric.Circle({ minScaleLimit : Number }: Object)参数
**`options` (可选)** − 此参数是一个 `Object`,它为我们的圆形提供了额外的自定义选项。使用此参数可以更改与对象相关的许多属性,例如颜色、光标、边框宽度等等,其中 `minScaleLimit` 就是一个属性。
选项键
**`minScaleLimit`** − 此属性接受一个 **`Number`** 作为值,允许我们控制圆形的最小允许缩放比例。
示例 1
圆形对象的默认外观
让我们来看一段代码,看看在不使用 `minScaleLimit` 属性时圆形对象是什么样的。在这种情况下,我们将能够自由地缩放对象,因为没有设置最小限制。
<!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>Setting the minimum allowed scale value of circle using FabricJS</h2>
<p>Select the object and scale it down by dragging one of its controlling corners. Here you can scale down the object freely since there is no minimum limit set.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 50,
fill: "#ff1493"
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 `minScaleLimit` 属性作为键并设置自定义值
在这个例子中,我们将看到为 `minScaleLimit` 属性赋值如何改变画布中圆形对象的最小允许缩放比例。这里我们使用了 0.8 作为值,这意味着我们将无法将对象缩小到小于 64px 的半径(计算方法为:**半径 * 限制** (0.8 * 80 = 64px))。
<!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>Setting the minimum allowed scale value of circle using FabricJS</h2>
<p>Select the object and try to scale it down by dragging one of its controlling corners. You cannot scale down the object freely, as we have set <b>minScaleLimit</b> at 0.8. So, the minimum scale of the circle must be at least 80% of the original radius, beyond which you cannot scale it down any further. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 80,
fill: "#ff1493",
minScaleLimit: 0.8
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP