gpt4 book ai didi

Javascript | HTML Canvas 错误

转载 作者:行者123 更新时间:2023-11-28 00:53:26 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com