2025-01-05 01:45:30 +00:00
|
|
|
---
|
|
|
|
toc: false
|
|
|
|
---
|
|
|
|
|
2025-01-11 06:18:43 +00:00
|
|
|
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
<h1>poptimal</h1>
|
2025-01-05 01:45:30 +00:00
|
|
|
|
2025-01-11 04:32:18 +00:00
|
|
|
<p>v1.1.0 | by <a href="https://mikelynch.org">mike lynch</a> | <a href="https://aus.social/@mikelynch">@mikelynch@aus.social</a> | <a href="https://git.tilde.town/bombinans/poptimal">source</a></p>
|
2025-01-06 03:28:47 +00:00
|
|
|
|
2025-01-05 07:02:23 +00:00
|
|
|
<div class="grid grid-cols-2">
|
|
|
|
|
|
|
|
<div class="card">
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
```js
|
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
import {RADIUS_OPTS, DotMaker} from './components/dots.js';
|
2025-01-08 03:02:47 +00:00
|
|
|
import {PALETTES, DotControls} from './components/controls.js';
|
2025-01-11 06:43:01 +00:00
|
|
|
import {download, download_as_svg, download_as_png} from './components/download.js';
|
2025-01-08 03:02:47 +00:00
|
|
|
import random from "npm:random";
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-11 06:18:43 +00:00
|
|
|
import * as resvg from 'npm:@resvg/resvg-wasm';
|
|
|
|
|
|
|
|
await resvg.initWasm(fetch('https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm'));
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
const CELL = 10;
|
2025-01-05 23:24:05 +00:00
|
|
|
const MAG = 2;
|
2025-01-05 06:12:51 +00:00
|
|
|
const WIDTH = 20;
|
2025-01-05 23:24:05 +00:00
|
|
|
const HEIGHT = WIDTH;
|
|
|
|
|
|
|
|
const dm = new DotMaker(WIDTH);
|
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
const bg_input = Inputs.color({label: "background", value: d3.color("yellow").formatHex()});
|
|
|
|
const bg = view(bg_input);
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
const ctrl1 = new DotControls(d3.color("red").formatHex(), RADIUS_OPTS);
|
|
|
|
const ctrl2 = new DotControls(d3.color("blue").formatHex(), RADIUS_OPTS);
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-06 03:28:47 +00:00
|
|
|
|
2025-01-08 03:02:47 +00:00
|
|
|
const fg1 = view(ctrl1.fg);
|
2025-01-06 01:28:13 +00:00
|
|
|
const m1 = view(ctrl1.m);
|
|
|
|
const n1 = view(ctrl1.n);
|
|
|
|
const r1 = view(ctrl1.r);
|
|
|
|
const f1 = view(ctrl1.f);
|
|
|
|
|
2025-01-06 03:28:47 +00:00
|
|
|
|
2025-01-08 03:02:47 +00:00
|
|
|
const fg2 = view(ctrl2.fg);
|
2025-01-06 01:28:13 +00:00
|
|
|
const m2 = view(ctrl2.m);
|
|
|
|
const n2 = view(ctrl2.n);
|
|
|
|
const r2 = view(ctrl2.r);
|
|
|
|
const f2 = view(ctrl2.f);
|
|
|
|
|
2025-01-09 00:33:13 +00:00
|
|
|
const palette_input = Inputs.select(PALETTES, {label: "palette"});
|
2025-01-08 03:02:47 +00:00
|
|
|
const palette_fn = view(palette_input);
|
|
|
|
|
2025-01-09 00:33:13 +00:00
|
|
|
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"}));
|
2025-01-08 03:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-05 06:17:52 +00:00
|
|
|
```
|
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
|
2025-01-05 06:17:52 +00:00
|
|
|
```js
|
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
randomise_pattern;
|
|
|
|
ctrl1.random_grid();
|
|
|
|
ctrl2.random_grid();
|
2025-01-05 06:17:52 +00:00
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
```
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
```js
|
|
|
|
randomise_all;
|
|
|
|
ctrl1.random_grid();
|
|
|
|
ctrl2.random_grid();
|
2025-01-08 03:02:47 +00:00
|
|
|
const rpalette = random.choice(Array.from(PALETTES.keys()));
|
|
|
|
palette_input.value = PALETTES.get(rpalette);
|
|
|
|
palette_input.dispatchEvent(new Event("input"));
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
randomise_palette
|
|
|
|
const rpalette = random.choice(Array.from(PALETTES.keys()));
|
|
|
|
palette_input.value = PALETTES.get(rpalette);
|
|
|
|
palette_input.dispatchEvent(new Event("input"));
|
2025-01-06 01:28:13 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2025-01-08 03:02:47 +00:00
|
|
|
```js
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
2025-01-06 01:28:13 +00:00
|
|
|
|
2025-01-05 07:02:23 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
|
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
```js
|
|
|
|
|
2025-01-06 01:28:13 +00:00
|
|
|
const dots1 = dm.dots(1 / m1, n1);
|
|
|
|
const dots2 = dm.dots(1 / m2, n2);
|
2025-01-05 23:24:05 +00:00
|
|
|
|
2025-01-05 07:02:23 +00:00
|
|
|
|
2025-01-05 06:12:51 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
|
|
|
|
|
|
|
const svg = d3.create("svg")
|
2025-01-05 23:24:05 +00:00
|
|
|
.attr("width", WIDTH * CELL * MAG)
|
|
|
|
.attr("height", HEIGHT * CELL * MAG)
|
|
|
|
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
|
2025-01-05 06:12:51 +00:00
|
|
|
|
|
|
|
const background = svg.append("rect")
|
|
|
|
.attr("x", 0)
|
|
|
|
.attr("y", 0)
|
2025-01-05 23:24:05 +00:00
|
|
|
.attr("width", WIDTH)
|
|
|
|
.attr("height", WIDTH)
|
2025-01-05 06:12:51 +00:00
|
|
|
.attr("fill", bg);
|
|
|
|
|
|
|
|
const dots_g1 = svg.append("g")
|
|
|
|
.attr("id", "dots1");
|
|
|
|
|
|
|
|
dots_g1.selectAll("circle")
|
|
|
|
.data(dots1)
|
|
|
|
.join("circle")
|
2025-01-05 23:24:05 +00:00
|
|
|
.attr("r", (d) => dm.radius(d, f1, r1))
|
2025-01-05 06:12:51 +00:00
|
|
|
.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")
|
2025-01-05 23:24:05 +00:00
|
|
|
.attr("r", (d) => dm.radius(d, f2, r2))
|
2025-01-05 06:12:51 +00:00
|
|
|
.attr("fill", fg2)
|
|
|
|
.attr("cx", (d) => d.x)
|
|
|
|
.attr("cy", (d) => d.y);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display(svg.node());
|
|
|
|
|
2025-01-11 04:32:18 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
2025-01-11 06:43:01 +00:00
|
|
|
display(download(() => {
|
|
|
|
const thing = download_as_svg(svg.node())
|
|
|
|
return thing;
|
|
|
|
}, "poptimal.svg", "Save as SVG"));
|
2025-01-05 06:12:51 +00:00
|
|
|
|
2025-01-11 06:43:01 +00:00
|
|
|
display(download(() => {
|
|
|
|
console.log("PNG value");
|
|
|
|
const thing = download_as_png(svg.node())
|
|
|
|
return thing;
|
|
|
|
}, "poptimal.png", "Save as PNG"));
|
2025-01-11 04:32:18 +00:00
|
|
|
|
2025-01-11 06:18:43 +00:00
|
|
|
|
|
|
|
```
|
2025-01-11 06:43:01 +00:00
|
|
|
(PNGs made with <a href="https://github.com/thx/resvg-js">resvg-wasm</a> in-browser)
|
2025-01-11 06:18:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-05 07:02:23 +00:00
|
|
|
</div>
|
2025-01-05 01:45:30 +00:00
|
|
|
|
|
|
|
<style>
|
2025-01-06 03:28:47 +00:00
|
|
|
h1 {
|
|
|
|
font-family: helvetica, arial, sans-serif;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
|
|
|
|
p {
|
|
|
|
font-family: helvetica, arial, sans-serif;
|
|
|
|
font-size: 9pt
|
|
|
|
}
|
2025-01-05 01:45:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
</style>
|