Skip to content

Instantly share code, notes, and snippets.

@piousdeer
Last active April 12, 2022 18:03
Show Gist options
  • Save piousdeer/fc40d412a8476c9038c62f048c96a54e to your computer and use it in GitHub Desktop.
Save piousdeer/fc40d412a8476c9038c62f048c96a54e to your computer and use it in GitHub Desktop.
<script>
function calc(from = 0, to = Infinity) {
const ctx = document.querySelector("canvas").getContext("2d");
ctx.font = "DejaVu Sans";
results = [];
let i = from;
while (i < to) {
let char;
if (i % 0xff == 0) {
console.log("0x" + i.toString(16));
}
i++;
try {
char = String.fromCodePoint(i);
} catch {
break;
}
const metrics = ctx.measureText(char.repeat(10));
results.push([metrics, char]);
}
str = "";
str +=
"AVERAGE: " +
(
results.reduce((acc, [metrics]) => acc + metrics.width, 0) /
results.length
).toFixed(10);
str += "\n\n";
str += results
.sort(([metrics1], [metrics2]) => metrics2.width - metrics1.width)
.map(([metrics, char]) => `${metrics.width.toFixed(10)} ${char}`)
.join("\n");
alert("cool, now hit f12 and type copy(str)");
}
</script>
<canvas style="display: none"></canvas>
<button onclick="calc()">Caclc</button>
<button onclick="calc(32, 126+1)">Calc (ASCII)</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment