Compare commits

..

28 Commits

Author SHA1 Message Date
c4e3c45a4d Merge pull request 'Fixed flashing grids' () from rc-1.2.2 into main
Reviewed-on: 
2025-04-20 23:56:50 +00:00
Mike Lynch
07713bcff7 Factored the radius function out of DotMaker - this is so that the
web version can update other parameters like cell size without making
the grids reset and flicker
2025-04-21 09:54:40 +10:00
14bee6cf6e Merge pull request 'rc-1.2.1 - new patterns and refactoring' () from rc-1.2.1 into main
Reviewed-on: 
2025-04-20 06:04:44 +00:00
Mike Lynch
824605820b Bumped version number 2025-04-20 16:03:44 +10:00
Mike Lynch
bf7ae246a3 Generalised grids and bands into a single function 2025-04-20 16:02:41 +10:00
Mike Lynch
077dc5254d Fixed alt-text for hyper-out 2025-04-20 15:32:22 +10:00
Mike Lynch
3a9d2190ba bot script now uses closures - I really shouldn't have to have
changed this in both places
2025-04-20 15:29:55 +10:00
Mike Lynch
f397af24cd the radius method now returns a closure, so we can pick random
parameters for things like the sinusoid grids or centre points
2025-04-20 15:25:08 +10:00
Mike Lynch
31c093e1ed Merge branch 'main' into rc-1.2.1 2025-04-20 14:58:46 +10:00
Mike Lynch
81f0702e27 Added first version of sin (diamond grid) and horizontal and vertical
bands
2025-04-19 18:44:01 +08:00
Mike Lynch
6a0d2470a1 Added hyperbolic vignettes in and out 2025-04-19 18:27:58 +08:00
Mike Lynch
ca10f8a038 Saves alt text to the image params 2025-04-06 23:03:48 +00:00
00e5fed522 Merge pull request 'feature-bot-cell-size' () from feature-bot-cell-size into main
Reviewed-on: 
2025-04-06 08:30:07 +00:00
Mike Lynch
91ab1a453c Merge branch 'main' into feature-bot-cell-size
bringing the json-params stuff from the server
2025-04-06 18:26:47 +10:00
Mike Lynch
3eb527008a Made the bot version do bigger cell sizes 2025-04-06 18:25:54 +10:00
007da37674 Merge pull request 'feature-json-params' () from feature-json-params into main
Reviewed-on: 
2025-04-06 08:24:10 +00:00
c852633119 Merge pull request 'Adjust the size of cells and dots' () from feature-size into main
Reviewed-on: 
2025-04-06 07:14:02 +00:00
Mike Lynch
77970dbcd1 Cell size is now randomised 2025-04-06 17:12:51 +10:00
Mike Lynch
46fd00600e Adjustable cell size 2025-04-06 16:47:16 +10:00
Mike Lynch
6c42510a7d Revert "Checking in this because I'm not sure why it's not working"
This reverts commit 6e2036ac7d2c04d184715d2b6fa93a5b3ede8bfd.
2025-04-06 15:49:25 +10:00
Mike Lynch
6e2036ac7d Checking in this because I'm not sure why it's not working 2025-04-06 15:36:19 +10:00
Mike Lynch
ce4771a79f Bumped version v1.1.2 -> 1.2.0 and improved styling 2025-04-06 12:43:34 +10:00
Mike Lynch
844e847eab Grids are working properly with varying aspect ratios 2025-04-06 12:43:17 +10:00
Mike Lynch
7b05658a7f Added height control, rearranged things so that the pattern isn't
randomised every time the dimensions change
2025-04-06 09:54:17 +10:00
e6f977b29e Merge pull request 'Better clipping for web' () from feature-clip-web-version into main
Reviewed-on: 
2025-03-26 07:59:04 +00:00
Mike Lynch
0f066251ca Merge branch 'feature-big-pngs' into feature-clip-web-version
Pulling the large pngs into this branch
2025-03-26 18:57:35 +11:00
Mike Lynch
7a1db41c18 Got clipping working in the web version too 2025-03-26 18:57:25 +11:00
Mike Lynch
3b50b39569 Made the downloadable pngs 1200x1200 2025-03-26 18:33:36 +11:00
4 changed files with 137 additions and 82 deletions

