Compare commits
No commits in common. "270bcc9cfaef1ce1660a34c6404307dcc50928e8" and "10f4f0b2c6b9102dff5914895fb1bf0f6c13af42" have entirely different histories.
270bcc9cfa
...
10f4f0b2c6
143
poptimal.js
143
poptimal.js
@ -19,33 +19,31 @@ const MAG = 2;
|
|||||||
const WIDTH = 20;
|
const WIDTH = 20;
|
||||||
const HEIGHT = WIDTH;
|
const HEIGHT = WIDTH;
|
||||||
|
|
||||||
function randomise_params() {
|
function poptimal_svg() {
|
||||||
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 window = new JSDOM().window;
|
||||||
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 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")
|
const svg = container.append("svg")
|
||||||
.attr("width", WIDTH * CELL * MAG)
|
.attr("width", WIDTH * CELL * MAG)
|
||||||
.attr("height", HEIGHT * CELL * MAG)
|
.attr("height", HEIGHT * CELL * MAG)
|
||||||
@ -56,23 +54,30 @@ function poptimal_svg(params) {
|
|||||||
.attr("y", 0)
|
.attr("y", 0)
|
||||||
.attr("width", WIDTH)
|
.attr("width", WIDTH)
|
||||||
.attr("height", WIDTH)
|
.attr("height", WIDTH)
|
||||||
.attr("fill", params.background);
|
.attr("fill", bg);
|
||||||
|
|
||||||
|
const dots_g1 = svg.append("g")
|
||||||
|
.attr("id", "dots1");
|
||||||
|
|
||||||
params.patterns.map((p) => {
|
dots_g1.selectAll("circle")
|
||||||
const dots = dm.dots(1 / p.m, p.n);
|
.data(dots1)
|
||||||
const dots_g = svg.append("g")
|
.join("circle")
|
||||||
.attr("id", `dots${p.i}`);
|
.attr("r", (d) => dm.radius(d, f1, r1))
|
||||||
|
.attr("fill", fg1)
|
||||||
dots_g.selectAll("circle")
|
.attr("cx", (d) => d.x)
|
||||||
.data(dots)
|
.attr("cy", (d) => d.y);
|
||||||
.join("circle")
|
|
||||||
.attr("r", (d) => dm.radius(d, p.f, p.r))
|
const dots_g2 = svg.append("g")
|
||||||
.attr("fill", p.colour)
|
.attr("id", "dots2");
|
||||||
.attr("cx", (d) => d.x)
|
|
||||||
.attr("cy", (d) => d.y);
|
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);
|
||||||
|
|
||||||
const node = svg.node();
|
const node = svg.node();
|
||||||
|
|
||||||
node.setAttributeNS(xmlns, "xmlns", svgns);
|
node.setAttributeNS(xmlns, "xmlns", svgns);
|
||||||
@ -82,73 +87,25 @@ function poptimal_svg(params) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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() {
|
async function main() {
|
||||||
const argv = yargs(hideBin(process.argv))
|
const argv = yargs(hideBin(process.argv))
|
||||||
.usage("Usage: -s SIZE -o output.png -c config.json")
|
.usage("Usage: -w WIDTH -o OUTPUT_PNG")
|
||||||
.default('s', 1200)
|
.default('w', 1200)
|
||||||
.default('g', 'config.json').argv;
|
.default('o', 'poptimal.png').argv;
|
||||||
|
|
||||||
const cfjson = await promises.readFile(argv.g);
|
const svg = poptimal_svg(argv.w);
|
||||||
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 = {
|
const opts = {
|
||||||
background: 'rgba(255, 255, 255, 1.0)',
|
background: 'rgba(255, 255, 255, 1.0)',
|
||||||
fitTo: {
|
fitTo: {
|
||||||
mode: 'width',
|
mode: 'width',
|
||||||
value: argv.s,
|
value: argv.w,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const resvg = new Resvg(svg, opts)
|
||||||
|
const pngData = resvg.render()
|
||||||
|
const pngBuffer = pngData.asPng()
|
||||||
|
|
||||||
const resvg = new Resvg(svg, opts);
|
await promises.writeFile(argv.o, pngBuffer);
|
||||||
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