我创建了一个 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/