本文介绍如何利用HTML5 Canvas结合JavaScript定时器实现动态加载动画。通过创建静态环绕圆形并使用rotate方法逐步旋转,模拟加载过程,最终在指定时间后隐藏Canvas元素,适用于前端页面加载状态的可视化展示。
Canvas动态加载动画实现
本文通过具体实例展示如何使用Canvas和定时器制作动态加载动画,供读者参考。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500">你的浏览器不支持canvas技术</canvas>
<script>
var c=document.getElementById('canvas');
var ctx= c.getContext('2d');
ctx.translate(c.width/2, c.height/2);
//首先绘制8个静态环绕的圆形
function create() {
for (var i = 1; i < 9; i++) {
if (i==1) {
ctx.beginPath();
ctx.arc(0, -30, 5, 0, 2 * Math.PI);
ctx.fillStyle='#f0f';
ctx.fill();
}else{
ctx.beginPath();
ctx.arc(0, -30, 5, 0, 2 * Math.PI);
ctx.strokeStyle ='#000';
ctx.stroke();
}
ctx.rotate(Math.PI * 45 / 180);
}
}
//定时转动
setInterval(function(){
ctx.clearRect(-c.width/2,-c.height, c.width, c.height);
create();
ctx.rotate(Math.PI*45/180);
},300);
//定时关闭加载
setTimeout(function(){
c.style.display='none';
},12200);
//
</script>
</body>
</html>
以上内容为本文的全部介绍,希望能对读者的学习提供辅助,同时也欢迎持续关注本站以获取更多资源。