<!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>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
html, body{
width: 100%;
height: 100%;
}
body{
background-size: 100% 180%;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<canvas id="can" width="500" height="400"></canvas>
<script>
// 1 获取画布
/** @type {HTMLCanvasElement} */
let canvas = document.getElementById('can')
// 2 获取上下文
let cxt = canvas.getContext('2d') // 获取canvas上下文绘制环境 类似绘制图形的工具
const constant = Math.PI/180
// 3 绘制
/*
startArc 开始角度
roateArc 需要旋转的角度
lineWidth 圆环宽度
color 圆环颜色
*/
let fun = function (startArc = 0, roateArc = 0, lineWidth = 6, color = 'white', speed = 1){
let angle = 0 // 每次旋转的角度
let origin = 0 // 每次的起点
let oldangle = 1 // 每次旋转的角度
let oldorigin = 0 // 每次的起点
let switchBlean = true
let a = 1
let time = setInterval(()=>{
if(switchBlean){
canvas.width = canvas.width
cxt.beginPath()
cxt.lineWidth = lineWidth
cxt.strokeStyle = color
cxt.arc(200,200,100,constant * (origin * roateArc + startArc), constant * (angle + startArc))
cxt.stroke()
angle += speed
if(parseInt(angle / roateArc) -1 === origin){
origin++
switchBlean = false
}
}else{
// 改变起始距离 终点只改一次
if(a == 1){
console.log(oldorigin + startArc)
console.log(oldangle * roateArc + startArc)
a++
}
canvas.width = canvas.width
cxt.beginPath()
cxt.lineWidth = lineWidth
cxt.strokeStyle = color
// cxt.globalAlpha = 0
cxt.arc(200,200,100, constant * (oldorigin + startArc) , constant * (oldangle * roateArc + startArc))
cxt.stroke()
oldorigin+=3
if( parseInt(oldorigin / roateArc) === oldangle ){
oldangle++
switchBlean = true
}
}
},20)
}
fun(0, 360, 30, 'pink', 2)
</script>
</body>
</html>
canvas绘制圆环动画
于 2022-03-29 10:09:31 首次发布