html convas画一个圆环
时间: 2024-09-29 21:09:47 浏览: 61
在HTML中使用`<canvas>`标签可以画出圆环,以下是一个简单的示例,展示了如何利用JavaScript的Canvas API来实现这个功能:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas 圆环</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="circleCanvas" width="400" height="400"></canvas>
<script>
// 获取canvas元素
const canvas = document.getElementById('circleCanvas');
const ctx = canvas.getContext('2d');
// 定义圆环参数
const outerRadius = 200;
const innerRadius = outerRadius - 50;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// 绘制圆环
ctx.beginPath();
ctx.arc(centerX, centerY, outerRadius, 0, 2 * Math.PI); // 外圆
ctx.clip(); // 开始剪切模式,后续只有在已画区域内的部分会保留
ctx.arc(centerX, centerY, innerRadius, 0, 2 * Math.PI); // 内圆
ctx.restore(); // 恢复到之前的非剪切模式
// 如果想要显示边界,去掉这两行即可
// ctx.strokeStyle = 'black';
// ctx.lineWidth = 2;
// ctx.stroke();
阅读全文
相关推荐
















