如何使用Fabric.js创建一个画布上的椭圆
在本文中,我们将看到如何使用FabricJS创建一个画布上的椭圆。画布指的是可以移动和根据需要拉伸的椭圆。此外,当涉及到初始描边颜色、填充颜色、描边宽度或半径时,可以对椭圆进行自定义。
做法: 为了实现这个目标,我们将使用一个叫做FabricJS的JavaScript库。在使用CDN导入库之后,我们将在body标签中创建一个canvas块,该块将包含我们的椭圆。在此之后,我们将初始化由FabricJS提供的Canvas和Ellipse的实例,并将椭圆渲染到Canvas中,如下例所示。
语法:
fabric.Ellipse({
rx: number,
ry: number,
radius: number,
fill: string,
stroke: string,
strokeWidth: int
});
参数:
- rx: 它指定了水平半径。
- ry: 它指定了垂直半径。
- radius: 它指定了半径。
- fill: 它指定了填充颜色。
- stroke: 它指定了描边颜色。
- strokeWidth: 它指定了描边宽度。
示例:
此示例使用FabricJS创建了一个简单的可编辑画布类型的椭圆如下所示:
<!DOCTYPE html>
<html>
<head>
<title>
How to create a canvas-type
ellipse with JavaScript?
</title>
<!-- Loading the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<canvas id="canvas" width="600" height="200"
style="border:1px solid #000000">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate a Ellipse instance
var ellipse = new fabric.Ellipse({
rx: 80,
ry: 40,
fill: '',
stroke: 'green',
strokeWidth: 3
});
// Render the Ellipse in canvas
canvas.add(ellipse);
</script>
</body>
</html>
输出: