如何使用 FabricJS 在缩放三角形时锁定翻转?
在本教程中,我们将学习如何使用 FabricJS 在缩放三角形时锁定翻转。就像我们可以在画布上指定三角形对象的 position、颜色、不透明度和尺寸一样,我们也可以指定在缩放时是否要停止翻转对象。这可以通过使用 lockScalingFlip 属性来实现。
语法
new fabric.Triangle({ lockScalingFlip : Boolean }: Object)参数
选项(可选) - 此参数是一个对象,它为我们的三角形提供了额外的自定义。使用此参数,可以更改与对象的许多属性相关的属性,例如颜色、光标、笔触宽度等,其中 lockScalingFlip 是一个属性。
选项键
lockMovementY - 此属性接受一个布尔值。如果我们将其分配为“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 Triangle object in the canvas</h2>
<p>Select a corner and drag the triangle 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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 70,
width: 90,
height: 80,
fill: "#746cc0",
stroke: "#967bb6",
strokeWidth: 5,
});
// Add it to the canvas
canvas.add(triangle);
</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 triangle 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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 70,
width: 90,
height: 80,
fill: "#746cc0",
stroke: "#967bb6",
strokeWidth: 5,
lockScalingFlip: true,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP