為什么沒現(xiàn)象
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>Title</title>
<script>
? ?function draw(id) {
? ? ? ?var ball={x:513,y:100,r:20,g:2,vx:-4,vy:0,color:"red"};
? ? ? ?var canvas=document.getElementById(id);
? ? ? ?if(canvas==null){
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?var context=canvas.getContext("2d");
? ? ? ?setInterval(
? ? ? ? ? ? ? function () {
? ? ? ? ? ? ? ? ? ?render(context);
? ? ? ? ? ? ? ? ? ?update();
? ? ? ? ? ? ? }
? ? ? ? ? ? ? ,
? ? ? ? ? ? ? ?50
)
? ?}
? ?function update() {
? ? ? ?ball.x+=ball.vx;
? ? ? ?ball.y+=ball.vy;
? ? ? ?ball.vy+=ball.g;
? ?}
? ?function render(cxt) {
? ? ? ?context.clearRect(0,0,1024,768);
? ? ? ?cxt.fillStyle=ball.color;
? ? ? ?cxt.beginPath();
? ? ? ?cxt.arc(ball.x,ball.y,ball.r,0,2*Math.PI);
? ? ? ?cxt.closePath();
? ? ? ?cxt.fill();
? ?}
</script>
</head>
<body onload="draw('canvas')">
? ? <canvas id="canvas" width="1024" height="768">
? ? </canvas>
</body>
</html>
2016-07-07
可以的 我問題已經(jīng)解決了
2016-06-03
定義ball應(yīng)該在function前面 ,他是個全局變量
2016-06-03
這個不影響
2016-06-02
你試試window.onload