如何使用 FabricJS 锁定椭圆的水平移动?
在本教程中,我们将学习如何使用 FabricJS 锁定椭圆的**水平**移动。就像我们可以指定画布中椭圆对象的 位置、颜色、不透明度和尺寸一样,我们还可以指定是否只想让它沿 Y 轴移动。这可以通过使用`lockMovementX`属性来实现。
语法
new fabric.Ellipse({ lockMovementX: Boolean }: Object)参数
options (可选) − 此参数是一个对象,它为我们的椭圆提供了额外的自定义选项。使用此参数可以更改与对象的许多属性相关的颜色、光标、笔划宽度以及其他许多属性,其中`lockMovementX`就是一个属性。
选项键
lockMovementX − 此属性接受一个布尔值。如果我们将其赋值为“true”,则对象将不再能够沿水平方向移动。
示例 1
画布中椭圆对象的默认行为
让我们来看一个例子,了解当`lockMovementX`属性未赋值为“true”时,我们如何自由地在 X 轴上移动椭圆对象。
<!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 horizontal movement of Ellipse using FabricJS?</h2>
<p>You can select the object and move it freely horizontally. Here we haven't used the <b>lockMovementX</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
将`lockMovementX`作为键,值为“true”
在这个例子中,我们将看到如何锁定椭圆对象的水平移动。通过将`lockMovementX`属性赋值为“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 horizontal movement of Ellipse using FabricJS?</h2>
<p>Here you can select the object but can't move it horizontally because we have set the <b>lockMovementX</b> property to True. </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,
lockMovementX: 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