viewer.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <!DOCTYPE html>
  2. <!--
  3. p5.js Interactive Viewer Template
  4. =================================
  5. USE THIS AS THE STARTING POINT for interactive generative art sketches.
  6. FIXED (keep as-is):
  7. ✓ Layout structure (sidebar + canvas)
  8. ✓ Seed navigation (prev/next/random/jump)
  9. ✓ Action buttons (regenerate, reset, download PNG)
  10. ✓ Responsive canvas sizing
  11. ✓ Parameter update + regeneration wiring
  12. VARIABLE (replace for each project):
  13. ✗ The p5.js algorithm (setup/draw/classes)
  14. ✗ The PARAMS object (define what your art needs)
  15. ✗ The parameter controls in the sidebar (sliders, pickers)
  16. ✗ The color palette
  17. ✗ The title and description
  18. For headless export: add noLoop() and window._p5Ready=true in setup().
  19. -->
  20. <html lang="en">
  21. <head>
  22. <meta charset="UTF-8">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>Generative Art Viewer</title>
  25. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.3/p5.min.js"></script>
  26. <style>
  27. * { margin: 0; padding: 0; box-sizing: border-box; }
  28. body {
  29. font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  30. background: #0a0a0f;
  31. color: #c8c8d0;
  32. display: flex;
  33. min-height: 100vh;
  34. overflow: hidden;
  35. }
  36. /* --- Sidebar --- */
  37. .sidebar {
  38. width: 280px;
  39. flex-shrink: 0;
  40. background: #12121a;
  41. border-right: 1px solid #1e1e2a;
  42. padding: 20px;
  43. overflow-y: auto;
  44. display: flex;
  45. flex-direction: column;
  46. gap: 20px;
  47. }
  48. .sidebar h1 {
  49. font-size: 18px;
  50. font-weight: 600;
  51. color: #e8e8f0;
  52. margin-bottom: 4px;
  53. }
  54. .sidebar .subtitle {
  55. font-size: 12px;
  56. color: #666;
  57. margin-bottom: 8px;
  58. }
  59. .section-title {
  60. font-size: 11px;
  61. font-weight: 600;
  62. text-transform: uppercase;
  63. letter-spacing: 1px;
  64. color: #555;
  65. margin-bottom: 8px;
  66. }
  67. /* --- Seed Controls --- */
  68. .seed-display {
  69. font-family: 'SF Mono', 'Fira Code', monospace;
  70. font-size: 24px;
  71. font-weight: 700;
  72. color: #e8e8f0;
  73. text-align: center;
  74. padding: 8px;
  75. background: #1a1a25;
  76. border-radius: 6px;
  77. margin-bottom: 8px;
  78. }
  79. .seed-nav {
  80. display: flex;
  81. gap: 6px;
  82. margin-bottom: 6px;
  83. }
  84. .seed-nav button {
  85. flex: 1;
  86. padding: 6px;
  87. font-size: 12px;
  88. }
  89. .seed-jump {
  90. display: flex;
  91. gap: 6px;
  92. }
  93. .seed-jump input {
  94. flex: 1;
  95. padding: 6px 8px;
  96. background: #1a1a25;
  97. border: 1px solid #2a2a35;
  98. border-radius: 4px;
  99. color: #c8c8d0;
  100. font-size: 12px;
  101. font-family: monospace;
  102. }
  103. .seed-jump button { padding: 6px 12px; font-size: 12px; }
  104. /* --- Parameter Controls --- */
  105. .control-group {
  106. margin-bottom: 12px;
  107. }
  108. .control-group label {
  109. display: flex;
  110. justify-content: space-between;
  111. font-size: 12px;
  112. color: #888;
  113. margin-bottom: 4px;
  114. }
  115. .control-group .value {
  116. color: #aaa;
  117. font-family: monospace;
  118. font-size: 11px;
  119. }
  120. .control-group input[type="range"] {
  121. width: 100%;
  122. height: 4px;
  123. -webkit-appearance: none;
  124. background: #2a2a35;
  125. border-radius: 2px;
  126. outline: none;
  127. }
  128. .control-group input[type="range"]::-webkit-slider-thumb {
  129. -webkit-appearance: none;
  130. width: 14px; height: 14px;
  131. border-radius: 50%;
  132. background: #6a9bcc;
  133. cursor: pointer;
  134. }
  135. .control-group input[type="color"] {
  136. width: 100%;
  137. height: 28px;
  138. border: 1px solid #2a2a35;
  139. border-radius: 4px;
  140. background: #1a1a25;
  141. cursor: pointer;
  142. }
  143. /* --- Buttons --- */
  144. button {
  145. padding: 8px 12px;
  146. background: #1e1e2a;
  147. border: 1px solid #2a2a35;
  148. border-radius: 4px;
  149. color: #c8c8d0;
  150. font-size: 12px;
  151. cursor: pointer;
  152. transition: background 0.15s;
  153. }
  154. button:hover { background: #2a2a3a; }
  155. button.primary { background: #2a4a6a; border-color: #3a5a7a; }
  156. button.primary:hover { background: #3a5a7a; }
  157. .actions { display: flex; flex-direction: column; gap: 6px; }
  158. .actions button { width: 100%; }
  159. /* --- Canvas Area --- */
  160. .canvas-area {
  161. flex: 1;
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. padding: 20px;
  166. background: #08080c;
  167. }
  168. canvas { display: block; }
  169. </style>
  170. </head>
  171. <body>
  172. <!-- === SIDEBAR === -->
  173. <div class="sidebar">
  174. <!-- FIXED: Title (customize text, keep structure) -->
  175. <div>
  176. <h1 id="art-title">Generative Sketch</h1>
  177. <div class="subtitle" id="art-subtitle">p5.js generative art</div>
  178. </div>
  179. <!-- FIXED: Seed Navigation -->
  180. <div>
  181. <div class="section-title">Seed</div>
  182. <div class="seed-display" id="seed-display">42</div>
  183. <div class="seed-nav">
  184. <button onclick="changeSeed(-1)">&#9664; Prev</button>
  185. <button onclick="changeSeed(1)">Next &#9654;</button>
  186. <button onclick="randomizeSeed()">Random</button>
  187. </div>
  188. <div class="seed-jump">
  189. <input type="number" id="seed-input" placeholder="Seed #" min="0">
  190. <button onclick="jumpToSeed()">Go</button>
  191. </div>
  192. </div>
  193. <!-- VARIABLE: Parameters (customize for each project) -->
  194. <div id="params-section">
  195. <div class="section-title">Parameters</div>
  196. <!-- === REPLACE THESE WITH YOUR PARAMETERS === -->
  197. <div class="control-group">
  198. <label>Count <span class="value" id="count-val">500</span></label>
  199. <input type="range" id="count" min="50" max="2000" step="50" value="500"
  200. oninput="updateParam('count', +this.value)">
  201. </div>
  202. <div class="control-group">
  203. <label>Scale <span class="value" id="scale-val">0.005</span></label>
  204. <input type="range" id="scale" min="0.001" max="0.02" step="0.001" value="0.005"
  205. oninput="updateParam('scale', +this.value)">
  206. </div>
  207. <div class="control-group">
  208. <label>Speed <span class="value" id="speed-val">2.0</span></label>
  209. <input type="range" id="speed" min="0.5" max="5" step="0.1" value="2.0"
  210. oninput="updateParam('speed', +this.value)">
  211. </div>
  212. <!-- === END PARAMETER CONTROLS === -->
  213. </div>
  214. <!-- VARIABLE: Colors (optional — include if art needs adjustable palette) -->
  215. <!--
  216. <div>
  217. <div class="section-title">Colors</div>
  218. <div class="control-group">
  219. <label>Background</label>
  220. <input type="color" id="bg-color" value="#0a0a14"
  221. oninput="updateParam('bgColor', this.value)">
  222. </div>
  223. <div class="control-group">
  224. <label>Primary</label>
  225. <input type="color" id="primary-color" value="#6a9bcc"
  226. oninput="updateParam('primaryColor', this.value)">
  227. </div>
  228. </div>
  229. -->
  230. <!-- FIXED: Actions -->
  231. <div class="actions">
  232. <div class="section-title">Actions</div>
  233. <button class="primary" onclick="regenerate()">Regenerate</button>
  234. <button onclick="resetDefaults()">Reset Defaults</button>
  235. <button onclick="downloadPNG()">Download PNG</button>
  236. </div>
  237. </div>
  238. <!-- === CANVAS === -->
  239. <div class="canvas-area" id="canvas-container"></div>
  240. <script>
  241. // ====================================================================
  242. // CONFIGURATION — REPLACE FOR EACH PROJECT
  243. // ====================================================================
  244. const DEFAULTS = {
  245. seed: 42,
  246. count: 500,
  247. scale: 0.005,
  248. speed: 2.0,
  249. // Add your parameters here
  250. };
  251. let PARAMS = { ...DEFAULTS };
  252. // ====================================================================
  253. // SEED NAVIGATION — FIXED (do not modify)
  254. // ====================================================================
  255. function changeSeed(delta) {
  256. PARAMS.seed = Math.max(0, PARAMS.seed + delta);
  257. document.getElementById('seed-display').textContent = PARAMS.seed;
  258. regenerate();
  259. }
  260. function randomizeSeed() {
  261. PARAMS.seed = Math.floor(Math.random() * 99999);
  262. document.getElementById('seed-display').textContent = PARAMS.seed;
  263. regenerate();
  264. }
  265. function jumpToSeed() {
  266. let v = parseInt(document.getElementById('seed-input').value);
  267. if (!isNaN(v) && v >= 0) {
  268. PARAMS.seed = v;
  269. document.getElementById('seed-display').textContent = PARAMS.seed;
  270. document.getElementById('seed-input').value = '';
  271. regenerate();
  272. }
  273. }
  274. // ====================================================================
  275. // PARAMETER UPDATES — CUSTOMIZE updateParam body as needed
  276. // ====================================================================
  277. function updateParam(name, value) {
  278. PARAMS[name] = value;
  279. let el = document.getElementById(name + '-val');
  280. if (el) el.textContent = typeof value === 'number' && value < 1 ? value.toFixed(3) : value;
  281. regenerate();
  282. }
  283. function resetDefaults() {
  284. PARAMS = { ...DEFAULTS };
  285. // Reset all sliders to default values
  286. for (let [key, val] of Object.entries(DEFAULTS)) {
  287. let el = document.getElementById(key);
  288. if (el) el.value = val;
  289. let valEl = document.getElementById(key + '-val');
  290. if (valEl) valEl.textContent = typeof val === 'number' && val < 1 ? val.toFixed(3) : val;
  291. }
  292. document.getElementById('seed-display').textContent = PARAMS.seed;
  293. regenerate();
  294. }
  295. function regenerate() {
  296. randomSeed(PARAMS.seed);
  297. noiseSeed(PARAMS.seed);
  298. // Clear and redraw
  299. clear();
  300. initializeArt();
  301. redraw();
  302. }
  303. function downloadPNG() {
  304. saveCanvas('generative-art-seed-' + PARAMS.seed, 'png');
  305. }
  306. // ====================================================================
  307. // P5.JS SKETCH — REPLACE ENTIRELY FOR EACH PROJECT
  308. // ====================================================================
  309. // Your state variables
  310. let particles = [];
  311. function initializeArt() {
  312. // Initialize your generative system using PARAMS
  313. // This is called on every regenerate()
  314. particles = [];
  315. for (let i = 0; i < PARAMS.count; i++) {
  316. particles.push({
  317. x: random(width),
  318. y: random(height),
  319. vx: 0, vy: 0
  320. });
  321. }
  322. }
  323. function setup() {
  324. // Size canvas to fit container
  325. let container = document.getElementById('canvas-container');
  326. let size = Math.min(container.clientWidth - 40, container.clientHeight - 40, 1080);
  327. let cnv = createCanvas(size, size);
  328. cnv.parent('canvas-container');
  329. pixelDensity(1);
  330. colorMode(HSB, 360, 100, 100, 100);
  331. randomSeed(PARAMS.seed);
  332. noiseSeed(PARAMS.seed);
  333. initializeArt();
  334. // For interactive/animated sketches: remove noLoop()
  335. // For static generation: keep noLoop()
  336. noLoop();
  337. }
  338. function draw() {
  339. background(0, 0, 5);
  340. // === YOUR ALGORITHM HERE ===
  341. // Use PARAMS.count, PARAMS.scale, PARAMS.speed, etc.
  342. noStroke();
  343. for (let p of particles) {
  344. let n = noise(p.x * PARAMS.scale, p.y * PARAMS.scale);
  345. let hue = (n * 200 + PARAMS.seed * 0.1) % 360;
  346. fill(hue, 70, 80, 60);
  347. circle(p.x, p.y, n * 10 + 2);
  348. }
  349. // === END ALGORITHM ===
  350. }
  351. function windowResized() {
  352. let container = document.getElementById('canvas-container');
  353. let size = Math.min(container.clientWidth - 40, container.clientHeight - 40, 1080);
  354. resizeCanvas(size, size);
  355. regenerate();
  356. }
  357. </script>
  358. </body>
  359. </html>