| 1 | // Interactive generative art, using a deterministic formula rather than assets. |
| 2 | module main |
| 3 | |
| 4 | import math |
| 5 | import vx11 |
| 6 | import vx11.examples.demo |
| 7 | |
| 8 | fn poster(mut c demo.Canvas, variant int) ! { |
| 9 | palettes := [['#f2eadb', '#d15b39', '#e4ab63', '#243c46'], |
| 10 | ['#e3e9ed', '#406486', '#e8a087', '#172c48'], ['#e9e7d9', '#74805a', '#d4ad65', '#333b30']] |
| 11 | p := palettes[variant % 3] |
| 12 | c.rect(0, 0, c.width, c.height, p[0])! |
| 13 | margin := 48 |
| 14 | c.text(margin, 48, 'FIELD NOTES V / X11', p[3], false)! |
| 15 | c.line(margin, 68, c.width - margin, 68, p[3])! |
| 16 | c.text(margin, 112, 'HARMONIC FIELDS', p[3], true)! |
| 17 | c.text(margin, 139, 'A study in repetition, interference, and color.', p[3], false)! |
| 18 | artx := margin |
| 19 | arty := 170 |
| 20 | aw := c.width - margin * 2 |
| 21 | ah := c.height - 280 |
| 22 | c.rect(artx, arty, aw, ah, p[3])! |
| 23 | // Concentric ripples are drawn as offset ribbons. All geometry is generated. |
| 24 | for row in 0 .. 72 { |
| 25 | mut lastx := artx |
| 26 | mut lasty := arty |
| 27 | for col in 0 .. 180 { |
| 28 | x := f64(col) / 179 |
| 29 | wave := math.sin(x * 9.4 + f64(variant) * 0.7 + f64(row) * 0.045) |
| 30 | envelope := math.sin(x * math.pi) |
| 31 | y := f64(row) / 71 + wave * envelope * 0.12 |
| 32 | px := artx + int(x * f64(aw)) |
| 33 | py := arty + int(ah / 6) + int(y * f64(ah * 2 / 3)) |
| 34 | color := if row % 9 < 3 { |
| 35 | p[1] |
| 36 | } else if row % 9 < 6 { |
| 37 | p[2] |
| 38 | } else { |
| 39 | p[0] |
| 40 | } |
| 41 | if col > 0 { c.line(lastx, lasty, px, py, color)! } |
| 42 | lastx = px |
| 43 | lasty = py |
| 44 | } |
| 45 | } |
| 46 | c.circle(artx + aw - 98, arty + 28, 42, p[2])! |
| 47 | c.text(margin, c.height - 71, 'No. 0${variant + 1} / THREE COLORWAYS', p[3], false)! |
| 48 | c.text(margin, c.height - 43, 'Space / click Next palette Q / Esc Close', p[3], false)! |
| 49 | c.text(c.width - 180, c.height - 43, '100% XLIB', p[3], false)! |
| 50 | c.present()! |
| 51 | } |
| 52 | |
| 53 | fn main() { |
| 54 | mut display := vx11.connect('')! |
| 55 | defer { display.close() } |
| 56 | mut window := display.new_window( |
| 57 | title: 'Harmonic Fields - generative poster' |
| 58 | width: 760 |
| 59 | height: 820 |
| 60 | events: vx11.exposure_mask | vx11.structure_notify_mask | vx11.key_press_mask | vx11.button_press_mask |
| 61 | )! |
| 62 | defer { window.destroy() } |
| 63 | demo.minimum_size(display, window, 600, 640)! |
| 64 | mut canvas := demo.new_canvas(display, window, 760, 820)! |
| 65 | defer { canvas.close() } |
| 66 | mut variant := 0 |
| 67 | window.show()! |
| 68 | poster(mut canvas, variant)! |
| 69 | if canvas.finish_preview()! { return } |
| 70 | for { |
| 71 | event := display.next_event()! |
| 72 | if window.should_close(event) { break } |
| 73 | if event.kind == vx11.key_press { |
| 74 | key := display.key_text(event)! |
| 75 | if key in ['q', 'Q', '\x1b'] { break } |
| 76 | if key == ' ' { variant = (variant + 1) % 3 } |
| 77 | } |
| 78 | if event.kind == vx11.button_press { variant = (variant + 1) % 3 } |
| 79 | if size := event.configure() { canvas.resize(size.width, size.height)! } |
| 80 | poster(mut canvas, variant)! |
| 81 | } |
| 82 | } |
| 83 | |