Compare commits
17 Commits
feature-js
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
ca10f8a038 | ||
00e5fed522 | |||
|
91ab1a453c | ||
|
3eb527008a | ||
007da37674 | |||
c852633119 | |||
|
77970dbcd1 | ||
|
46fd00600e | ||
|
6c42510a7d | ||
|
6e2036ac7d | ||
|
ce4771a79f | ||
|
844e847eab | ||
|
7b05658a7f | ||
e6f977b29e | |||
|
0f066251ca | ||
|
7a1db41c18 | ||
|
3b50b39569 |
24
poptimal.js
24
poptimal.js
@ -18,7 +18,7 @@ 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,18 +136,21 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
@ -227,7 +232,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 +254,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);
|
||||||
|
@ -44,10 +44,12 @@ function int_range(v1, v2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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,13 +57,13 @@ 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});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -78,25 +80,25 @@ class DotMaker {
|
|||||||
case "left":
|
case "left":
|
||||||
return maxr * (this.width - d.x) / this.width;
|
return maxr * (this.width - d.x) / this.width;
|
||||||
case "down":
|
case "down":
|
||||||
return maxr * d.y / this.width;
|
return maxr * d.y / this.height;
|
||||||
case "up":
|
case "up":
|
||||||
return maxr * (this.width - d.y) / this.width;
|
return maxr * (this.height - d.y) / this.height;
|
||||||
case "right-up":
|
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":
|
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":
|
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":
|
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":
|
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":
|
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":
|
// case "hyper-out":
|
||||||
// return 2 * maxr * Math.abs(d.x - this.cx) (d.y - this.cy)) / this.width;
|
// 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;
|
// return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
|
||||||
|
|
||||||
case "noise":
|
case "noise":
|
||||||
|
@ -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)
|
||||||
|
67
src/index.md
67
src/index.md
@ -8,11 +8,11 @@ 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.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
|
```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,13 +123,18 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -129,9 +144,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 +175,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 +193,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)
|
||||||
@ -211,6 +237,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>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user