setup()
setup() runs once when the sketch starts and is where you configure the canvas size, color space, initial variables, and interactive controls. WEBGL mode is key here—it centers the canvas origin at the middle rather than the top-left, which makes rotation and symmetry math much cleaner.
function setup() {
// Create a canvas in WEBGL mode for better gradient and glow effects
createCanvas(windowWidth, windowHeight, WEBGL);
// Use HSB color mode for easier color harmony generation and cycling
colorMode(HSB, 360, 100, 100, 100);
// Set angle mode to RADIANS for rotate() function
angleMode(RADIANS);
// No fill for strokes
noFill();
// Initial stroke weight
strokeWeight(2);
// Generate an initial color palette
generatePalette();
// Initialize dat.GUI
gui = new dat.GUI();
gui.add(settings, 'symmetryCount', [6, 8, 12]).name('Symmetry Axes');
gui.add(settings, 'rotationSpeed', 0, 0.02).name('Rotation Speed');
gui.add(settings, 'colorCyclingSpeed', 0, 0.1).name('Color Cycle Speed');
gui.add(settings, 'showSymmetryLines').name('Show Symmetry Lines');
gui.add(settings, 'isCrystallized').name('Crystallize Pattern');
gui.add(settings, 'clearCanvas').name('Clear Canvas');
gui.add(settings, 'suggestPalette').name('AI Suggest Palette'); // Manual trigger for AI suggestion
// Request device access for motion sensors (for mobile devices)
if (typeof DeviceOrientationEvent !== 'undefined' && typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
console.log('Device orientation permission granted.');
} else {
console.log('Device orientation permission denied.');
}
})
.catch(console.error);
}
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a full-screen 3D canvas using WEBGL mode, which enables smooth, hardware-accelerated rendering better than 2D mode
colorMode(HSB, 360, 100, 100, 100);
Switches to HSB (Hue-Saturation-Brightness) mode, making it easier to generate harmonious color palettes and cycle colors
gui = new dat.GUI();
Creates an interactive control panel in the corner where users can adjust symmetry, speed, colors, and trigger functions
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-screen WEBGL canvas (not 2D). WEBGL mode provides hardware acceleration and allows for better 3D transformations and coordinate centering at the middle of the canvas rather than the top-left corner.
colorMode(HSB, 360, 100, 100, 100);- Switches from default RGB to HSB color space (Hue 0-360, Saturation 0-100, Brightness 0-100, Alpha 0-100). This is perfect for generating harmonious color schemes and cycling colors smoothly.
angleMode(RADIANS);- Sets all rotation angles to use radians (0 to TWO_PI) instead of degrees (0 to 360), which is standard in mathematics and required for rotate() to work with TWO_PI.
noFill();- Disables fill color for all shapes drawn after this—only strokes (outlines) will be visible, which is what we want for this line-based kaleidoscope.
strokeWeight(2);- Sets the thickness of all strokes to 2 pixels. This can be changed to make lines thicker or thinner.
generatePalette();- Calls the color palette generator function immediately at startup to create the first set of harmonious colors from a random base hue.
gui = new dat.GUI();- Creates a new dat.GUI control panel object in the top-right corner that lets users interactively adjust parameters without editing code.