在本教程中,我们将学习如何使用 FabricJS 创建带有 Circle 对象的画布。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆圈,我们必须创建一个 Fabric.Circle 类的实例并将其添加到画布中。
语法
new fabric.Circle({ radius: Number }: Object)
参数
选项(可选) - 此参数是一个对象 为我们的对象提供额外的定制。使用此参数,可以更改与半径为属性的圆相关的颜色、光标、描边宽度和许多其他属性等属性。
ul>
选项键
示例 1
创建一个实例Fabric.Circle() 并将其添加到我们的画布
让我们看一个如何向画布添加圆形的示例。这里我们创建了一个半径为 50px 的圆。笔划属性表示边框颜色,笔画宽度指定其宽度。我们使用天蓝色填充我们的对象,其十六进制值为#80daeb。
<!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>Creating a canvas with circle using FabricJS</h2> <p>Here we have created a circle of radius 50px over a canvas. In addition, we have used the <b>fill</b> and <b>stroke</b> properties to color its body and outline. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Creating an instance of the fabric.Circle class var circle = new fabric.Circle({ left: 215, top: 100, radius: 50, fill: "#80daeb", stroke: "#00b7eb", strokeWidth: 2, }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
使用 set 方法操作 Circle 对象
在此示例中,我们使用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>Creating a canvas with circle using FabricJS</h2> <p>Here we have used the <b>set</b> method to create a circle of radius 40px and then filled the object with a color. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle(); canvas.add(circle); // Use set to set the properties circle.set("radius", 40); circle.set("fill", "green"); circle.set({ stroke: "rgba(133, 187, 101, 0.7)", strokeWidth: 4 }); circle.set("left", 50); circle.set("top", 50); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
以上就是如何使用 FabricJS 创建带有 Circle 的画布?的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。