canvas运动的圆饼


<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#myCanvas{
				border:1px solid red;
				transition:all 2s linear;
			}
		</style>
	</head>
	<body>
		<canvas id="myCanvas" width="600" height="600">您的浏览器不支持canvas</canvas>
		<script>
			var data = [
			        {
			            num: 100,
			            money:200,
			            color: 'red',
			            title:"一月"
			        },
			        {
			            num: 150,
			            money:300,
			            color: 'orange',
			            title:"二月"
			        },
			        {
			            num: 240,
			            money:300,
			            color: 'yellow',
			            title:"三月"
			        },
			        {
			            num: 300,
			            money:400,
			            color: 'green',
			            title:"四月"
			        },
			        {
			            num: 150,
			            money:340,
			            color: 'pink',
			            title:"五月"
			        },
			    ];
			var sun = 0;
			data.forEach(function(item){
				sun+=item.num
			})
			//console.log(150/sun,'sun')
			var canvas = document.querySelector('#myCanvas');
			var ctx = canvas.getContext('2d');
			var start = 0,end = 0;
			data.forEach(function(item,index){
				start = move(start,2*Math.PI*(item.num/sun)+start,item.color)
			})
			function move(start,end,color){
				console.log(end,'哈哈哈哈')
				// 距离
				var step = 0;
				var timer = null;
				timer = setInterval(function(){
					step+=0.01;
					if(start+step*2*Math.PI>=end){
						clearInterval(timer)
					}
					ctx.beginPath();
					ctx.moveTo(200,200);
					ctx.arc(200,200,100,start,start+step*2*Math.PI,false);
					ctx.fillStyle = color;
					ctx.fill();
				},50)
				return end
			}
			
			// 图例
			data.forEach(function(item,index){
				ctx.beginPath();
				ctx.rect(500,100+50*index,30,30)
				ctx.fillStyle = item.color;
				ctx.fill();
				ctx.textBaseline = 'middle;';
				ctx.fillText(item.title,550,115+50*index);
			})
		</script>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值