我正在制作一个程序,将文本转换为莫尔斯电码音频.
说我输入sos.我的程序将把它变成数组[1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1].哪里s = dot dot dot(或1,1,1),和o = dash dash dash(或2,2,2).这部分很容易.
接下来,我有两个声音文件:
var dot = new Audio('dot.mp3');
var dash = new Audio('dash.mp3');
Run Code Online (Sandbox Code Playgroud)
我的目标是有一个功能,dot.mp3当它看到a时1,dash.mp3当它看到a时会播放,并在看到a 2时暂停0.
以下类型/有时/有时可行,但我认为它存在根本缺陷,我不知道如何修复它.
function playMorseArr(morseArr) {
for (let i = 0; i < morseArr.length; i++) {
setTimeout(function() {
if (morseArr[i] === 1) {
dot.play();
}
if (morseArr[i] === …Run Code Online (Sandbox Code Playgroud) 我在 javascript 中使用递归洪水填充算法,我不确定如何避免超过最大调用堆栈大小。这是一个在浏览器中运行的小项目。
我从这里得到了这个想法:https ://guide.freecodecamp.org/algorithms/flood-fill/
我选择这个算法是因为它很容易理解,而且到目前为止我喜欢它,因为它非常快。
x和y是从左上角开始的二维坐标,targetColor并且newColor每个都是 a Uint8ClampedArray,并且id = ctx.createImageData(1,1);从 获取其信息newColor。
function floodFill2(x, y, targetColor, newColor, id) {
let c = ctx.getImageData(x, y, 1, 1).data;
// if the pixel doesnt match the target color, end function
if (c[0] !== targetColor[0] || c[1] !== targetColor[1] || c[2] !== targetColor[2]) {
return;
}
// if the pixel is already the newColor, exit function
if (c[0] === newColor[0] && …Run Code Online (Sandbox Code Playgroud)