This is the 2nd post in the Use three.js in Jekyll Blog series (« - »), introducing
- fixed aspect ratio;
- three.js on window resize;
- dat.gui.js for control panel.
Try to resize the window and change the parameters!
Fixed aspect ratio
Inspired by this, I found a new method to embed three.js content as well as dat.gui.js panel in Jekyll blog.
Using this method, the HTML side can be simple, readable and customizable. Users can fix the aspect ratio by setting the custom property called aspect-ratio
, which is 16:9
in the following example.
The following CSS snippet does the magic under the hood, and is now in /assets/share/style.css
.
three.js on window resize
When the user resizes the window, the div size can automatically update using the CSS above, but the three.js side also needs to update. This can be accomplished by an event listener as follows.
dat.gui.js for control panel
dat.gui.js can create a control panel and enable users to adjust parameters and immediately see the feedback. This kind of interactive experience is critical in a fancy demo.
Add a div inside the div with fixed aspect ratio.
Use the following CSS to locate the control panel at the top-right corner. The .dg
part is necessary here due to a conflict between dat.gui.js and TeXt theme. This CSS snippet is also in /assets/share/style.css
now.
Create the control panel and define all the parameters. Due to this bug, I have to call close()
to close the control panel after initialization.