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

标签 javascript html canvas

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

相关文章:

javascript - 当我使用 JS 时,CSS 动画速度变慢

javascript - 是否有 document.execCommand 的替代品? (或者使用 document.execCommand 是否安全?)

javascript - jQuery:鼠标交互 + 简单动画 [img]

java - 简单的Java Web应用程序: Table Not Loading On Startup

javascript - 更平滑的 Canvas 噪声过渡 - 粒子淡入

javascript - IE 9 中 <canvas> 的 z-index 问题

javascript - 在 Meteor 中重用相同的 block 助手会导致奇怪的上下文问题

javascript - jQuery - 如何在 DOM 底部添加新的 div

html - 如何使两列高度相同且响应迅速

javascript - 如何在一个二维 Canvas 上下文上一次绘制多条线?多用户画板