@ -12,13 +12,13 @@ const xmlns = "http://www.w3.org/2000/xmlns/";
const xlinkns = "http://www.w3.org/1999/xlink"; const xlinkns = "http://www.w3.org/1999/xlink";
const svgns = "http://www.w3.org/2000/svg"; const svgns = "http://www.w3.org/2000/svg";
import {RADIUS_OPTS, RADIUS_DESC, DotMaker} from './src/components/dots.js'; import {RADIUS_OPTS, RADIUS_DESC, radius_func, DotMaker} from './src/components/dots.js';
import {PALETTES} from './src/components/palettes.js'; import {PALETTES} from './src/components/palettes.js';
import {ColourNamer} from './src/components/colour_namer.js'; import {ColourNamer} from './src/components/colour_namer.js';
const CELL = 10; const CELL = 10;
const MAG = 2; const MAG = 2;
const WIDTH = 20; const WIDTH = 200;
const HEIGHT = WIDTH; const HEIGHT = WIDTH;
// number of pixels which have to be visible for a colour to be // number of pixels which have to be visible for a colour to be
// mentioned in the alt text // mentioned in the alt text
@ -36,10 +36,12 @@ function randomise_params() {
f: random.choice(RADIUS_OPTS), f: random.choice(RADIUS_OPTS),
r: random.float(0, 0.4), r: random.float(0, 0.4),
}}); }});
const cell = 5 + random.float() * random.float() * 55;
return { return {
background: palette[0], background: palette[0],
palette: palette_name, palette: palette_name,
patterns: patterns patterns: patterns,
cell: cell,
} }
} }
@ -134,30 +136,34 @@ function poptimal_svg(params) {
const document = window.document; const document = window.document;
const container = d3.select(document.body).append("div"); const container = d3.select(document.body).append("div");
const dm = new DotMaker(WIDTH); const width = WIDTH / params.cell;
const height = WIDTH / params.cell;
const dm = new DotMaker(width, height);
const svg = container.append("svg") const svg = container.append("svg")
.attr("width", WIDTH * CELL * MAG) .attr("width", WIDTH * MAG)
.attr("height", HEIGHT * CELL * MAG) .attr("height", HEIGHT * MAG)
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]); .attr("viewBox", [ 0, 0, width, height ]);
const background = svg.append("rect") const background = svg.append("rect")
.attr("x", 0) .attr("x", 0)
.attr("y", 0) .attr("y", 0)
.attr("width", WIDTH) .attr("width", width)
.attr("height", WIDTH) .attr("height", height)
.attr("fill", params.background); .attr("fill", params.background);
params.patterns.map((p) => { params.patterns.map((p) => {
const dots = dm.dots(1 / p.m, p.n, false); const dots = dm.dots(1 / p.m, p.n, false);
const rfunc = radius_func(p.f);
const dots_g = svg.append("g") const dots_g = svg.append("g")
.attr("id", `dots${p.i}`); .attr("id", `dots${p.i}`);
dots_g.selectAll("circle") dots_g.selectAll("circle")
.data(dots) .data(dots)
.join("circle") .join("circle")
.attr("r", (d) => dm.radius(d, p.f, p.r)) .attr("r", (d) => rfunc(d, p.r, width, height))
.attr("fill", p.colour) .attr("fill", p.colour)
.attr("cx", (d) => d.x) .attr("cx", (d) => d.x)
.attr("cy", (d) => d.y); .attr("cy", (d) => d.y);
@ -227,7 +233,6 @@ async function main() {
const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour']; const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
const namer = new ColourNamer(); const namer = new ColourNamer();
console.log(`Loading colours ${colourf}`);
await namer.load_colours(colourf); await namer.load_colours(colourf);
@ -250,7 +255,7 @@ async function main() {
// so we don't include obscured colours // so we don't include obscured colours
const hist = await get_histogram(imgfile); const hist = await get_histogram(imgfile);
const alt_text = image_description(namer, params, hist); const alt_text = image_description(namer, params, hist);
params.alt_text = alt_text;
await save_params(paramsfile, params); await save_params(paramsfile, params);
console.log(alt_text); console.log(alt_text);
console.log(imgfile); console.log(imgfile);

@ -11,8 +11,11 @@ const RADIUS_OPTS = [
"right-down", "right-down",
"left-up", "left-up",
"left-down", "left-down",
"in", "circle-in",
"out", "circle-out",
"hyper-in",
"hyper-out",
"grid",
"noise", "noise",
]; ];
@ -26,15 +29,19 @@ const RADIUS_DESC = {
"right-down": "getting larger towards the lower right", "right-down": "getting larger towards the lower right",
"left-up": "getting larger towards the upper left", "left-up": "getting larger towards the upper left",
"left-down": "getting larger towards the lower left", "left-down": "getting larger towards the lower left",
"in": "getting larger in the centre", "circle-in": "forming a circle at the centre",
"out": "getting larger at the edges", "circle-out": "leaving a circular gap at the centre",
"hyper-in": "forming a starlike pattern at the centre",
"hyper-out": "leaving a starlike gap in the centre",
"noise": "of random sizes", "noise": "of random sizes",
"grid": "forming a grid pattern",
} }
function distance(dx, dy) { function distance(dx, dy) {
return Math.sqrt(dx ** 2 + dy ** 2); return Math.sqrt(dx ** 2 + dy ** 2);
} }
function int_range(v1, v2) { function int_range(v1, v2) {
const vs = [v1, v2]; const vs = [v1, v2];
vs.sort((a, b) => a - b); vs.sort((a, b) => a - b);
@ -43,11 +50,17 @@ function int_range(v1, v2) {
return [...Array(high - low + 1).keys()].map((i) => i + low); return [...Array(high - low + 1).keys()].map((i) => i + low);
} }
function randint(min, max) {
return min + Math.floor(Math.random() * (max + 1));
}
class DotMaker { class DotMaker {
constructor(width) { constructor(width, height) {
this.width = width; this.width = width;
this.height = height;
this.wh = 0.5 * (width + height);
this.cx = 0.5 * width; this.cx = 0.5 * width;
this.cy = 0.5 * width; this.cy = 0.5 * height;
} }
dots(m, n, clip) { dots(m, n, clip) {
@ -55,58 +68,61 @@ class DotMaker {
return []; return [];
} }
const ps = []; const ps = [];
const is = int_range(-this.width, this.width / m) const is = int_range(-this.width, this.height / m)
is.map((i) => { is.map((i) => {
const js = int_range(m * i + (m - n) * this.width, m * i) const js = int_range(m * i + (m - n) * this.width, m * i)
js.map((j) => { js.map((j) => {
const x = (j - m * i) / (m - n); const x = (j - m * i) / (m - n);
const y = m * (x + i); const y = m * (x + i);
if( !clip || (x > 0 && y > 0 && x < this.width && y < this.width) ) { if( !clip || (x > 0 && y > 0 && x < this.width && y < this.height) ) {
ps.push({i:i, j:j, x:x, y:y}); ps.push({i:i, j:j, x:x, y:y});
} }
}); });
}); });
return ps; return ps;
} }
radius(d, func, maxr) {
switch (func) {
case "const":
return maxr;
case "right":
return maxr * d.x / this.width;
case "left":
return maxr * (this.width - d.x) / this.width;
case "down":
return maxr * d.y / this.width;
case "up":
return maxr * (this.width - d.y) / this.width;
case "right-up":
return 0.5 * maxr * (d.x + this.width - d.y) / this.width;
case "left-up":
return 0.5 * maxr * (this.width - d.x + this.width - d.y) / this.width;
case "right-down":
return 0.5 * maxr * (d.x + d.y) / this.width;
case "left-down":
return 0.5 * maxr * (this.width - d.x + d.y) / this.width;
case "out":
return 2 * maxr * distance((d.x - this.cx), (d.y - this.cy)) / this.width;
case "in":
return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
// case "hyper-out":
// return 2 * maxr * Math.abs(d.x - this.cx) (d.y - this.cy)) / this.width;
// case "hyoer-in":
// return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
case "noise":
return maxr * Math.random();
default:
return maxr;
}
}
} }
export { RADIUS_OPTS, RADIUS_DESC, DotMaker };
function radius_func(func) {
switch (func) {
case "const":
return (d, r, w, h) => r;
case "right":
return (d, r, w, h) => r * d.x / w;
case "left":
return (d, r, w, h) => r * (w - d.x) / w;
case "down":
return (d, r, w, h) => r * d.y / h;
case "up":
return (d, r, w, h) => r * (h - d.y) / h;
case "right-up":
return (d, r, w, h) => r * (d.x + h - d.y) / (w + h);
case "left-up":
return (d, r, w, h) => r * (w - d.x + h - d.y) / (w + h);
case "right-down":
return (d, r, w, h) => r * (d.x + d.y) / (w + h);
case "left-down":
return (d, r, w, h) => r * (w - d.x + d.y) / (w + h);
case "circle-out":
return (d, r, w, h) => 4 * r * distance((d.x - w / 2), (d.y - h / 2)) / (w + h);
case "circle-in":
return (d, r, w, h) => 4 * r * (0.5 * (w + h) - distance((d.x - w / 2), (d.y - h / 2))) / (w + h);
case "hyper-in":
return (d, r, w, h) => 2 * r * (1 - Math.abs((d.x - w / 2) * (d.y - h / 2)) / (w + h));
case "hyper-out":
return (d, r, w, h) => 2 * r * Math.abs((d.x - w / 2) * (d.y - h / 2)) / (w + h);
case "noise":
return (d, r, w, h) => r * Math.random();
case "grid":
const xk = Math.PI * (2 * randint(1, 10) - 1);
const yk = Math.PI * (2 * randint(1, 10) - 1);
return (d, r, w, h) => r * (0.5 + 0.5 * (Math.sin(xk * d.x / w) + Math.sin(yk * d.y / h)));
default:
return (d, r, w, h) => r;
}
}
export { RADIUS_OPTS, RADIUS_DESC, DotMaker, radius_func };

@ -73,7 +73,7 @@ export async function download_as_png (svg) {
const opts = { const opts = {
fitTo: { fitTo: {
mode: 'width', // If you need to change the size mode: 'width', // If you need to change the size
value: 400, value: 1200,
} }
}; };
const resvgJS = new resvg.Resvg(svgstr, opts) const resvgJS = new resvg.Resvg(svgstr, opts)

@ -8,15 +8,15 @@ toc: false
colourful generative patterns using [d3](https://d3js.org/) and [Observable Framework](https://observablehq.com/framework/) colourful generative patterns using [d3](https://d3js.org/) and [Observable Framework](https://observablehq.com/framework/)
<p>v1.1.1 | 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> <p>v1.2.2 | 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> | <a href="https://mikelynch.org/2025/Mar/09/poptimal/">about</a></p>
<div class="grid grid-cols-2"> <div class="grid grid-cols-3">
<div class="card"> <div class="card grid-colspan-1">
```js ```js
import {RADIUS_OPTS, DotMaker} from './components/dots.js'; import {RADIUS_OPTS, DotMaker, radius_func} from './components/dots.js';
import {DotControls} from './components/controls.js'; import {DotControls} from './components/controls.js';
import {PALETTES} from './components/palettes.js'; import {PALETTES} from './components/palettes.js';
import {download, download_as_svg, download_as_png} from './components/download.js'; import {download, download_as_svg, download_as_png} from './components/download.js';
@ -24,18 +24,26 @@ import random from "npm:random";
import * as resvg from 'npm:@resvg/resvg-wasm'; import * as resvg from 'npm:@resvg/resvg-wasm';
const CELL = 10;
const MAG = 2; const MAG = 2;
const WIDTH = 20; const WIDTH = 200;
const HEIGHT = WIDTH; const HEIGHT = 200;
const dm = new DotMaker(WIDTH); const cell_input = Inputs.range([5,60], {value: 10, label: "cell size"});
const cell = view(cell_input);
const bg_input = Inputs.color({label: "background", value: d3.color("yellow").formatHex()});
```
```js
const bg_input = Inputs.color({
label: "background", value: d3.color("white").formatHex()
});
const bg = view(bg_input); const bg = view(bg_input);
const ctrl1 = new DotControls(d3.color("red").formatHex(), RADIUS_OPTS); const ctrl1 = new DotControls(d3.color("white").formatHex(), RADIUS_OPTS);
const ctrl2 = new DotControls(d3.color("blue").formatHex(), RADIUS_OPTS); const ctrl2 = new DotControls(d3.color("white").formatHex(), RADIUS_OPTS);
const fg1 = view(ctrl1.fg); const fg1 = view(ctrl1.fg);
@ -87,6 +95,8 @@ ctrl1.null_grid();
ctrl2.null_grid(); ctrl2.null_grid();
palette_input.value = PALETTES.get(rpalette); palette_input.value = PALETTES.get(rpalette);
palette_input.dispatchEvent(new Event("input")); palette_input.dispatchEvent(new Event("input"));
cell_input.value = 5 + random.float() * random.float() * 55;
cell_input.dispatchEvent(new Event("input"));
ctrl1.random_grid(); ctrl1.random_grid();
ctrl2.random_grid(); ctrl2.random_grid();
@ -113,14 +123,26 @@ if( palette_fn ) {
``` ```
</div> </div>
<div> <div class="card grid-colspan-2">
```js ```js
const dots1 = dm.dots(1 / m1, n1, true); const width = WIDTH / cell;
const dots2 = dm.dots(1 / m2, n2, true); const height = HEIGHT / cell;
const dm = new DotMaker(width, height);
const dots1 = dm.dots(1 / m1, n1, false);
const dots2 = dm.dots(1 / m2, n2, false);
```
```js
const rfunc1 = radius_func(f1);
const rfunc2 = radius_func(f2);
``` ```
@ -129,9 +151,18 @@ const dots2 = dm.dots(1 / m2, n2, true);
const svg = d3.create("svg") const svg = d3.create("svg")
.attr("width", WIDTH * CELL * MAG) .attr("width", WIDTH * MAG)
.attr("height", HEIGHT * CELL * MAG) .attr("height", HEIGHT * MAG)
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]); .attr("viewBox", [ 0, 0, width, height ]);
svg.append("clipPath")
.attr("id", "clipRect")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height);
// re transitions: they should only run when updating the palette and // re transitions: they should only run when updating the palette and
@ -151,14 +182,15 @@ bg_g.selectAll("rect")
.join("rect") .join("rect")
.attr("x", 0) .attr("x", 0)
.attr("y", 0) .attr("y", 0)
.attr("width", WIDTH) .attr("width", width)
.attr("height", WIDTH) .attr("height", height)
.attr("fill", (d) => d.bg) .attr("fill", (d) => d.bg)
; ;
const dots_g1 = svg.append("g") const dots_g1 = svg.append("g")
.attr("id", "dots1"); .attr("id", "dots1")
.attr("clip-path", "url(#clipRect)");
dots_g1.selectAll("circle") dots_g1.selectAll("circle")
.data(dots1) .data(dots1)
@ -168,7 +200,8 @@ dots_g1.selectAll("circle")
.attr("fill", fg1); .attr("fill", fg1);
const dots_g2 = svg.append("g") const dots_g2 = svg.append("g")
.attr("id", "dots2"); .attr("id", "dots2")
.attr("clip-path", "url(#clipRect)");
dots_g2.selectAll("circle") dots_g2.selectAll("circle")
.data(dots2) .data(dots2)
@ -184,8 +217,8 @@ display(svg.node());
```js ```js
// separate code block for when I understand transitions better // separate code block for when I understand transitions better
dots_g1.selectAll("circle").attr("r", (d) => dm.radius(d, f1, r1)); dots_g1.selectAll("circle").attr("r", (d) => rfunc1(d, r1, width, height));
dots_g2.selectAll("circle").attr("r", (d) => dm.radius(d, f2, r2)); dots_g2.selectAll("circle").attr("r", (d) => rfunc2(d, r2, width, height));
``` ```
@ -211,6 +244,7 @@ display(download(() => {
``` ```
(PNGs made with <a href="https://github.com/thx/resvg-js">resvg-wasm</a> in-browser) (PNGs made with <a href="https://github.com/thx/resvg-js">resvg-wasm</a> in-browser)
</div>
</div> </div>
<style> <style>