
在本教程中,我们将学习如何使用 FabricJS 创建带有 Circle 对象的画布。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆圈,我们必须创建一个 Fabric.Circle 类的实例并将其添加到画布中。
new fabric.Circle({ radius: Number }: Object)选项(可选) - 此参数是一个对象 为我们的对象提供额外的定制。使用此参数,可以更改与半径为属性的圆相关的颜色、光标、描边宽度和许多其他属性等属性。
半径 - 此属性接受Number 确定圆的半径。如果我们不指定半径,圆将不会显示在画布上。
创建一个实例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>使用 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 的画布?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号