p5((s) => {
let typedKey = "";
let fontPath;
let spacing = 100;
let spaceWidth = 100;
let fontSize = 200;
let lineSpacing = fontSize * 1.2;
let textW = 0;
let letterX = 10;
let letterY = lineSpacing;
let stepSize = 2;
let distortion = 1;
let font;
let points;
s.preload = function () {
font = s.loadFont(fontUrl);
};
s.setup = function () {
s.createCanvas(width, height);
s.textFont(font);
points = getPoints(typedKey, letterX, letterY);
};
s.draw = function () {
s.noFill();
s.strokeWeight(0.1);
s.stroke("blue");
s.translate(letterX, letterY);
s.beginShape();
for (let i = 0; i < points.length; i++) {
let pt = points[i];
pt.x += s.random(-stepSize, stepSize) * distortion;
pt.y += s.random(-stepSize, stepSize) * distortion;
s.vertex(pt.x, pt.y);
s.ellipse(pt.x, pt.y, 7, 7);
}
s.endShape();
};
s.keyPressed = function () {
switch (s.keyCode) {
case s.ENTER:
case s.RETURN:
typedKey = "";
points = getPoints(typedKey);
letterY += lineSpacing;
letterX = 50;
case s.BACKSPACE:
case s.DELETE:
s.background(255);
typedKey = "";
points = getPoints(typedKey);
letterX = 50;
letterY = lineSpacing;
}
};
s.keyTyped = function () {
if (s.keyCode >= 32) {
if (s.keyCode == 32) {
typedKey = "";
letterX += textW + spaceWidth;
points = getPoints(typedKey);
} else {
typedKey = s.key;
letterX += textW + spacing;
points = getPoints(typedKey);
}
}
};
function getPoints(typedKey, x, y) {
let result = [];
let path = font.textToPoints(typedKey, x, y, fontSize, {
sampleFactor: 0.1
});
for (let i = 0; i < path.length; i++) {
result.push(s.createVector(path[i].x, path[i].y));
}
return result;
}
})