通过html5画布绘制一个矩形?

时间:2015-10-01 15:29:48

标签: javascript html5 canvas

我是初学者,我坚持在画布中用html 5画一个矩形; 我的HTML代码:

<script src="new 3y.js"></script>
<title>test</title>
</head>
<body>
<section id="main">
    <canvas id="canvas" width="600" height="400">
      test
    </canvas>
</section>
</body

我的js代码:

function doFirst() {
var x = document.getElementbyId('canvas');
canvas = x.getContext("2d");
canvas.strokeRect(10,10,100,200);
}

window.addEventListener("load" , doFirst ,false);

1 个答案:

答案 0 :(得分:2)

问题是您使用的是document.getElementbyId,但b必须为大写。

解决这个问题,您的代码运行良好:

function doFirst() {
  var x = document.getElementById('canvas');
  canvas = x.getContext("2d");
  canvas.strokeRect(10,10,100,200);
}

window.addEventListener("load" , doFirst ,false);
<body>
<section id="main">
    <canvas id="canvas" width="600" height="400">
      test
    </canvas>
</section>
</body>