Javascript | HTML Canvas 错误

标签 javascript html html5-canvas

我试图在黑色 Canvas 中“绘制”一个矩形,但它不起作用。这是我的代码:

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>

关于Javascript | HTML Canvas 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46041554/

相关文章:

javascript - Canvas 上大约有 120 000 个粒子?

javascript - 使用 HTML5 和 JavaScript 创建 Sprite 动画

javascript - 如何选择 div 表的第一列?

javascript - 使用 jQuery、AMD 和 require.js 进行事件处理

javascript - 具有良好移动支持的多选 jQuery 插件

javascript - 从 ECMA6 到 ECMA5 的跟踪器转换不起作用

javascript - 使用路由在 EmberJS 中渲染 View

javascript - 用于更改 iFrame 的单选按钮不起作用

javascript - 无法更改 Cordova 3.5 的 index.html 文件

javascript - HTML5 canvas 绘制自定义光标