用html5标签让小球动起来
<!doctype html>
<html>
<head>
<meta charset='utf-8'/>
</head>
<body onkeydown="changedirect()">
<canvas id='tankmap' width='500px' height='300px' style='background-color:black'>
你的不支持canvas标签</canvas>
</body>
<script>
//开始画出我们的tanke
var canvas = document.getelementbyid('tankmap');
//相当于获得画笔
var ctx = canvas.getcontext('2d');
var myx = 30;
var myy = 30;
//把生成小球的代码封装到一个函数中
function createball(){
ctx.fillstyle = '#ff0000';
ctx.beginpath();
ctx.arc(myx,myy,5,0,math.pi*2,true);
ctx.closepath();
ctx.fill();
}
createball();
function changedirect(){
//触发事件后,传递参数 event
var keycode = event.keycode;
switch(keycode){
case 87:
//alert('up');
myy--;
break;
case 68:
myx++;
break;
case 83:
myy++;
break;
case 65:
myx--;
break;
}
ctx.clearrect(0,0,500,300);
createball();
}
</script>
</html>