Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate interactive generative art using p5.js with seeded randomness, flow fields, and particle systems
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
templates/generator_template.js
1/**2* ═══════════════════════════════════════════════════════════════════════════3* P5.JS GENERATIVE ART - BEST PRACTICES4* ═══════════════════════════════════════════════════════════════════════════5*6* This file shows STRUCTURE and PRINCIPLES for p5.js generative art.7* It does NOT prescribe what art you should create.8*9* Your algorithmic philosophy should guide what you build.10* These are just best practices for how to structure your code.11*12* ═══════════════════════════════════════════════════════════════════════════13*/1415// ============================================================================16// 1. PARAMETER ORGANIZATION17// ============================================================================18// Keep all tunable parameters in one object19// This makes it easy to:20// - Connect to UI controls21// - Reset to defaults22// - Serialize/save configurations2324let params = {25// Define parameters that match YOUR algorithm26// Examples (customize for your art):27// - Counts: how many elements (particles, circles, branches, etc.)28// - Scales: size, speed, spacing29// - Probabilities: likelihood of events30// - Angles: rotation, direction31// - Colors: palette arrays3233seed: 12345,34// define colorPalette as an array -- choose whatever colors you'd like ['#d97757', '#6a9bcc', '#788c5d', '#b0aea5']35// Add YOUR parameters here based on your algorithm36};3738// ============================================================================39// 2. SEEDED RANDOMNESS (Critical for reproducibility)40// ============================================================================41// ALWAYS use seeded random for Art Blocks-style reproducible output4243function initializeSeed(seed) {44randomSeed(seed);45noiseSeed(seed);46// Now all random() and noise() calls will be deterministic47}4849// ============================================================================50// 3. P5.JS LIFECYCLE51// ============================================================================5253function setup() {54createCanvas(800, 800);5556// Initialize seed first57initializeSeed(params.seed);5859// Set up your generative system60// This is where you initialize:61// - Arrays of objects62// - Grid structures63// - Initial positions64// - Starting states6566// For static art: call noLoop() at the end of setup67// For animated art: let draw() keep running68}6970function draw() {71// Option 1: Static generation (runs once, then stops)72// - Generate everything in setup()73// - Call noLoop() in setup()74// - draw() doesn't do much or can be empty7576// Option 2: Animated generation (continuous)77// - Update your system each frame78// - Common patterns: particle movement, growth, evolution79// - Can optionally call noLoop() after N frames8081// Option 3: User-triggered regeneration82// - Use noLoop() by default83// - Call redraw() when parameters change84}8586// ============================================================================87// 4. CLASS STRUCTURE (When you need objects)88// ============================================================================89// Use classes when your algorithm involves multiple entities90// Examples: particles, agents, cells, nodes, etc.9192class Entity {93constructor() {94// Initialize entity properties95// Use random() here - it will be seeded96}9798update() {99// Update entity state100// This might involve:101// - Physics calculations102// - Behavioral rules103// - Interactions with neighbors104}105106display() {107// Render the entity108// Keep rendering logic separate from update logic109}110}111112// ============================================================================113// 5. PERFORMANCE CONSIDERATIONS114// ============================================================================115116// For large numbers of elements:117// - Pre-calculate what you can118// - Use simple collision detection (spatial hashing if needed)119// - Limit expensive operations (sqrt, trig) when possible120// - Consider using p5 vectors efficiently121122// For smooth animation:123// - Aim for 60fps124// - Profile if things are slow125// - Consider reducing particle counts or simplifying calculations126127// ============================================================================128// 6. UTILITY FUNCTIONS129// ============================================================================130131// Color utilities132function hexToRgb(hex) {133const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);134return result ? {135r: parseInt(result[1], 16),136g: parseInt(result[2], 16),137b: parseInt(result[3], 16)138} : null;139}140141function colorFromPalette(index) {142return params.colorPalette[index % params.colorPalette.length];143}144145// Mapping and easing146function mapRange(value, inMin, inMax, outMin, outMax) {147return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin));148}149150function easeInOutCubic(t) {151return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;152}153154// Constrain to bounds155function wrapAround(value, max) {156if (value < 0) return max;157if (value > max) return 0;158return value;159}160161// ============================================================================162// 7. PARAMETER UPDATES (Connect to UI)163// ============================================================================164165function updateParameter(paramName, value) {166params[paramName] = value;167// Decide if you need to regenerate or just update168// Some params can update in real-time, others need full regeneration169}170171function regenerate() {172// Reinitialize your generative system173// Useful when parameters change significantly174initializeSeed(params.seed);175// Then regenerate your system176}177178// ============================================================================179// 8. COMMON P5.JS PATTERNS180// ============================================================================181182// Drawing with transparency for trails/fading183function fadeBackground(opacity) {184fill(250, 249, 245, opacity); // Anthropic light with alpha185noStroke();186rect(0, 0, width, height);187}188189// Using noise for organic variation190function getNoiseValue(x, y, scale = 0.01) {191return noise(x * scale, y * scale);192}193194// Creating vectors from angles195function vectorFromAngle(angle, magnitude = 1) {196return createVector(cos(angle), sin(angle)).mult(magnitude);197}198199// ============================================================================200// 9. EXPORT FUNCTIONS201// ============================================================================202203function exportImage() {204saveCanvas('generative-art-' + params.seed, 'png');205}206207// ============================================================================208// REMEMBER209// ============================================================================210//211// These are TOOLS and PRINCIPLES, not a recipe.212// Your algorithmic philosophy should guide WHAT you create.213// This structure helps you create it WELL.214//215// Focus on:216// - Clean, readable code217// - Parameterized for exploration218// - Seeded for reproducibility219// - Performant execution220//221// The art itself is entirely up to you!222//223// ============================================================================