如何使用 FabricJS 锁定三角形的水平倾斜?
在本教程中,我们将学习如何使用 FabricJS 锁定三角形的水平倾斜。就像我们可以指定画布中三角形对象的位 置、颜色、不透明度和尺寸一样,我们也可以指定是否要停止对象的水平倾斜。这可以通过使用`lockSkewingX`属性来完成。
语法
new fabric.Triangle({ lockSkewingX : Boolean }: Object)参数
选项(可选) - 此参数是一个对象,它为我们的三角形提供了额外的自定义功能。使用此参数,可以更改与`lockSkewingX`属性相关的对象的许多属性,例如颜色、光标、笔划宽度等。
选项键
lockSkewingX - 此属性接受布尔值。如果我们将其赋值为“true”,则对象的水平倾斜将被锁定。
示例 1
画布中三角形对象的默认行为
让我们来看一个代码示例,了解在不使用`lockSkewingX`属性时三角形对象的默认行为。通过按住**Shift 键**,然后沿水平或垂直方向拖动,可以在水平和垂直方向上倾斜对象。
<!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>Press the shift-key and drag the edge along the X-axis or Y-axis and observe that skewing is possible in both directions.</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
将`lockSkewingX`作为键并赋值为'true'
在这个例子中,我们将看到如何使用`lockSkewingX`属性来阻止三角形对象水平倾斜。正如我们所看到的,虽然我们可以垂直倾斜三角形对象,但我们不允许水平执行相同的操作。
<!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 lockSkewingX as key with 'true' value</h2>
<p>You can try and see that skewing along the X-axis is no longer feasible.</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,
lockSkewingX: true,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP