|
| 1 | +import { Engine } from "./lib.js"; |
| 2 | + |
| 3 | +import { |
| 4 | + Color, |
| 5 | + GradientPalette, |
| 6 | + Palette, |
| 7 | + PaletteFactory, |
| 8 | + Point, |
| 9 | + SimplexNoise, |
| 10 | + Utils, |
| 11 | + XOR128, |
| 12 | +} from "./lib.js"; |
| 13 | + |
| 14 | +import { Grid } from "./grid.js"; |
| 15 | + |
| 16 | +class Sketch extends Engine { |
| 17 | + preload() { |
| 18 | + this._duration = 900; |
| 19 | + this._recording = false; |
| 20 | + |
| 21 | + this._bg = Color.fromMonochrome(240); |
| 22 | + this._time_scl = 0.5; |
| 23 | + |
| 24 | + this._hex_palettes = [ |
| 25 | + ["#D0EFB1", "#B3D89C", "#9DC3C2", "#77A6B6", "#4D7298"], |
| 26 | + ["#1F363D", "#40798C", "#70A9A1", "#9EC1A3", "#CFE0C3"], |
| 27 | + ["#355070", "#6D597A", "#B56576", "#E56B6F", "#EAAC8B"], |
| 28 | + ["#03588C", "#0396A6", "#04BFAD", "#F28963", "#F25C5C"], |
| 29 | + ["#025259", "#007172", "#F29325", "#D94F04", "#F4E2DE"], |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + setup() { |
| 34 | + this._seed = new Date().getTime(); |
| 35 | + this._xor128 = new XOR128(this._seed); |
| 36 | + |
| 37 | + this._cols = this._xor128.random_int(5, 15); |
| 38 | + this._rows = this._xor128.random_int(5, 15); |
| 39 | + |
| 40 | + this._palette_factory = PaletteFactory.fromHEXArray(this._hex_palettes); |
| 41 | + this._palette = this._palette_factory.getRandomPalette(this._xor128, false); |
| 42 | + if (this._xor128.random_bool()) this._palette.reverse(); |
| 43 | + |
| 44 | + this._grid = new Grid( |
| 45 | + this.width, |
| 46 | + this._cols, |
| 47 | + this._rows, |
| 48 | + this._xor128.random_int(2e32), |
| 49 | + this._palette, |
| 50 | + ); |
| 51 | + |
| 52 | + this._bg = this._palette.getRandomColor(this._xor128); |
| 53 | + document.body.style.backgroundColor = this._bg.hex; |
| 54 | + |
| 55 | + this._frame_offset = this.frame_count; |
| 56 | + if (this._recording) { |
| 57 | + this.startRecording(); |
| 58 | + console.log("%cRecording started", "color:green"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + draw(dt) { |
| 63 | + const delta_frame = this.frame_count - this._frame_offset; |
| 64 | + const t = (delta_frame / this._duration) % 1; |
| 65 | + |
| 66 | + const theta = t * Math.PI * 2; |
| 67 | + const tx = (1 + Math.cos(theta)) * this._time_scl; |
| 68 | + const ty = (1 + Math.sin(theta)) * this._time_scl; |
| 69 | + |
| 70 | + this.ctx.save(); |
| 71 | + this.background(this._bg); |
| 72 | + this._grid.update(tx, ty); |
| 73 | + this._grid.show(this.ctx); |
| 74 | + this.ctx.restore(); |
| 75 | + |
| 76 | + if (t == 0 && delta_frame > 0 && this._recording) { |
| 77 | + this._recording = false; |
| 78 | + this.stopRecording(); |
| 79 | + console.log("%cRecording stopped. Saving...", "color:yellow"); |
| 80 | + this.saveRecording(); |
| 81 | + console.log("%cRecording saved", "color:green"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + click() { |
| 86 | + this.setup(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export { Sketch }; |
0 commit comments