用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>
补充:web前端 , HTML 5 ,