admin管理员组

文章数量:1312656

Is there a problem with VScode intellisense to work on HTML 5 'canvas' element. Intellisense doesn't provide auto pletion support

var canvas = document.getElementById("canvas")
canvas.getC... /*there is no .getContext("2d")*/

Is there a problem with VScode intellisense to work on HTML 5 'canvas' element. Intellisense doesn't provide auto pletion support

var canvas = document.getElementById("canvas")
canvas.getC... /*there is no .getContext("2d")*/

Share Improve this question asked Sep 19, 2020 at 16:19 Ḉớᶑẽr ĦậꞣǐɱḈớᶑẽr Ħậꞣǐɱ 1901 silver badge7 bronze badges 4
  • 1 Intellisense doesn't work well for JavaScript because of its prototype-based inheritance. – FlatAssembler Commented Sep 19, 2020 at 16:22
  • 1 All it can know is that getElementById() will return a reference to an element node. It cannot predict that the element found will be a <canvas> element. – Pointy Commented Sep 19, 2020 at 16:25
  • @Pointy That's no excuse, if a human being can do it, software can do too. – BEIC Commented Sep 26, 2020 at 8:32
  • @BEIC well if you have some ideas on how such a facility could be implemented, allowing a code editor to know what type of element a call to .getElementById() will return, you can create a peting tool to VSCode. – Pointy Commented Sep 26, 2020 at 12:25
Add a ment  | 

2 Answers 2

Reset to default 12

Use JSDoc @type with getting canvas element:

/** @type {HTMLCanvasElement} */
const c = document.getElementById('canvas');

See more: Get IntelliSense in VS Code for HTML Canvas by Nathan Vaughn

You can also do this. Remove the canvas from HTML, and add it dynamically using javascript code.

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

本文标签: VSCode intellisense for javascript not working for Canvas elementStack Overflow