Skip to content

Instantly share code, notes, and snippets.

@osteele
Created December 13, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osteele/e29b05db2eec9bd17984604356e626a8 to your computer and use it in GitHub Desktop.
Save osteele/e29b05db2eec9bd17984604356e626a8 to your computer and use it in GitHub Desktop.
emojimorse
/**
* Author: Oliver Steele
*
* Graphic design adapted from image on
* https://www.offgridweb.com/preparation/morse-code-dits-dahs-dots-and-dashes/
*/
const DOT_COLOR = "orange";
const DASH_COLOR = "#4CACE9";
let rowStrings = [
"ET",
"IANM",
"SURWDKGO",
"HVF L PJBXCYZQ ",
"54 3 2 16 7 8 90",
];
// Source: https://home.unicode.org/emoji/emoji-frequency/
let emojiFreqs = [
"๐Ÿ˜‚ โค๏ธ",
"๐Ÿ˜ ๐Ÿคฃ",
"๐Ÿ˜Š ๐Ÿ™ ๐Ÿ’• ๐Ÿ˜ญ ๐Ÿ˜˜",
"๐Ÿ‘ ๐Ÿ˜… ๐Ÿ‘ ๐Ÿ˜ โ™ฅ๏ธ ๐Ÿ”ฅ ๐Ÿ’” ๐Ÿ’– ๐Ÿ’™ ๐Ÿ˜ข ๐Ÿค” ๐Ÿ˜† ๐Ÿ™„ ๐Ÿ’ช ๐Ÿ˜‰ ๐Ÿ‘Œ ๐Ÿค—", // skip โ˜บ๏ธ
"๐Ÿ’œ ๐Ÿ˜” ๐Ÿ˜Ž ๐Ÿ˜‡ ๐ŸŒน ๐Ÿคฆ ๐ŸŽ‰ โ€ผ๏ธ ๐Ÿ’ž โœŒ๏ธ โœจ ๐Ÿคท ๐Ÿ˜ฑ ๐Ÿ˜Œ ๐ŸŒธ ๐Ÿ™Œ ๐Ÿ˜‹ ๐Ÿ’— ๐Ÿ’š ๐Ÿ˜ ๐Ÿ’› ๐Ÿ™‚ ๐Ÿ’“ ๐Ÿคฉ ๐Ÿ˜„ ๐Ÿ˜€ ๐Ÿ–ค ๐Ÿ˜ƒ ๐Ÿ’ฏ ๐Ÿ™ˆ ๐Ÿ‘‡ ๐ŸŽถ ๐Ÿ˜’ ๐Ÿคญ โฃ๏ธ",
];
let includeEmoji = false;
let rows;
function setup() {
createCanvas(windowWidth - 10, windowHeight - 10);
noLoop();
}
function draw() {
let emoji = includeEmoji ? emojiFreqs.join(" ").split(/\s+/g) : [];
rows = [
["START"],
...rowStrings.map((s) =>
s.split("").map((c) => (c === " " ? emoji.shift() : c))
),
];
background(255);
fill(200);
textFont("Helvetica");
textAlign(RIGHT, BOTTOM);
textSize(20);
text("osteele.com", width, height - 16);
textSize(12);
text("Graphic design adapted from offgridweb.com", width, height);
if (!includeEmoji) {
textAlign(CENTER, BASELINE);
text("Click to add emoji", width / 2, 40);
}
textFont("Arial Black");
textAlign(CENTER, BASELINE);
noStroke();
textSize(30);
let th = textAscent() * 0.8;
push();
fill(DASH_COLOR);
translate(width / 4, 40);
text("DOT", 0, 0);
text("โ€ข", 0, 25);
translate(-textWidth("DOT") / 2 - 5, 0);
triangle(0, 0, 0, -th, -35, -th / 2);
fill(DASH_COLOR);
pop();
push();
fill(DOT_COLOR);
translate((width * 3) / 4, 40);
text("DASH", 0, 0);
text("โ€“", 0, 22);
translate(textWidth("DASH") / 2 + 5, 0);
triangle(0, 0, 0, -th, 35, -th / 2);
pop();
rows.forEach((row, j) => {
row.forEach((c, i) => {
// parent row, column and label
let iสน = floor(i / 2);
let jสน = j - 1;
let cสน = (rows[jสน] || [])[iสน];
// left child
let iสนสน = 2 * i;
let rowสนสน = rows[j + 1] || [];
let { x, y } = getCenter(i, j);
let { x: xสน, y: yสน } = getCenter(iสน, jสน);
fill(0);
if (!c && (rowสนสน[iสนสน] || rowสนสน[iสนสน + 1])) {
c = "โ€ข-".charAt(i & 1);
fill([DASH_COLOR, DOT_COLOR][i & 1]);
}
text(c, x, y);
if (c && j > 0) {
let w = textWidth(c);
let wสน = textWidth(cสน || "");
let s = sign(xสน - x);
fill(s < 0 ? DOT_COLOR : DASH_COLOR);
let x0 = xสน - (s * wสน) / 2;
let x1 = x + (s * w) / 2;
arrow(x0, yสน + 5, sign(x0 - x1) === s ? x1 : x, y - 25, !(i & 1));
}
});
});
}
function mousePressed() {
includeEmoji = !includeEmoji;
redraw();
}
function windowResized() {
resizeCanvas(windowWidth - 10, windowHeight - 10);
redraw();
}
function arrow(x0, y0, x1, y1, dashed) {
let ll = dist(x0, y0, x1, y1); // line length
let sl = ll - 10; // stem length
let hw = 5; // line half-width
let pts = [
[0, hw],
[sl, hw],
[sl, hw + 5],
[ll, 0],
];
push();
translate(x0, y0);
rotate(atan2(y1 - y0, x1 - x0));
noStroke();
beginShape();
pts.forEach(([x, y]) => vertex(x, y));
pts.reverse().forEach(([x, y]) => vertex(x, -y));
endShape();
if (dashed) {
fill(255);
for (let x = 10; x < sl; x += 10) {
rect(x, -hw, -2, 2 * hw);
}
}
pop();
}
const sign = (x) => (x < 0 ? -1 : x > 0 ? 1 : 0);
const getCenter = (i, j) => {
let rowCount = rows.length;
return {
x: map((i + 0.5) * 2 ** (rowCount - j), -1, 2 ** rowCount + 1, 0, width),
y: min(100 + 80 * j, map(j, 0, rowCount - 0.5, 100, height)),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment