Compare commits

...

17 Commits

Author SHA1 Message Date
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' (#35) from feature-bot-cell-size into main
Reviewed-on: #35
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' (#34) from feature-json-params into main
Reviewed-on: #34
2025-04-06 08:24:10 +00:00
c852633119 Merge pull request 'Adjust the size of cells and dots' (#33) from feature-size into main
Reviewed-on: #33
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' (#31) from feature-clip-web-version into main
Reviewed-on: #31
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 77 additions and 44 deletions

View File

@ -18,7 +18,7 @@ import {ColourNamer} from './src/components/colour_namer.js';
const CELL = 10;
const MAG = 2;
const WIDTH = 20;
const WIDTH = 200;
const HEIGHT = WIDTH;
// number of pixels which have to be visible for a colour to be
// mentioned in the alt text
@ -36,10 +36,12 @@ function randomise_params() {
f: random.choice(RADIUS_OPTS),
r: random.float(0, 0.4),
}});
const cell = 5 + random.float() * random.float() * 55;
return {
background: palette[0],
palette: palette_name,
patterns: patterns
patterns: patterns,
cell: cell,
}
}
@ -134,18 +136,21 @@ function poptimal_svg(params) {
const document = window.document;
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")
.attr("width", WIDTH * CELL * MAG)
.attr("height", HEIGHT * CELL * MAG)
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
.attr("width", WIDTH * MAG)
.attr("height", HEIGHT * 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("width", width)
.attr("height", height)
.attr("fill", params.background);
@ -227,7 +232,6 @@ async function main() {
const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
const namer = new ColourNamer();
console.log(`Loading colours ${colourf}`);
await namer.load_colours(colourf);
@ -250,7 +254,7 @@ async function main() {
// so we don't include obscured colours
const hist = await get_histogram(imgfile);
const alt_text = image_description(namer, params, hist);
params.alt_text = alt_text;
await save_params(paramsfile, params);
console.log(alt_text);
console.log(imgfile);

View File

@ -44,10 +44,12 @@ function int_range(v1, v2) {
}
class DotMaker {
constructor(width) {
constructor(width, height) {
this.width = width;
this.height = height;
this.wh = 0.5 * (width + height);
this.cx = 0.5 * width;
this.cy = 0.5 * width;
this.cy = 0.5 * height;
}
dots(m, n, clip) {
@ -55,13 +57,13 @@ class DotMaker {
return [];
}
const ps = [];
const is = int_range(-this.width, this.width / m)
const is = int_range(-this.width, this.height / m)
is.map((i) => {
const js = int_range(m * i + (m - n) * this.width, m * i)
js.map((j) => {
const x = (j - m * i) / (m - n);
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});
}
});
@ -78,25 +80,25 @@ class DotMaker {
case "left":
return maxr * (this.width - d.x) / this.width;
case "down":
return maxr * d.y / this.width;
return maxr * d.y / this.height;
case "up":
return maxr * (this.width - d.y) / this.width;
return maxr * (this.height - d.y) / this.height;
case "right-up":
return 0.5 * maxr * (d.x + this.width - d.y) / this.width;
return 0.5 * maxr * (d.x + this.height - d.y) / this.wh;
case "left-up":
return 0.5 * maxr * (this.width - d.x + this.width - d.y) / this.width;
return 0.5 * maxr * (this.width - d.x + this.height - d.y) / this.wh;
case "right-down":
return 0.5 * maxr * (d.x + d.y) / this.width;
return 0.5 * maxr * (d.x + d.y) / this.wh;
case "left-down":
return 0.5 * maxr * (this.width - d.x + d.y) / this.width;
return 0.5 * maxr * (this.width - d.x + d.y) / this.wh;
case "out":
return 2 * maxr * distance((d.x - this.cx), (d.y - this.cy)) / this.width;
return 2 * maxr * distance((d.x - this.cx), (d.y - this.cy)) / this.wh;
case "in":
return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
return 2 * maxr * (0.5 * this.wh - distance((d.x - this.cx), (d.y - this.cy))) / this.wh;
// case "hyper-out":
// return 2 * maxr * Math.abs(d.x - this.cx) (d.y - this.cy)) / this.width;
// case "hyoer-in":
// case "hyper-in":
// return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
case "noise":

View File

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

View File

@ -8,11 +8,11 @@ toc: false
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.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>
<div class="grid grid-cols-2">
<div class="grid grid-cols-3">
<div class="card">
<div class="card grid-colspan-1">
```js
@ -24,18 +24,26 @@ import random from "npm:random";
import * as resvg from 'npm:@resvg/resvg-wasm';
const CELL = 10;
const MAG = 2;
const WIDTH = 20;
const HEIGHT = WIDTH;
const WIDTH = 200;
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 ctrl1 = new DotControls(d3.color("red").formatHex(), RADIUS_OPTS);
const ctrl2 = new DotControls(d3.color("blue").formatHex(), RADIUS_OPTS);
const ctrl1 = new DotControls(d3.color("white").formatHex(), RADIUS_OPTS);
const ctrl2 = new DotControls(d3.color("white").formatHex(), RADIUS_OPTS);
const fg1 = view(ctrl1.fg);
@ -87,6 +95,8 @@ ctrl1.null_grid();
ctrl2.null_grid();
palette_input.value = PALETTES.get(rpalette);
palette_input.dispatchEvent(new Event("input"));
cell_input.value = 5 + random.float() * random.float() * 55;
cell_input.dispatchEvent(new Event("input"));
ctrl1.random_grid();
ctrl2.random_grid();
@ -113,13 +123,18 @@ if( palette_fn ) {
```
</div>
<div>
<div class="card grid-colspan-2">
```js
const dots1 = dm.dots(1 / m1, n1, true);
const dots2 = dm.dots(1 / m2, n2, true);
const width = WIDTH / cell;
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);
```
@ -129,9 +144,18 @@ const dots2 = dm.dots(1 / m2, n2, true);
const svg = d3.create("svg")
.attr("width", WIDTH * CELL * MAG)
.attr("height", HEIGHT * CELL * MAG)
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
.attr("width", WIDTH * MAG)
.attr("height", HEIGHT * MAG)
.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
@ -151,14 +175,15 @@ bg_g.selectAll("rect")
.join("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", WIDTH)
.attr("height", WIDTH)
.attr("width", width)
.attr("height", height)
.attr("fill", (d) => d.bg)
;
const dots_g1 = svg.append("g")
.attr("id", "dots1");
.attr("id", "dots1")
.attr("clip-path", "url(#clipRect)");
dots_g1.selectAll("circle")
.data(dots1)
@ -168,7 +193,8 @@ dots_g1.selectAll("circle")
.attr("fill", fg1);
const dots_g2 = svg.append("g")
.attr("id", "dots2");
.attr("id", "dots2")
.attr("clip-path", "url(#clipRect)");
dots_g2.selectAll("circle")
.data(dots2)
@ -211,6 +237,7 @@ display(download(() => {
```
(PNGs made with <a href="https://github.com/thx/resvg-js">resvg-wasm</a> in-browser)
</div>
</div>
<style>