setup()
setup() runs once and is the perfect place to configure rendering mode, color mode, and build any external UI like dat.GUI before the animation loop begins.
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 (9 lines)
🔧 Subcomponents:
if (typeof DeviceOrientationEvent !== 'undefined' && typeof DeviceOrientationEvent.requestPermission === 'function') {
Checks if the browser supports iOS-style motion sensor permission and requests access if so
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-window canvas using WEBGL mode, which enables 3D-style transforms and better gradient/glow rendering than the default 2D mode
colorMode(HSB, 360, 100, 100, 100);- Switches p5's color system to Hue-Saturation-Brightness with ranges 0-360 for hue and 0-100 for saturation, brightness and alpha - much easier for generating harmonious palettes than RGB
angleMode(RADIANS);- Sets rotation functions like rotateZ() to expect radians instead of degrees, matching p5's TWO_PI constant used later
noFill();- Disables shape fill so only strokes (outlines/points) are visible - important since we're drawing lines and points, not solid shapes
strokeWeight(2);- Sets the default thickness of lines and points drawn afterward to 2 pixels
generatePalette();- Calls the custom function that builds the initial set of harmonious colors used to paint the kaleidoscope
gui = new dat.GUI();- Creates a dat.GUI control panel instance that will appear in the corner of the screen
gui.add(settings, 'symmetryCount', [6, 8, 12]).name('Symmetry Axes');- Adds a dropdown control to the GUI that lets the user pick 6, 8, or 12 symmetry axes, directly editing settings.symmetryCount
DeviceOrientationEvent.requestPermission()- On iOS devices, motion sensor access requires explicit permission - this asks the browser for that permission