www.xbdev.net
xbdev - software development
Saturday April 19, 2025
Home | Contact | Support | JavaScript... so much power in such a few lines of code..
     
 

JavaScript...

so much power in such a few lines of code..

 

Dat.GUI Library (Lightweight, Fast, Free - JavaScript Library)


dat.GUI is a lightweight JavaScript library that provides a simple graphical user interface (GUI) for controlling variables and parameters in web applications. It creates a floating control panel that allows users to interactively adjust values like numbers, colors, booleans, and strings without needing to modify code.

Originally developed for Three.js projects to tweak 3D scene parameters in real-time, it's now widely used in creative coding, data visualization, and prototyping to quickly build debugging interfaces or user controls.

The library works by binding to a JavaScript object's properties and automatically generating appropriate UI controls (sliders, color pickers, checkboxes etc.) based on the property types. When users adjust these controls, the underlying object's values are updated in real-time, which can then trigger visual changes in the application. Its minimalistic design, ease of implementation (requiring just a few lines of code to set up), and flexibility make it particularly popular among developers working on interactive graphics, animations, or simulations where real-time parameter tuning is valuable.


How it Works


To use dat.GUI, you first include the library via a CDN or install it via npm. Then, you create a JavaScript object containing the variables you want to control (e.g., colors, sizes, or toggle switches). Next, you instantiate a dat.GUI object and use its methods (add, addColor, addFolder) to generate interactive controls for your variables. These controls automatically update the object’s properties when adjusted, allowing you to link them to visual changes in your application.


dat.GUI works by binding UI elements (sliders, color pickers, buttons) to properties of a JavaScript object. When a user interacts with a control, the library updates the corresponding property in real time. You can listen for changes using .onChange() to trigger functions that modify your application dynamically. The library also supports nested folders for organizing controls, making it ideal for complex projects where you need to tweak multiple parameters without cluttering the interface. Its simplicity and real-time feedback make it a favorite for debugging, prototyping, and interactive demos.


Working Example



Dat.GUI Example for the code given below.
Dat.GUI Example for the code given below.


The example below using a CDN library link - so it can run in a browser without any installation or work (easy-peasy).

<!DOCTYPE html>
<
html lang="en">
<
head>
    <
meta charset="UTF-8">
    <
meta name="viewport" content="width=device-width, initial-scale=1.0">
    <
title>dat.GUI Example</title>
    <
script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
    <
style>
        
body {
            
margin0;
            
padding20px;
            
font-familyArialsans-serif;
        }
        
#container {
            
width400px;
            
height300px;
            
background-color#eee;
            
margin-bottom20px;
        }
    </
style>
</
head>
<
body>
    <
h1>dat.GUI Example</h1>
    <
div id="container"></div>
    <
p>Adjust the controls to change the box properties.</p>

    <
script>
        
// Our application state object
        
const params = {
            
color'#ff0000',
            
width200,
            
height150,
            
rotation0,
            
visibletrue,
            
message'Hello dat.GUI!',
            
speed0.5,
            
reset: function() {
                
params.color '#ff0000';
                
params.width 200;
                
params.height 150;
                
params.rotation 0;
                
params.visible true;
                
params.message 'Hello dat.GUI!';
                
params.speed 0.5;
                
                
// Update the GUI to reflect changes
                
gui.updateDisplay();
                
updateBox();
            }
        };

        
// Create the GUI
        
const gui = new dat.GUI({ name'My Controls'width400 });

        
// Add controllers for different types of properties
        
gui.addColor(params'color').name('Box Color').onChange(updateBox);
        
gui.add(params'width'50400).name('Width').onChange(updateBox);
        
gui.add(params'height'50300).name('Height').onChange(updateBox);
        
gui.add(params'rotation'0360).name('Rotation').onChange(updateBox);
        
gui.add(params'visible').name('Visible').onChange(updateBox);
        
gui.add(params'message').name('Text Message').onChange(updateBox);
        
gui.add(params'speed'01).name('Animation Speed');
        
        
// Add a button
        
gui.add(params'reset').name('Reset All');
        
        
// Create a folder for advanced options
        
const advanced gui.addFolder('Advanced');
        
advanced.addColor(params'color').name('Alt Color Control');
        
advanced.add(params'width').name('Width (no limits)');
        
advanced.open(); // Open the folder by default

        // Get the container element
        
const container document.getElementById('container');
        
        
// Function to update the box based on parameters
        
function updateBox() {
            
container.innerHTML '';
            
            if (!
params.visible) return;
            
            const 
box document.createElement('div');
            
box.style.width params.width 'px';
            
box.style.height params.height 'px';
            
box.style.backgroundColor params.color;
            
box.style.transform = `rotate(${params.rotation}deg)`;
            
box.style.display 'flex';
            
box.style.alignItems 'center';
            
box.style.justifyContent 'center';
            
box.style.margin '0 auto';
            
box.style.transition 'all 0.3s';
            
            const 
text document.createElement('p');
            
text.textContent params.message;
            
text.style.color '#fff';
            
text.style.textShadow '1px 1px 2px #000';
            
            
box.appendChild(text);
            
container.appendChild(box);
        }
        
        
// Initial render
        
updateBox();
    </
script>
</
body>
</
html>


Resources & Links


Dat GUI Demo (WebBook)

WebBook Lab
















101 WebGPU Programming Projects. WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu wgsl compute graphics all in one kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright Ray-Tracing with WebGPU kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.