These instructions show how to use Visual Studio Code with the Live Server extension.

The newer, simpler instructions use my P5 Server extension.

A. Configure Visual Studio Code

  1. Install Visual Studio Code. If you do not already have Visual Studio Code installed, download it

  2. Launch Visual Studio Code

  3. Enable Auto Save

    Verify that File > Auto Save is enabled

    Verify that File > Auto Save is enabled

  4. Install Live Server

    ① Open the Extensions panel. ② Search for the “Live Server” extension. ③ Click “Install”.

    ① Open the Extensions panel. ② Search for the “Live Server” extension. ③ Click “Install”.

  5. Configure additional settings. The instructions in the accompanying slide presentation specify how to configure Visual Studio Code to format code whenever you save it, and to disable some of the more annoying autocomplete suggestions.

B. Create a sketch

  1. Create an HTML file. Create a new file (File > New), paste the following text into it, and save it with the name index.html.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>My Sketch</title>
        <style>
          body {
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
          }
        </style>
      </head>
      <body></body>
    
      <!-- import the javascript library file(s) -->
      <script src="<https://cdn.jsdelivr.net/npm/p5@1/lib/p5.min.js>" crossorigin="anonymous"></script>
    	<!-- <script src="<https://cdn.jsdelivr.net/npm/p5@1/lib/addons/p5.sound.min.js>" crossorigin="anonymous"></script> -->
    
      <!-- import the javascript sketch file -->
      <script src="./sketch.js"></script>
    </html>
    
  2. Create a JavaScript file. Create a new file, paste the following text into it, and save it with the name sketch.js.

    function setup() {
      createCanvas(windowWidth, windowHeight);
    }
    
    function draw() {
      background(255);
      circle(mouseX, mouseY, 20);
    }
    

C. Run the sketch in a browser

In Visual Studio Code, click the Go Live button on the lower right corner of the window. (If this button is not present, perhaps you did not install the Live Server extension. Or perhaps you do not have a JavaScript project open in Visual Studio Code.)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d3447211-c261-47d9-8bc8-e9a160d961b4/Untitled.png

This opens the code in Chrome (or your default browser).

D. Configure Chrome

  1. Chrome (or your preferred browser) should open a tab that displays the sketch. The starter sketch above shows a circle that follows the mouse.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5bcbbf60-7309-43b6-971e-e3e87126d280/Untitled.png

  1. Follow these instructions to display the JavaScript Console. In Chrome: Open the JavaScript Console: View > Developer Tools > JavaScript Console. Or, press Command+Option+J (Mac) or Control+Shift+J (Windows).

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0da2545f-a52f-4654-aa44-c54ba267108f/Screenshot_of_Google_Chrome_(2019-11-21_1-57-20_PM).png

E. Arrange your windows

  1. If your screen is big enough, you can configure your desktop so that you can see your source code (in Visual Studio Code) and the drawing (in Chrome) at the same time.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a1ec9fea-414c-4aaf-acac-9ef1a7c50c4d/Untitled.png

  1. In order to make more space for your editor in Visual Studio Code, you can click the top (Explorer) icon to hide the Explorer side bar.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/63187833-673e-4749-8269-8ffb27a1d50f/Untitled.png