KaBoom-font
我很喜欢这个范例,打字的时候字体不断改变,给人一种非常不稳定的感觉。
使用
loadFont()
进行内容的加载:默认暂时接受ascii码的内容,// default character mappings: (ASCII 32 - 126)
// const ASCII_CHARS = " !"#$%&'()*+,-./0123456789:;<=>?
//@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
// load a bitmap font called "04b03", with bitmap "04b03.png", each character on bitmap has a size of (6, 8), and contains default ASCII_CHARS
loadFont("04b03", "04b03.png", 6, 8);
// load a font with custom characters
loadFont("CP437", "CP437.png", 6, 8, "☺☻♥♦♣♠");
init()
初始化后开始的主体代码先创建fonts存储几个不同的,同样创建一个let变量存储目前内容
scene("main", () => {
const fonts = [
"unscii",
"04b03",
"proggy",
"CP437",
];
let curFont = 0;
const input = add([
text("123abc", 24, {
width: width(),
font: fonts[curFont],
}),
]);
charInput((ch) => {
input.text += ch; //增加text内容
curFont = (curFont + 1) % fonts.length;//当前字体切换
input.font = fonts[curFont];//当前字体应用
});
keyPressRep("enter", () => {
input.text += "\n";//增加换行符 \n
});
keyPressRep("backspace", () => {
input.text = input.text.substring(0, input.text.length - 1);
//删除最后一个字母
});
});
charInput(cb)
runs when an user inputs text 当进行输入的时候就会调用// similar to keyPress, but focused on text input
charInput((ch) => {
input.text += ch;
});
String.prototype.substring()
最近更新 1yr ago