<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>刻度进度条</title>
</head>
<canvas id="dialCanvas" width="1000px" height="1000px">
<span>您的浏览器不支持canvsa,请您更换浏览器试试</span>
</canvas>
<body>
<script>
const percent = 360
let count = 120
const mycanvas = document.getElementById('dialCanvas');
const ctx = mycanvas.getContext('2d');
console.log('mycanvas.width, mycanvas.height',mycanvas.width, mycanvas.height)
//画进度条函数
function process(ctx,percent) {
//画刻度盘
for(let i = 120 ; i < 420 ; i++ ) {
if(i%4===0) {
ctx.beginPath();
ctx.arc(500, 500, 100, Math.PI/180*i, Math.PI/180*(i+1), false); // 绘制
ctx.strokeStyle = '#EDEDED';
ctx.lineWidth = 30;
ctx.stroke();
}
}
//画进度条
for(let i = 120 ; i <=percent ; i++ ) {
if(i%4===0 && i < percent ) {
ctx.beginPath();
ctx.arc(500, 500, 100, Math.PI/180*i, Math.PI/180*(i+1), false); // 绘制
ctx.strokeStyle = ' #FF82AB';
ctx.lineWidth = 30;
ctx.stroke();
} else if ( i === percent ) {
ctx.beginPath();
ctx.arc(500, 500, 100, Math.PI/180*i, Math.PI/180*(i+1), false); // 绘制
ctx.strokeStyle = ' #FF82AB';
ctx.lineWidth = 50;
ctx.stroke();
}
}
}
let time = setInterval( ()=>{
if( count <= percent ) {
ctx.clearRect(0, 0,mycanvas.width, mycanvas.height);
process(ctx,count)
} else if ( count > percent ) {
clearInterval(time)
console.log('count',count)
}
count++
},10)
</script>
<style>
</style>
</body>
</html>
该代码示例展示了如何利用HTML5的Canvas元素和JavaScript来创建一个从120度到360度的刻度进度条,并实现动态增加的动画效果。进度条由两种颜色区分,一种是基础背景色,另一种表示进度的颜色,当达到设定的百分比时,进度线宽度会增大,形成视觉焦点。
469

被折叠的 条评论
为什么被折叠?



