在本教程中,我们将学习如何使用 fabricjs 设置椭圆的水平和垂直半径。椭圆形是 fabricjs 提供的各种形状之一。为了创建一个椭圆,我们必须创建一个 fabric.ellipse 类的实例并将其添加到画布中。我们可以通过指定椭圆对象的位置、颜色、不透明度和尺寸来自定义椭圆对象。然而,最重要的属性是rx和ry,它们允许我们指定椭圆的水平和垂直半径。
语法new fabric.ellipse({ rx : number, ry: number }: object)
参数选项(可选)- 此参数是一个对象 为我们的椭圆提供额外的定制。使用此参数,可以更改与 rx 和 ry 属性的对象相关的颜色、光标、描边宽度和许多其他属性。
选项键rx - 此属性接受 数字值。分配的值确定椭圆对象的水平半径。
ry - 该属性接受 数字值。分配的值决定椭圆对象的垂直半径。
示例 1rx时的默认外观/em> 和 ry 未使用
以下代码显示了当 rx 和 时椭圆对象将如何出现ry 属性未被使用。在此示例中,我们可以看到我们使用了填充颜色来发现椭圆,它是不可见的,因为我们没有为其分配水平和垂直半径。
<!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>setting the horizontal and vertical radius of an ellipse using fabricjs</h2> <p>here we are getting a blank output because we have not assigned any horizontal and vertical radius.</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: "red", }); // adding it to the canvas canvas.add(ellipse); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script> </body></html>
示例 2将 rx 和 ry 属性作为键传递
在此示例中,我们分别向 rx 和 ry 属性传递值 100 和 70。因此,我们的椭圆对象的水平半径为 100 像素,垂直半径为 70 像素。
<!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 and vertical radius of ellipse using fabricjs?</h2> <p>here we have supplied the horizontal and vertical radius, <b>rx</b> and <b>ry</b>. hence we are getting an ellipse of a definite dimension. </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: 100, ry: 70, fill: "red", }); // adding it to the canvas canvas.add(ellipse); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script> </body></html>
以上就是如何使用 fabricjs 设置椭圆的水平和垂直半径?的详细内容。