0

我刚刚开始学习如何使用 HTML5 画布进行绘图,我正在尝试制作一个简单的正方形,但我得到的只是一个空白屏幕,我在 chrome 控制台中也没有出现错误

<!Doctype html>
<html>
<head>
<title>Drawing to a canvas</title>

<script type="text/javascript" language="javascript" >
window.onload = draw;
function draw() 
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");

ctx.fillStyle = "rgba(0,200,0,1)";

ctx.fillRect = (36,10,50,50);

}

</script>
</head>
<body>

<canvas id="canvas1" width="400" height="300">
This text is displayed if your browser 
does not support HTML5 Canvas.
</canvas>

</body>

</html>

这看起来很简单,但它对我不起作用!

4

2 回答 2

0

一个错误是 Elon 指出的一个错误,但您也使用了fillRect错误:

ctx.fillRect = (36,10,50,50);

应该:

ctx.fillRect(36,10,50,50);
于 2013-11-13T07:15:42.433 回答
0

请在寻求帮助之前进行调试!

您有语法错误,因为您忘记了{函数名称之后。

function draw() {
    var canvas = document.getElementById("canvas1");
    var ctx = canvas.getContext("2d");

    ctx.fillStyle = "rgba(0,200,0,1)";

    ctx.fillRect(36,10,50,50);
}
于 2013-11-13T07:06:24.880 回答