如何使用 Fabric.js 更改画布圆形的背景颜色?
Fabric.js 的 Circle 类用于通过 fabric.Circle 对象提供圆形形状。Circle 对象用于提供圆形形状,该圆形可移动,并且可以根据需要进行拉伸。对于笔触、颜色、宽度、高度和填充颜色,Circle 是可自定义的。与 canvas 类相比,Circle 类提供了更丰富的功能。
在这里,我们使用 Circle 对象的 backgroundColor 属性来更改画布圆形的背景颜色。我们可以通过为该属性定义值来更改背景颜色。
语法
以下是 Circle 的语法:
fabric.Circle({
radius: number,
backgroundColor: string,
fill: string,
stroke: string,
strokewidth: int
});
参数
radius - 用于指定圆形的半径
fill - 用于指定填充颜色
stroke - 用于指定笔触颜色
strokeWidth - 用于指定笔触的宽度
radius - 用于指定半径
backgroundColor - 指定背景的颜色
示例
在本示例中,我们将初始化圆形和画布的实例,它有助于使用 backgroundColor 属性更改圆形的背景颜色并在画布上渲染圆形。
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"></script>
</head>
<body>
<h2>Changing the background color of a canvas circle</h2>
<p>Select the textbox to see that the controlling corners.</p>
<p>Background color of Canvas Circle is changed to "yellow".</p>
<canvas id="canvas" width="550" height="450" ></canvas>
<script>
var canvas = new fabric.Canvas("canvas");
var round = new fabric.Circle({
top: 50,
left: 50,
radius: 80,
backgroundColor: 'yellow'
});
canvas.add(round);
</script>
</body>
</html>
正如我们在示例中看到的,这里我们使用了 Fabric.js 的 Circle 类,该类用于通过 fabric.Circle 对象提供圆形形状。在这里,我们将画布圆形的背景颜色更改为黄色。
让我们再举一个例子,我们将学习如何更改具有主体和轮廓颜色的画布圆形的背景颜色。
示例
我们将初始化 Canvas 和 Circle 的实例,它有助于使用 backgroundColor 属性更改圆形的背景颜色并在画布上渲染圆形。在 Circle 类对象下,我们定义了诸如 fill、stroke、strokeWidth、left、top、radius 和 backgroundColor 之类的属性,这些属性为圆形主体和轮廓提供颜色。
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"></script>
</head>
<body>
<h2>Changing the background color of a canvas circle</h2>
<p>Select the textbox to see that the controlling corners.</p>
<p>Background color of Canvas Circle is changed to "green".</p>
<canvas id="canvas" width="600" height="300" style="border:1px solid #00008B"></canvas>
<script>
var canvas = new fabric.Canvas("canvas");
var round1 = new fabric.Circle({
radius: 80,
left: 180,
top: 80,
fill: "#70daeb",
stroke: "#00b7eb",
strokeWidth: 2,
backgroundColor: 'green'
});
canvas.add(round1);
</script>
</body>
</html>
结论
在本文中,我们看到了两个不同的示例,在第一个示例中,我们使用了 Circle 类的 backgroundColor 属性将圆形的背景颜色更改为黄色。
在第二个示例中,我们使用了 Circle 类及其属性(如 radius、backgroundColor、fill、top、left、stroke 和 strokeWidth)来更改圆形的背景颜色以及圆形主体和轮廓的颜色。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP