gpt4 book ai didi

javascript - canvas.getContext ('2D' ) 返回空值

转载 作者:搜寻专家 更新时间:2023-10-31 22:21:17 26 4
gpt4 key购买 nike

我创建了一个 Canvas 并调用了它的 getContext() 方法。但它为上下文返回 null

这是我使用的代码:

<script>
window.onload = init;
function init(){
var canvas, context;
canvas = document.getElementById('canvas');
context = canvas.getContext('2D');
console.log(canvas);
console.log(context);
}
</script>

我在我的控制台中得到了这个:

<canvas id="canvas" width='500' height='250'>This is a canvas</canvas> null

最佳答案

我认为它是带有小写“d”的2d。以这种方式尝试:

context = canvas.getContext('2d');

如果不是那样,则根据 to the specs :

(getContext) Returns null if the given context ID is not supported, if the canvas has already been initialized with the other context type (e.g. trying to get a "2d" context after getting a "webgl" context).

关于javascript - canvas.getContext ('2D' ) 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402577/

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