Skip to content

Instantly share code, notes, and snippets.

@jleedev
Last active November 24, 2024 19:42
Show Gist options
  • Select an option

  • Save jleedev/eee830909d23c4a89a9078cbac241e54 to your computer and use it in GitHub Desktop.

Select an option

Save jleedev/eee830909d23c4a89a9078cbac241e54 to your computer and use it in GitHub Desktop.
import sharp from "npm:sharp";
import * as Plot from "npm:@observablehq/plot";
import land110 from "npm:world-atlas/land-110m.json" with { type: "json" };
import topojson from "npm:topojson-client";
import { DOMImplementation, XMLSerializer } from "npm:@xmldom/xmldom";
const document = new DOMImplementation().createHTMLDocument();
const land = topojson.feature(land110, land110.objects.land);
const figure = Plot.plot({
document,
marks: [
Plot.geo({ type: "Sphere" }, { fill: "lightskyblue" }),
Plot.geo(land, { fill: "cornsilk" }),
],
projection: "equal-earth",
marginTop: 0,
marginRight: 0,
marginBottom: 0,
marginLeft: 0,
});
const svg = new XMLSerializer().serializeToString(figure);
const img = sharp(new TextEncoder().encode(svg));
async function toImageData(im) {
const { data: { buffer }, info: { width, height } } = await im.raw().toBuffer(
{ resolveWithObject: true },
);
return new ImageData(new Uint8ClampedArray(buffer), width, height);
}
const { rows, columns } = process.stdout;
const im = await toImageData(
img.resize(columns >> 1, rows - 3, { fit: "inside", background: "white" }),
);
for (let i = 0; i < im.data.length; i += 4) {
let [r, g, b, a] = im.data.subarray(i, i + 4);
const ansi = `\x1b[48;2;${r};${g};${b}m`;
process.stdout.write(ansi + " ");
if ((i / 4) % im.width === im.width - 1) console.log();
}
console.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment