簡體   English   中英

動畫畫布

[英]Animation canvas

我在畫布上有一個矩形(應該出現),我想從畫布的一側移到另一側。 但是,我擁有的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>

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

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
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM