Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 canvas intellisense in Visual Studio Code

Tags:

is there a way to get intellisense for the HTML5 canvas element? In VS Code 0.7.10 when I write in my JS code this:

context = document.getElementById(canvasId).getContext('2d'); 

then when I write

context.  

I do not have any intellisense help for my context.

Thanks.

like image 784
gregkalapos Avatar asked Aug 25 '15 10:08

gregkalapos


People also ask

How do I get IntelliSense on VS Code?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.)

How do I enable IntelliSense in Visual Studio?

To access this options page, choose Tools > Options, and then choose Text Editor > C# > IntelliSense.

How do I enable autocomplete VS Code in HTML?

If HTML tags autocompleting issue is on JavaScript files, then you just need to change "select language mode" from "JavaScript" to "JavaScript React". Show activity on this post. Press Ctrl + Shift + P to open the command. Then, type Change Language Mode an select HTML or any other desired language.


Video Answer


2 Answers

VS Code can support it!

Just tell VS Code what type is the context. Adding the following code on your variable then the VS Code will know what it is. Sorry that I don't have enough point to post the image. Just click the solution to see how it works.

/** @type {CanvasRenderingContext2D} */

solution

like image 68
Jeremy Su Avatar answered Oct 06 '22 01:10

Jeremy Su


That's currently not supported by VS Code and it is hard to fix. Because JavaScript lacks type annotations, VS Code tries to flow types as good as possible. In your example document.getElementById breaks this flow because from the spec it can return any html element (and we have no further knowledge of the html structure or the value of canvasId).

Something like this, would be more favourable to VS Code:

var canvas = document.createElement('canvas'); canvas.|

Alternative, you could look into using TypeScript because there you use type annotations and type casting.

like image 33
Johannes Rieken Avatar answered Oct 06 '22 00:10

Johannes Rieken