画布没有显示

时间:2013-05-03 00:18:15

标签: javascript canvas

我已经按照有关创建画布的教程,但它不起作用,也没有在其上绘制矩形。是否有必要在<head>中添加脚本?任何帮助将不胜感激!

这是我的代码JSFiddle

<!DOCTYPE html>
<html>
    <head>
        <title>Simple animations in HTML5</title>
    </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>
    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 "> 
    style="border:1px solid #000000;">
</canvas>

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

2 个答案:

答案 0 :(得分:2)

我认为您需要删除“&gt;”从这里也删除额外的空格

<canvas id= "myCanvas " width= "500 " height= "500 "> 
    style="border:1px solid #000000;">
</canvas>

变得如此

<canvas id="myCanvas" width="500" height="500" style="border: 1px solid #000000;">
</canvas>

答案 1 :(得分:2)

我已经清理了你的代码,你有一些空间导致了问题。

<击> 此外,当您使用脚本标记时,请设置type属性:

<script type="text/javascript">
// your code here
</script>

<击>

这是一个更正的小提琴:http://jsfiddle.net/8bK4y/

编辑:如下所述,HTML5 Doctype不需要type属性。