如何使用 FabricJS 设置椭圆的水平缩放因子?
在本教程中,我们将学习如何使用 FabricJS 设置椭圆的水平缩放因子。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们将必须创建一个fabric.Ellipse类的实例并将其添加到画布中。就像我们可以在画布中指定椭圆对象的位置、颜色、不透明度和尺寸一样,我们也可以设置椭圆对象的水平缩放比例。这可以通过使用scaleX属性来完成。
语法
new fabric.Ellipse({ scaleX : Number }: Object)参数
options (可选) − 此参数是一个对象,它为我们的椭圆提供额外的自定义功能。使用此参数,可以更改与对象相关的许多属性,例如颜色、光标、笔触宽度,其中scaleX也是一个属性。
选项键
scaleX − 此属性接受一个数字值。分配的值决定了水平对象缩放因子。其默认值为 1。
示例 1
不使用scaleX时的默认外观
以下代码将显示当不使用scaleX属性时椭圆对象的外观。默认情况下,椭圆对象的水平缩放因子为 1。scaleX决定沿 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 set the horizontal scale factor of Ellipse using FabricJS?</h2>
<p>By default, the horizontal scale factor is 1. Here we haven't used the <b>scaleX</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,
rx: 80,
ry: 50,
fill: "#ff1493",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将scaleX属性作为键传递
在这个例子中,我们将scaleX属性作为键传递,其值为 2。这意味着椭圆对象在水平方向上的缩放因子加倍。
<!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 set the horizontal scale factor of Ellipse using FabricJS?</h2>
<p>Observe that the horizontal scale factor of the ellipse is doubled due to <b>scaleX</b>. </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,
rx: 80,
ry: 50,
fill: "#ff1493",
scaleX: 2,
});
// 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