我试着在黑色画布上“画”一个矩形,但没用。这是我的代码:

window.onload = function() {
  canvas = document.getElementById('canvas');
  canvasContext = canvas.getContext('2d');

  draw();
}

function draw() {
  canvasContext.fillstyle='black';
  canvasContext.fillRect(0,0,800,600);
  canvasContext.fillstyle='white';
  canvasContext.fillRect(0,0,10,10);
}

<canvas id="canvas" width="800" height="600"></canvas>

为什么不起作用?

最佳答案

在fillstyle中使用小写“s”而不是大写“s”
改变

canvasContext.fillstyle


canvasContext.fillStyle

window.onload = function() {
  canvas = document.getElementById('canvas');
  canvasContext = canvas.getContext('2d');


  draw();

}

function draw() {
  canvasContext.fillStyle='black';
  canvasContext.fillRect(0,0,800,600);
  canvasContext.fillStyle='white';
  canvasContext.fillRect(0,0,100,100);


}

<canvas id="canvas" width="800" height="600"></canvas>

10-06 08:04