html5 canvas 可调速的大风车转啊转

效果:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="buttons.css">
    <title></title>
    <style>
	   
		#myCanvas{
			margin: 0 auto;
			border: black 1px solid;
			text-align: center;
		}
		#my-bottom{
			
		}

    </style>
</head>
<body>
    <canvas id="myCanvas" width="400px" height="400px">
    </canvas>

    <div id="my-bottom">
    	 <button id="start-btn" οnclick="fnStart(this)"><i class="fa fa-thumbs-up"></i>暂停</button>
    	<input id="range" type="range" min="0" max="100" value="5" οnchange="change()">
    	<span id="value">5</span>
    </div>
</body>
</html>
<script>

    var ctx = document.getElementById("myCanvas").getContext("2d");
    var runTime = 0;
    var speed = document.getElementById('range').value;
		ctx.font="60px Georgia";
   	var gradient=ctx.createLinearGradient(0,0,document.getElementById("myCanvas").width,0);
    //var gradient=ctx.createLinearGradient(0,0,ctx.width,0);
		gradient.addColorStop("0","magenta");
		gradient.addColorStop("0.5","blue");
		gradient.addColorStop("1.0","red");

    function run(){

    	//console.log(speed);
        var x;
        var y;
        //console.log("x:"+x+" y:"+y);
        ctx.clearRect(0,0,400,400);
        
		// Fill with gradient
		ctx.strokeStyle=gradient;
		ctx.strokeText("大风车转啊转",10,350);

		ctx.beginPath();


		// 用渐变进行填充
		ctx.strokeStyle="black";
        ctx.moveTo(100,100);
        ctx.lineTo(100,400);
        ctx.lineWidth=5;
        ctx.stroke();

        runTime+=speed*0.01;

        ctx.beginPath();
      	ctx.fillStyle="red";
		ctx.strokeStyle="red";
        x=50*Math.sin(runTime);
        y=50*Math.cos(runTime);
        ctx.arc(100+x,100-y,50,(Math.PI/180)*90+runTime,(Math.PI/180)*270+runTime,false);
        ctx.stroke();
        ctx.closePath();
        ctx.fill();

        ctx.beginPath();
      	ctx.fillStyle="green";
      	ctx.strokeStyle="green";
        x=50*Math.sin(runTime+0.5*Math.PI);
        y=50*Math.cos(runTime+0.5*Math.PI);
        ctx.arc(100+x,100-y,50,(Math.PI/180)*180+runTime,(Math.PI/180)*360+runTime,false);
         ctx.stroke();
        ctx.closePath();
        ctx.fill();

        ctx.beginPath();
        ctx.fillStyle="blue";
        ctx.strokeStyle="blue";
        x=50*Math.sin(runTime+Math.PI);
        y=50*Math.cos(runTime+Math.PI);
        ctx.arc(100+x,100-y,50,(Math.PI/180)*270+runTime,(Math.PI/180)*90+runTime,false);
        ctx.stroke();
        ctx.closePath();
        ctx.fill();

        ctx.beginPath();
        ctx.fillStyle="yellow";
        ctx.strokeStyle="yellow";
        x=50*Math.sin(runTime+1.5*Math.PI);
        y=50*Math.cos(runTime+1.5*Math.PI);
        ctx.arc(100+x,100-y,50,(Math.PI/180)*0+runTime,(Math.PI/180)*180+runTime,false);
        ctx.stroke();
        ctx.closePath();
        ctx.fill();
    }
    
var timer = setInterval(run,100);
var flag = 1;
function fnStart(myThis){
	if(flag == 1){
		clearInterval(timer);
		flag = 0;
		myThis.innerHTML="开始";
	}else{
		clearInterval(timer);
		timer = setInterval(run,100);
		flag = 1;
		myThis.innerHTML="暂停";
	}
   		
}

function change() {
  var value = document.getElementById('range').value;
  document.getElementById('value').innerHTML = value;
  speed = value;
}
</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值