<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body onkeydown="test()"><h1>让一个小球动起来</h1><canvas id="test"width="400px"height="300px"style="background-color:black"></canvas><script type="text/javascript">var canvas1=document.getElementById("test"); var cxt=canvas1.getContext("2d");var ballX=30;var ballY=30;drawBall();function drawBall(){cxt.fillStyle="#FF0000";cxt.arc(30,30,10,0,Math.PI*2,true);cxt.fill();}function test (){var code=event.keyCode;switch(code){case 87:ballY--;break;case 68:ballX++;break;case 83:ballY++;break;case 65:ballX--;break;} drawBall();}</script></body onkeydown="test()"></html>