在本教程中,我们将学习如何使用 fabricjs 创建带有 ellipse 对象的画布。椭圆形是 fabricjs 提供的各种形状之一。为了创建一个椭圆,我们将创建一个 fabric.ellipse 类的实例并将其添加到画布中。
语法new fabric.ellipse({ rx: number, ry: number }: object)
参数选项(可选)- 此参数是一个提供额外自定义的对象到我们的椭圆。使用此参数可以更改与椭圆对象相关的颜色、光标、描边宽度和许多其他属性,其中 rx 和 ry 是椭圆对象的属性。
选项键rx - 此属性接受数字,确定椭圆的水平半径。如果我们不指定水平半径,椭圆将不会显示在画布上。
ry -此属性接受一个数字,它确定椭圆的垂直半径。如果我们不指定垂直半径,椭圆将不会显示在画布上。
示例 1创建实例fabric.ellipse() 并将其添加到我们的画布
让我们看一个如何向画布添加椭圆的示例。这里我们创建了一个水平半径为 80 像素、垂直半径为 50 像素的对象。我们使用天蓝色来填充十六进制值为#87ceeb的对象。
<!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 create a canvas with an ellipse using fabricjs?</h2> <p>here we have created an ellipse object and set it over a canvas.</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: 100, fill: "#87ceeb", rx: 80, ry: 50, }); // adding it to the canvas canvas.add(ellipse); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script> </body></html>
示例 2使用 set 方法操作 ellipse 对象
在此示例中,我们使用set 方法,它是值的设置器。任何与描边、描边宽度、半径、缩放、旋转等相关的属性都可以使用此方法进行改变。
<!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 create a canvas with an ellipse using fabricjs?</h2> <p>here we have used the <b>set</b> method to create an ellipse object over the canvas. </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(); // using set to set the properties ellipse.set("rx", 90); ellipse.set("ry", 40); ellipse.set("fill", "#1e90ff"); ellipse.set({ stroke: "rgba(245,199,246,0.5)", strokewidth: 6 }); ellipse.set("left", 150); ellipse.set("top", 90); // adding it to the canvas canvas.add(ellipse); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script> </body></html>
以上就是如何使用 fabricjs 创建带有椭圆的画布?的详细内容。