我无法让此画布填充矩形

时间:2019-01-20 12:05:37

标签: javascript html canvas

HTML:

<html>
   <head>
       <link rel="stylesheet" href="index.css">
       <title>sdssd</title>
       <meta charset="UTF-8">
   </head>
   <body>
      <canvas id="area">
      </canvas>
   </body>
   <footer>
      <script type="text/javascript" src="index.js"></script>
   </footer>
</html>

CSS:

#area {
    width: 600px;
    height: 600px;
    margin: auto 30%;
    background-color: lightgray;
    outline: 1px solid black;
}

JS:所有这些都很裸露,我只想暂时绘制一个形状,但是context.fillRrect似乎不起作用...我不知道为什么

var canvas = document.getElementsByTagName("canvas")[0];
var context = canvas.getContext("2d");

context.fillStyle("red");
context.fillRect(4, 5, 132, 132);

1 个答案:

答案 0 :(得分:1)

更改

context.fillStyle("red");

context.fillStyle = "red";

使其生效:https://jsfiddle.net/t0yqm6jb/