Jekyll with Three.js
06 Feb 2017Here is an example of embedding three.js and dat.gui on a Jekyll blog. It is working after a few attempts.
Demo
You can CLICK on the canvas to explode the teapot. Number of teapots and the tessellation level of them can be modifed via the GUI elements on the top right corner.
How?
Essentially, Jekyll is a tool to parse pre-defined styles, scripts and texts, and build static website out of these pieces. Three.js and dat.gui are perfectly static, and parsable for Jekyll. So, why not?
Canvas holder
In order for the canvas to be located on a specific position of the blog, like other blog components, a canvas holder is needed to hold the canvas. Therefore, all elements in the blog are displayed in a correct order. This is a little bit different from other demos of three.js, because in most cases, or examples, the three.js canvas is the whole screen.
Another role of the canvas holder is to place the dat.gui interface as well. We don’t want the default location of dat.gui (right top corner of the whole page).
Dat.gui holder
This div
element is used for holding the dat.gui, in order for manipulate the location of the GUI easier.
The combination of canvas holder and dat.gui holder can be specified like this:
The dat.gui holder has two interesting style definitions:
position:absolute
places the GUI at fixed location relative to the parent.z-index:1
places the GUI on top of the canvas, aka overlay.
You might noticed the additional style definition for canvas. Basically, it forces the actual rendered canvas to be the same size of the canvas holder.
Scripts
The auto placement of dat.gui should be turned off when it is initialized. It is highly inspired by this example.
In addition, the canvas is put inside canvas holder :
Finally
Blogs should be COOL.
Thanks!