VScode 智能感知在 HTML 5“canvas”元素上工作是否存在问题? Intellisense 不提供自动完成支持
var canvas = document.getElementById("canvas")
canvas.getC... /*there is no .getContext("2d")*/
使用 JSDoc
@type
获取 canvas
元素:
/** @type {HTMLCanvasElement} */
const c = document.getElementById('canvas');
查看更多:在 VS Code 中获取 HTML Canvas 的 IntelliSense 作者:Nathan Vaughn
你也可以这样做。从 HTML 中删除画布,并使用 javascript 代码动态添加它。
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
document.body.appendChild(canvas);