如何使用 FabricJS 锁定椭圆的垂直移动?
在本教程中,我们将学习如何使用 FabricJS 锁定椭圆的垂直移动。就像我们可以指定画布中椭圆对象的 position、颜色、不透明度和尺寸一样,我们也可以指定是否只想沿 X 轴移动它。这可以通过使用 `lockMovementY` 属性来实现。
语法
new fabric.Ellipse({ lockMovementY: Boolean }: Object)参数
options (可选) − 此参数是一个对象,它为我们的椭圆提供了额外的自定义选项。使用此参数可以更改与对象的许多属性相关的颜色、光标、笔划宽度以及其他许多属性,其中 `lockMovementY` 是一个属性。
选项键
lockMovementY − 此属性接受一个布尔值。如果我们将其赋值为“true”,则对象将无法再沿垂直方向移动。
示例 1
画布中椭圆对象的默认行为
让我们来看一个例子,了解当 `lockMovementY` 属性未赋值为“true”时,我们如何自由地沿 Y 轴移动椭圆对象。
<!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> How to lock the vertical movement of Ellipse using FabricJS?</h2>
<p>Here you can select the object and move it freely as we have not applied the <b>lockMovementX</b> or <b>lockMovementY</b> property. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 `lockMovementY` 作为键,值设置为 'true'
在这个例子中,我们将看到如何锁定椭圆对象的垂直移动。通过将 `lockMovementY` 属性的值设置为“true”,我们实际上就停止了垂直方向的移动。
<!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>How to lock the vertical movement of Ellipse using FabricJS?</h2>
<p>Here you can select the object but we have locked it vertical movement by applying the <b>lockMovementY</b> property. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
lockMovementY: true,
});
// Adding it to the canvas
canvas.add(ellipse);
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