我试图在黑色 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/