JavaScript graphics
If Ajax is
JavaScript's 2nd life,
then maybe graphics is
JavaScript's 3rd life.
Javascript is entering a new future as a graphics language.
3D graphics rendering on client side in Javascript.
Client side rendering of course scales very well.
Server CPU is overloaded.
Client has idle CPU and dedicated GPU.
Example of JavaScript graphics
-
The rotating sphere in my header is JavaScript graphics.
There is no movie.
No animated GIF.
-
There is just a flat JPEG on disk
and some JavaScript code that (at run-time, on the client side)
makes a sphere, paints it with the JPEG, rotates it,
and responds to mouse.
canvas
WebGL
JavaScript libraries
If you are not a graphics specialist,
you will probably want to
use a high-level Javascript library that wraps WebGL
in higher-level objects and methods.
Github
Get three.js
- github.com/mrdoob/three.js
- All previous releases are still on github.
It is best to include a copy of a specific release on your site, rather than link to the latest,
so that your code will always work.
- release 127
- threejs.org has latest release - though it does not note version
- 3D Game Programming for Kids
by Chris Strom
- Coding with Three.js
- Don't be put off by the "For Kids". There is real code here.
- 2nd edn, 2018.
- 1st edn, 2013.
- Can code online in
3DE editor.
Test of Three.js
For all these demos, "View Source" to see the entire thing!
|
Debugging
- Javascript debugging
- Look at objects in the console:
- window
- window.scene.children
- Not ideal: Debug offline
- For offline debugging, we have a problem. We need to read local files (e.g. textures) off disk.
But JavaScript cannot access local files. (Rightly so!)
- Can run Chrome as follows:
"C:\Program Files..\chrome.exe" --allow-file-access-from-files "C:\Users\..\file.html"
- Chrome Command Line Switches
- Better: Debug online
- Better to debug online on your own site.
Then the file reads are Ajax calls to the server, which is fine.
Resources (colors, textures, skyboxes, music, key codes)
Colors
Textures
Cube maps (Skyboxes)
Music
Mouse and keyboard control
More advanced demo