为什么我不能在创建 canvas 后立即设置 context.fillStyle?

Why can I not set context.fillStyle immediately after I create my canvas?

我正在尝试按照 perfection kills 上的绘图 canvas 教程进行操作 我有以下代码:

let c = document.createElement('canvas')
document.body.appendChild(c)
let ctx = c.getContext("2d")

...(set canvas size)

ctx.fillStyle = 'red';
console.log(ctx)

并且控制台显示 fillStyle 在 firefox 和 electron 中仍然设置为 #000000 shell。 google chrome 完全符合我的预期。

如果我稍后在用于绘图的 mousemove 事件处理程序中设置 fillStyle 或在检查器控制台中设置值,它会按预期工作。

如果有区别,上面的代码是我绑定到 DOMContentLoaded 事件的 ready 函数顶部的顶部。

所以问题似乎是上下文 cleared/reset canvas 调整大小。