poptimal/src/index.md
2025-01-10 10:36:25 +11:00

3.3 KiB

toc
false

poptimal

v1.0.2 | by mike lynch | @mikelynch@aus.social | source


import {RADIUS_OPTS, DotMaker} from './components/dots.js';
import {PALETTES, DotControls} from './components/controls.js';
import random from "npm:random";

const CELL = 10;
const MAG = 2;
const WIDTH = 20;
const HEIGHT = WIDTH;

const dm = new DotMaker(WIDTH);

const bg_input = Inputs.color({label: "background", value: d3.color("yellow").formatHex()});
const bg = view(bg_input);

const ctrl1 = new DotControls(d3.color("red").formatHex(), RADIUS_OPTS);
const ctrl2 = new DotControls(d3.color("blue").formatHex(), RADIUS_OPTS);


const fg1 = view(ctrl1.fg);
const m1 = view(ctrl1.m);
const n1 = view(ctrl1.n);
const r1 = view(ctrl1.r);
const f1 = view(ctrl1.f);


const fg2 = view(ctrl2.fg);
const m2 = view(ctrl2.m);
const n2 = view(ctrl2.n);
const r2 = view(ctrl2.r);
const f2 = view(ctrl2.f);

const palette_input = Inputs.select(PALETTES, {label: "palette"});
const palette_fn = view(palette_input);

const randomise_pattern = view(Inputs.button("Random", {label:"grids"}));
const randomise_colours = view(Inputs.button("Random", {label:"colours"}));
const randomise_palette = view(Inputs.button("Random", {label:"palette"}));
const randomise_all = view(Inputs.button("Random", {label:"all"}));





randomise_pattern;
ctrl1.random_grid();
ctrl2.random_grid();

randomise_all;
ctrl1.random_grid();
ctrl2.random_grid();
const rpalette = random.choice(Array.from(PALETTES.keys()));
palette_input.value = PALETTES.get(rpalette);
palette_input.dispatchEvent(new Event("input"));

randomise_palette
const rpalette = random.choice(Array.from(PALETTES.keys()));
palette_input.value = PALETTES.get(rpalette);
palette_input.dispatchEvent(new Event("input"));

randomise_colours;
if( palette_fn ) {
  const palette = palette_fn();
  bg_input.value = palette[0].formatHex();
  bg_input.dispatchEvent(new Event("input"));
  ctrl1.set_colour(palette[1].formatHex());
  ctrl2.set_colour(palette[2].formatHex());
}


const dots1 = dm.dots(1 / m1, n1);
const dots2 = dm.dots(1 / m2, n2);




const svg = d3.create("svg")
  .attr("width", WIDTH * CELL * MAG)
  .attr("height", HEIGHT * CELL * MAG)
  .attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);

const background = svg.append("rect")
  .attr("x", 0)
  .attr("y", 0)
  .attr("width", WIDTH)
  .attr("height", WIDTH)
  .attr("fill", bg);

const dots_g1 = svg.append("g")
  .attr("id", "dots1");

dots_g1.selectAll("circle")
  .data(dots1)
  .join("circle")
  .attr("r", (d) => dm.radius(d, f1, r1))
  .attr("fill", fg1)
  .attr("cx", (d) => d.x)
  .attr("cy", (d) => d.y);

const dots_g2 = svg.append("g")
  .attr("id", "dots2");

dots_g2.selectAll("circle")
  .data(dots2)
  .join("circle")
  .attr("r", (d) => dm.radius(d, f2, r2))
  .attr("fill", fg2)
  .attr("cx", (d) => d.x)
  .attr("cy", (d) => d.y);



display(svg.node());