Javascript jQuery:使字符串中的字母闪烁
我想用Canvas做一个textadventure,解析器应该像Dos控制台中的解析器一样永久闪烁。解析器保存为全局变量。我应该怎么做才能通过jQuery永久地更改全局变量的字符Javascript jQuery:使字符串中的字母闪烁,javascript,jquery,canvas,Javascript,Jquery,Canvas,我想用Canvas做一个textadventure,解析器应该像Dos控制台中的解析器一样永久闪烁。解析器保存为全局变量。我应该怎么做才能通过jQuery永久地更改全局变量的字符 var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), string = '', //Empty string which combines command, parser and '&
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
string = '', //Empty string which combines command, parser and '>' character
start_cmd ='>', //The character '>' starts before the command
command = '', //Typed characters
parser = '_'; //Parser which appears at the end of 'command'
$(document).on('keypress', function(event) {
string = start_cmd + command + parser;
$(document).clear(); //Self declared function which makes the whole canvas white.
$(document).drawConsole(); //Self declared function which draws the console.
$(document).println(string, 10, 550);//Self declared function which writes what you last typed in the keyboard.
switch (event.which)
{
case 8:
command = command.slice(0, -1);
$(document).clear();
$(document).drawConsole();
break;
case 13:
command = '';
$(document).clear();
$(document).drawConsole();
break;
default:
if (command.length < 46)
{
$(document).clear();
$(document).drawConsole();
command += String.fromCharCode(event.which);
}
break;
}
string = start_cmd + command + parser;
$(document).println(string, 10, 550);
console.log(event.which);
});
});
var canvas=document.getElementById('canvas'),
context=canvas.getContext('2d'),
string='',//组合了命令、解析器和'>'字符的空字符串
start_cmd='>',//字符'>'在命令之前开始
命令=“”,//键入的字符
解析器=''//出现在“命令”末尾的解析器
$(文档).on('keypress',函数(事件){
string=start\u cmd+command+parser;
$(document).clear();//使整个画布变白的自声明函数。
$(document).drawConsole();//绘制控制台的自声明函数。
$(document).println(string,10550);//自声明函数,用于写入上次在键盘上键入的内容。
开关(event.which)
{
案例8:
command=command.slice(0,-1);
$(document.clear();
$(document.drawConsole();
打破
案例13:
命令=“”;
$(document.clear();
$(document.drawConsole();
打破
违约:
如果(命令长度<46)
{
$(document.clear();
$(document.drawConsole();
command+=String.fromCharCode(event.which);
}
打破
}
string=start\u cmd+command+parser;
$(文档).println(字符串,10550);
console.log(event.which);
});
});
实现此功能的最佳位置可能是在println()方法中。您可以创建一个从字符串中删除最后一个字符的间隔,该字符将始终是\uuu
,或者添加它
let visible=true;
设置间隔(()=>{
设t=document.getElementById('foo');
可见=!可见;
t、 innerText=可见?t.innerText.trim().slice(0,-1):(t.innerText+“”);
}, 200);代码>
福
您已经尝试了什么?到目前为止,您的代码是什么样子的?你遇到了什么问题?请尝试解决这些问题,以便其他人能够帮助您。您可以向我们展示一些代码吗?因此,我以一种缩写形式添加了我的代码。自声明函数与解析器变量无关。