canvas绘制左右来回移动,触碰边缘返回的小黑方块~

完整代码如下:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
		canvas{
			background:#f2f2f2;
		}
	</style>
  <script></script>
 </head>
 <body>
  <canvas width="500" height="400" id="canvas"></canvas>
	<script>
		var ctx=canvas.getContext('2d');
		var x=0;
		var y=0;
		var s=10;
		setInterval(function(){
					ctx.clearRect(0,0,500,400);
					ctx.fillRect(x,y,50,40);
					x+=s;
					if(x>=450){s=-10}
					else if(x<=0){s=10}
		},100);
	</script>
 </body>
</html>

上一篇:canvas刮刮卡


下一篇:文件读写习题(2)