2013-05-03 141 views
0

我在畫布上有一個矩形(應該出現),我想從畫布的一側移動到另一側。然而,我擁有atm的代碼並沒有工作,因爲沒有任何東西顯示出來!任何幫助將不勝感激,歡呼!動畫畫布

<!DOCTYPE html> 
<html> 
<head> 
<title>Simple animations in HTML5</title> 

<!--<script> 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.fillRect (20, 50, 200, 100); 
</script> --> 

<script> 
function drawMessage() 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.fillRect(x, y, WIDTH, HEIGHT); 
context.fillStyle = "white"; 
context.font = "30px Arial"; 
context.fillText ("Hello World", MESSAGE_WIDTH, MESSAGE_HEIGHT); 

x += dx; 
y += dy; 

if(x <= 0 || x >= 500) 
{ 
    dx = -dx; 
} 
    if(y <= 0 || y >= 200) 
{ 
dy = -dy 
} 

function animate() 
{ 
    return setInterval(drawMessage, 10); 
} 

</script> 

</head> 
<body> 
<h2> Optical Illusion </h2> 
<video id="illusion" width="640" height="480" controls> 
    <source src="Illusion_movie.ogg"> 
</video> 

<div id="buttonbar"> 
    <button onclick="changeSize()">Big/Small</button> 
</div> 

<p> 
Watch the animation for 1 minute, staring at the centre of the image. Then look at something else near you. 
For a few seconds everything will appear to distort. 
Source: <a href="http://en.wikipedia.org/wiki/File:Illusion_movie.ogg">Wikipedia:Illusion movie</a> 
</p> 

<script type="text/javascript"> 
var myVideo=document.getElementById("illusion"); 
var littleSize = false; 
function changeSize() 
{ 
    myVideo.width = littleSize ? 800 : 400; 
    littleSize = !littleSize;//toggle boolean 
} 
</script> 

<canvas id="myCanvas" width="500" height="500"> 
</canvas> 

<!--<script type="text/javascript"> 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.fillRect(20, 50, 200, 100); 
context.fillStyle = "white"; 
context.font = "30px Arial"; 
context.fillText ("Hello World", 35, 110); 
</script> --> 

<script> 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
var x = 20; // x coordinate of box position 
var y = 50; // y coordinate of box position 
var dx = 2; // amount to move box to the right 
var dy = 4; // amount to move box down 
var WIDTH = 500; // width of canvas 
var HEIGHT = 200; // height of canvas 
var MESSAGE_WIDTH = 200; // width of message 
var MESSAGE_HEIGHT = 100; // height of message 
animate(); // run the animation 
</script> 

</body> 
</html> 

回答

1

在我看來,你的代碼的第一個腳本部分可能缺少花括號。 具體地,部分:

function drawMessage() 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.fillRect(x, y, WIDTH, HEIGHT); 
context.fillStyle = "white"; 
context.font = "30px Arial"; 
context.fillText ("Hello World", MESSAGE_WIDTH, MESSAGE_HEIGHT); 

x += dx; 
y += dy; 

if(x <= 0 || x >= 500) 
{ 
    dx = -dx; 
} 
    if(y <= 0 || y >= 200) 
{ 
dy = -dy 
} 

可能會更好地工作爲:

function drawMessage() 
{ 
var canvas = document.getElementById("myCanvas"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.fillRect(x, y, WIDTH, HEIGHT); 
context.fillStyle = "white"; 
context.font = "30px Arial"; 
context.fillText ("Hello World", MESSAGE_WIDTH, MESSAGE_HEIGHT); 

x += dx; 
y += dy; 

if(x <= 0 || x >= 500) 
{ 
    dx = -dx; 
} 
    if(y <= 0 || y >= 200) 
{ 
dy = -dy 
} 
}