Compare commits
2 Commits
10f4f0b2c6
...
270bcc9cfa
Author | SHA1 | Date | |
---|---|---|---|
|
270bcc9cfa | ||
|
4decaaf156 |
143
poptimal.js
143
poptimal.js
@ -19,31 +19,33 @@ const MAG = 2;
|
||||
const WIDTH = 20;
|
||||
const HEIGHT = WIDTH;
|
||||
|
||||
function poptimal_svg() {
|
||||
function randomise_params() {
|
||||
const palette_name = random.choice(Array.from(PALETTES.keys()));
|
||||
const palette_fn = PALETTES.get(palette_name);
|
||||
const palette = palette_fn();
|
||||
const patterns = [1,2].map((n) => { return {
|
||||
i: n,
|
||||
colour: palette[n],
|
||||
m: random.choice([1, 2, 3, 4, 5]),
|
||||
n: random.choice([1, 2, 3, 4, 5]),
|
||||
f: random.choice(RADIUS_OPTS),
|
||||
r: random.float(0, 0.4),
|
||||
}});
|
||||
return {
|
||||
background: palette[0],
|
||||
palette: palette_name,
|
||||
patterns: patterns
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function poptimal_svg(params) {
|
||||
const window = new JSDOM().window;
|
||||
const document = window.document;
|
||||
const container = d3.select(document.body).append("div");
|
||||
|
||||
const dm = new DotMaker(WIDTH);
|
||||
|
||||
const m1 = random.choice([1, 2, 3, 4, 5]);
|
||||
const n1 = random.choice([1, 2, 3, 4, 5]);
|
||||
const m2 = random.choice([1, 2, 3, 4, 5]);
|
||||
const n2 = random.choice([1, 2, 3, 4, 5]);
|
||||
const palette_fn = random.choice(Array.from(PALETTES.values()));
|
||||
const palette = palette_fn();
|
||||
const bg = palette[0];
|
||||
const fg1 = palette[1];
|
||||
const fg2 = palette[2];
|
||||
const f1 = random.choice(RADIUS_OPTS);
|
||||
const f2 = random.choice(RADIUS_OPTS);
|
||||
const r1 = random.float(0, 0.4);
|
||||
const r2 = random.float(0, 0.4);
|
||||
|
||||
const dots1 = dm.dots(1 / m1, n1);
|
||||
const dots2 = dm.dots(1 / m2, n2);
|
||||
|
||||
|
||||
const svg = container.append("svg")
|
||||
.attr("width", WIDTH * CELL * MAG)
|
||||
.attr("height", HEIGHT * CELL * MAG)
|
||||
@ -54,30 +56,23 @@ function poptimal_svg() {
|
||||
.attr("y", 0)
|
||||
.attr("width", WIDTH)
|
||||
.attr("height", WIDTH)
|
||||
.attr("fill", bg);
|
||||
.attr("fill", params.background);
|
||||
|
||||
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);
|
||||
|
||||
params.patterns.map((p) => {
|
||||
const dots = dm.dots(1 / p.m, p.n);
|
||||
const dots_g = svg.append("g")
|
||||
.attr("id", `dots${p.i}`);
|
||||
|
||||
dots_g.selectAll("circle")
|
||||
.data(dots)
|
||||
.join("circle")
|
||||
.attr("r", (d) => dm.radius(d, p.f, p.r))
|
||||
.attr("fill", p.colour)
|
||||
.attr("cx", (d) => d.x)
|
||||
.attr("cy", (d) => d.y);
|
||||
});
|
||||
|
||||
const node = svg.node();
|
||||
|
||||
node.setAttributeNS(xmlns, "xmlns", svgns);
|
||||
@ -87,25 +82,73 @@ function poptimal_svg() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function post_image(image, alt_text, cf) {
|
||||
const status_url = `${cf.base_url}/api/v1/statuses`;
|
||||
const media_url = `${cf.base_url}/api/v1/media`;
|
||||
const headers = {
|
||||
'Authorization': `Bearer ${cf.access_token}`,
|
||||
};
|
||||
const rawData = await promises.readFile(image);
|
||||
const blob = new Blob([rawData]);
|
||||
const fd = new FormData();
|
||||
fd.set('description', alt_text);
|
||||
fd.set('file', blob, image, 'text/png');
|
||||
const resp = await fetch(media_url, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: fd
|
||||
});
|
||||
|
||||
const bodyjson = await resp.text();
|
||||
const response = JSON.parse(bodyjson);
|
||||
const media_id = response["id"];
|
||||
|
||||
headers['Accept'] = 'application/json';
|
||||
headers['Content-Type'] = 'application/json';
|
||||
const resp2 = await fetch(status_url, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify({ media_ids: [ media_id ] })
|
||||
});
|
||||
const bodyjson2 = await resp2.text();
|
||||
console.log(bodyjson2);
|
||||
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const argv = yargs(hideBin(process.argv))
|
||||
.usage("Usage: -w WIDTH -o OUTPUT_PNG")
|
||||
.default('w', 1200)
|
||||
.default('o', 'poptimal.png').argv;
|
||||
.usage("Usage: -s SIZE -o output.png -c config.json")
|
||||
.default('s', 1200)
|
||||
.default('g', 'config.json').argv;
|
||||
|
||||
const svg = poptimal_svg(argv.w);
|
||||
const cfjson = await promises.readFile(argv.g);
|
||||
const cf = JSON.parse(cfjson);
|
||||
|
||||
const fn = argv.o || String(Date.now()) + '.png';
|
||||
|
||||
const imgfile = cf['working_dir'] + '/' + fn;
|
||||
|
||||
const params = randomise_params();
|
||||
const alt_text = JSON.stringify(params);
|
||||
|
||||
const svg = poptimal_svg(params);
|
||||
const opts = {
|
||||
background: 'rgba(255, 255, 255, 1.0)',
|
||||
fitTo: {
|
||||
mode: 'width',
|
||||
value: argv.w,
|
||||
value: argv.s,
|
||||
},
|
||||
};
|
||||
const resvg = new Resvg(svg, opts)
|
||||
const pngData = resvg.render()
|
||||
const pngBuffer = pngData.asPng()
|
||||
|
||||
await promises.writeFile(argv.o, pngBuffer);
|
||||
const resvg = new Resvg(svg, opts);
|
||||
const pngData = resvg.render();
|
||||
const pngBuffer = pngData.asPng();
|
||||
|
||||
await promises.writeFile(imgfile, pngBuffer);
|
||||
if( cf['base_url'] ) {
|
||||
await post_image(imgfile, alt_text, cf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user