Creative project in Three.js with 3D paper airplanes
- Create a GitHub repository
- Clone the repository
- Create the project with Node and Vite
- Create a placeholder web page
- Test Vite dev setup
- Setup GitHub Pages
- Add JavaScript
- Add the Three.js dependency
- Add vite.config.js
- Add build steps for GitHub Pages
Go to github.com/new to create a repository for the project.
Give the repository a name in kebab case. It's worth a bit of thought, because the name of the project will be part of the URL for the web page. Don't get frozen; the name of the repository can be changed.
I like to start the project with a readme, .gitignore and the MIT license.
Writing a modest description and some notes in the readme will help developers in the future understand the project. You may be your own "future" developer.
The Node .gitignore is a reasonable choice for a web project. The template is a bit heavy-handed, but it covers build and dependency folders well. There is no reason to push a ton of extra baggage to cloud.
I use the MIT license. It was recommended in a previous workplace. The company required using GitHub, suggested public open-source and recommended the MIT license. I just went with it.
Open GitHub Desktop and clone the repository. That is, make a copy on your computer.
GitHub Desktop makes simple repository management intuitive.
Open the terminal and navigate to the project folder. Initialize the project by running
npm init -y
Install Vite by running
npm install vite
Update package.json. Change the license to MIT. Replace the scripts: test with dev and build.
{
...
"scripts": {
"dev": "vite",
"build": "vite build"
},
...
"license": "MIT",
...
}Create a new index.html file. Tip: in VS Code type an exclamation point (!) and then tab-complete it.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> </body> </html>
The content of the file is less important than checking that Vite is ready to go. You are in the "Hello World" stage of the project.
From the terminal, start the development server: npm run dev. Once the server is started, enter 'o' to open the browser. A new browser tab should open with a blank page.
In index.html, add some text (e.g., Hi.) to the body and save. The text should appear on the web page.
From the repository page on github.com, open the Settings > Pages tab. Select GitHub Actions for the build and deployment source. Choose "Static HTML", click Configure. The YAML file for the GitHub Actions deployment is shown. Click the green Commit changes button and commit to the main branch.
Open the Actions tab. There should be a workflow run for our commit "Create static.yml." Click on the link for the workflow run "Create static.yml". There should be one card, "deploy", for the job in our YAML script.
Click the link on the "deploy" card to open the newly published web page.
NB: Since this was done in on the GitHub site, you will need to pull from GitHub to your local version to synchronize your work.
Add a script.js to the project.
document.body.innerText = 'Scripting.'
Update index.html.
...
Hi.
<script type="module" src="script.js"></script>
</body>
</html>From the terminal install three.js
npm install three
Check that Three.js in available. Replace the code in script.js with the following.
import * as THREE from 'three' document.body.innerText = `THREE.REVISION: ${THREE.REVISION}`
NB: Your GitHub Pages page will not be working as expected. You will need to go through the next two steps
You will need to tell Vite that the base of your site is "./" right here, rather than "/" your base domain. This is necessary to have the CSS and script links point to where your files are deployed.
GitHub also needs to know to build the application. And to install the library dependencies needed to build the application.
To get the GitHub Pages to work correctly the project needs a vite.config.js with base defined as base: './'. This makes the links to assets relative to the site for the repository, rather than your homepage (YOUR_USERNAME.github.io).
Add vite-plugin-restart as a development dependency.
Create a folder src and move your source files: index.html and script.js into the src folder.
npm install -d vite-plugin-restart
import restart from 'vite-plugin-restart' export default { root: 'src/', // Sources files (typically where index.html is) publicDir: '../static/', // Path from "root" to static assets (files that are served as they are) base: './', // links relative to the current directory (needed for repository GitHub Pages) server: { host: true, // Open to local network and display URL open: !('SANDBOX_URL' in process.env || 'CODESANDBOX_HOST' in process.env) // Open if it's not a CodeSandbox }, build: { outDir: '../dist', // Output in the dist/ folder emptyOutDir: true, // Empty the folder first sourcemap: true // Add sourcemap }, plugins: [ restart({ restart: [ '../static/**', ] }) // Restart server on static file change ], }
The last deployment won't have worked. The initial page text will not have been changed to show the THREE.REVISION
Update static.yml to include Install and Build steps. Also change the path to dist.
...
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install
run: npm ci
- name: Build
run: npm run build
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: dist
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4The Install step uses npm ci "clean install".
This command is similar to npm install, except it's meant to be used in automated environments such as test platforms, continuous integration, and deployment -- or any situation where you want to make sure you're doing a clean install of your dependencies.
npm Docs - https://docs.npmjs.com/cli/v10/commands/npm-ci
At this point you have a blank slate. But a slate with good supports:
- a remote git repository (GitHub)
- a build tool (Vite)
- a local web server with live reload (Vite)
- a published web page (GitHub Pages)
- and automatic deployment! (GitHub Actions)
Alot of this content is based on Bruno Simon's free lesson First Three.js Project. The lesson is part of his amazing course Three.js Journey.