Javascript getContext(';2D';)返回空值
我创建了一个画布并调用了它的Javascript getContext(';2D';)返回空值,javascript,html,canvas,Javascript,Html,Canvas,我创建了一个画布并调用了它的getContext()方法。但是它返回上下文的null 以下是我使用的代码: <script> window.onload = init; function init(){ var canvas, context; canvas = document.getElementById('canvas'); context = canvas.getContext('2D'); c
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>
window.onload=init;
函数init(){
var,上下文;
canvas=document.getElementById('canvas');
context=canvas.getContext('2D');
console.log(canvas);
console.log(上下文);
}
我在控制台里找到了这个:
<canvas id="canvas" width='500' height='250'>This is a canvas</canvas> null
这是一个画布空值
我想是2d
加上小写的“d”。试着这样做:
context = canvas.getContext('2d');
如果不是这样,那么根据:
(getContext
)如果给定的上下文ID不受支持,或者画布已使用其他上下文类型初始化(例如,在获取“webgl”上下文后尝试获取“2d”上下文),则返回null