generator_template.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * ═══════════════════════════════════════════════════════════════════════════
  3. * P5.JS GENERATIVE ART - BEST PRACTICES
  4. * ═══════════════════════════════════════════════════════════════════════════
  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. */
  14. // ============================================================================
  15. // 1. PARAMETER ORGANIZATION
  16. // ============================================================================
  17. // Keep all tunable parameters in one object
  18. // This makes it easy to:
  19. // - Connect to UI controls
  20. // - Reset to defaults
  21. // - Serialize/save configurations
  22. let params = {
  23. // Define parameters that match YOUR algorithm
  24. // Examples (customize for your art):
  25. // - Counts: how many elements (particles, circles, branches, etc.)
  26. // - Scales: size, speed, spacing
  27. // - Probabilities: likelihood of events
  28. // - Angles: rotation, direction
  29. // - Colors: palette arrays
  30. seed: 12345,
  31. // define colorPalette as an array -- choose whatever colors you'd like ['#d97757', '#6a9bcc', '#788c5d', '#b0aea5']
  32. // Add YOUR parameters here based on your algorithm
  33. };
  34. // ============================================================================
  35. // 2. SEEDED RANDOMNESS (Critical for reproducibility)
  36. // ============================================================================
  37. // ALWAYS use seeded random for Art Blocks-style reproducible output
  38. function initializeSeed(seed) {
  39. randomSeed(seed);
  40. noiseSeed(seed);
  41. // Now all random() and noise() calls will be deterministic
  42. }
  43. // ============================================================================
  44. // 3. P5.JS LIFECYCLE
  45. // ============================================================================
  46. function setup() {
  47. createCanvas(800, 800);
  48. // Initialize seed first
  49. initializeSeed(params.seed);
  50. // Set up your generative system
  51. // This is where you initialize:
  52. // - Arrays of objects
  53. // - Grid structures
  54. // - Initial positions
  55. // - Starting states
  56. // For static art: call noLoop() at the end of setup
  57. // For animated art: let draw() keep running
  58. }
  59. function draw() {
  60. // Option 1: Static generation (runs once, then stops)
  61. // - Generate everything in setup()
  62. // - Call noLoop() in setup()
  63. // - draw() doesn't do much or can be empty
  64. // Option 2: Animated generation (continuous)
  65. // - Update your system each frame
  66. // - Common patterns: particle movement, growth, evolution
  67. // - Can optionally call noLoop() after N frames
  68. // Option 3: User-triggered regeneration
  69. // - Use noLoop() by default
  70. // - Call redraw() when parameters change
  71. }
  72. // ============================================================================
  73. // 4. CLASS STRUCTURE (When you need objects)
  74. // ============================================================================
  75. // Use classes when your algorithm involves multiple entities
  76. // Examples: particles, agents, cells, nodes, etc.
  77. class Entity {
  78. constructor() {
  79. // Initialize entity properties
  80. // Use random() here - it will be seeded
  81. }
  82. update() {
  83. // Update entity state
  84. // This might involve:
  85. // - Physics calculations
  86. // - Behavioral rules
  87. // - Interactions with neighbors
  88. }
  89. display() {
  90. // Render the entity
  91. // Keep rendering logic separate from update logic
  92. }
  93. }
  94. // ============================================================================
  95. // 5. PERFORMANCE CONSIDERATIONS
  96. // ============================================================================
  97. // For large numbers of elements:
  98. // - Pre-calculate what you can
  99. // - Use simple collision detection (spatial hashing if needed)
  100. // - Limit expensive operations (sqrt, trig) when possible
  101. // - Consider using p5 vectors efficiently
  102. // For smooth animation:
  103. // - Aim for 60fps
  104. // - Profile if things are slow
  105. // - Consider reducing particle counts or simplifying calculations
  106. // ============================================================================
  107. // 6. UTILITY FUNCTIONS
  108. // ============================================================================
  109. // Color utilities
  110. function hexToRgb(hex) {
  111. const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  112. return result ? {
  113. r: parseInt(result[1], 16),
  114. g: parseInt(result[2], 16),
  115. b: parseInt(result[3], 16)
  116. } : null;
  117. }
  118. function colorFromPalette(index) {
  119. return params.colorPalette[index % params.colorPalette.length];
  120. }
  121. // Mapping and easing
  122. function mapRange(value, inMin, inMax, outMin, outMax) {
  123. return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin));
  124. }
  125. function easeInOutCubic(t) {
  126. return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
  127. }
  128. // Constrain to bounds
  129. function wrapAround(value, max) {
  130. if (value < 0) return max;
  131. if (value > max) return 0;
  132. return value;
  133. }
  134. // ============================================================================
  135. // 7. PARAMETER UPDATES (Connect to UI)
  136. // ============================================================================
  137. function updateParameter(paramName, value) {
  138. params[paramName] = value;
  139. // Decide if you need to regenerate or just update
  140. // Some params can update in real-time, others need full regeneration
  141. }
  142. function regenerate() {
  143. // Reinitialize your generative system
  144. // Useful when parameters change significantly
  145. initializeSeed(params.seed);
  146. // Then regenerate your system
  147. }
  148. // ============================================================================
  149. // 8. COMMON P5.JS PATTERNS
  150. // ============================================================================
  151. // Drawing with transparency for trails/fading
  152. function fadeBackground(opacity) {
  153. fill(250, 249, 245, opacity); // Anthropic light with alpha
  154. noStroke();
  155. rect(0, 0, width, height);
  156. }
  157. // Using noise for organic variation
  158. function getNoiseValue(x, y, scale = 0.01) {
  159. return noise(x * scale, y * scale);
  160. }
  161. // Creating vectors from angles
  162. function vectorFromAngle(angle, magnitude = 1) {
  163. return createVector(cos(angle), sin(angle)).mult(magnitude);
  164. }
  165. // ============================================================================
  166. // 9. EXPORT FUNCTIONS
  167. // ============================================================================
  168. function exportImage() {
  169. saveCanvas('generative-art-' + params.seed, 'png');
  170. }
  171. // ============================================================================
  172. // REMEMBER
  173. // ============================================================================
  174. //
  175. // These are TOOLS and PRINCIPLES, not a recipe.
  176. // Your algorithmic philosophy should guide WHAT you create.
  177. // This structure helps you create it WELL.
  178. //
  179. // Focus on:
  180. // - Clean, readable code
  181. // - Parameterized for exploration
  182. // - Seeded for reproducibility
  183. // - Performant execution
  184. //
  185. // The art itself is entirely up to you!
  186. //
  187. // ============================================================================