Skip to content

Instantly share code, notes, and snippets.

@sjardim
Forked from fontanon/simulated-typing.html
Created June 27, 2023 18:33
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 sjardim/504e64a17ae52ff6c68b8d1818f1fe20 to your computer and use it in GitHub Desktop.
Save sjardim/504e64a17ae52ff6c68b8d1818f1fe20 to your computer and use it in GitHub Desktop.
Simulate typing on HTML Input Box with Javascript
<html>
<head>
<title>Simulated typing</title>
</head>
<body>
<input type="text" id="transcribe-searchbox" placeholder="Escribe Aquí">
<input type="button" id="transcribe-button" value="Transcibir a Andaluz">
</body>
<script>
// Quotes to simulate typing, then deletion
// The animation cycles between them.
var andaluh = [
"La comunidad del andaluz escrito",
"Aprende a escribir en andaluz",
"Es tu derecho, es tu lengua 😛",
"No ni ná 🤗"
];
// Miliseconds between each letter
// when typing/deleting. The higher, the slower.
var writeSpeed = 80;
var deleteSpeed = 30;
// Global var to control animation start/stop
var animate = true;
// If the user clicks on the searchbox => stop animation.
function stopAnimation(input) {
if (animate) {
input.value = "";
}
animate = false;
}
// Listen to click to stop animation
var transcribeSearchbox = document.querySelector("#transcribe-searchbox");
transcribeSearchbox.addEventListener("click", function(event) {
stopAnimation(transcribeSearchbox);
event.preventDefault();
});
// Listen to click to open transcriptor
var transcribeButton = document.querySelector("#transcribe-button");
transcribeButton.addEventListener("click", function(event) {
window.open('https://andaluh.es/transcriptor/#/?text=' + encodeURIComponent(transcribeSearchbox.value))
event.preventDefault();
});
// Main function
function startAnimation(quotes, writeSpeed=100, deleteSpeed=50) {
// Transcribe when Enter is pressed
transcribeSearchbox.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
window.open('https://andaluh.es/transcriptor/#/?text=' + encodeURIComponent(transcribeSearchbox.value))
}
});
// Intialize
transcribeSearchbox.select();
transcribeSearchbox.value="";
// Simulates text deletion
var delete_text = function(quoteIndex, charIndex) {
var quote = quotes[quoteIndex];
var l = quote.length;
if (animate) {
transcribeSearchbox.value = transcribeSearchbox.value.slice(0, -1); // Remove last character
if(charIndex > 0) {
charIndex--;
setTimeout(function() {
delete_text(quoteIndex, charIndex)
}, deleteSpeed);
} else { // Cycle between quotes
if (quoteIndex == quotes.length -1) {
var newQuote = 0; // Start from beginning
} else {
var newQuote = quoteIndex + 1; // Next quote
}
setTimeout(function() {
write_text(newQuote, charIndex)
}, writeSpeed);
}
}
}
// Simulates text typing
var write_text = function(quoteIndex, charIndex) {
var quote = quotes[quoteIndex];
var l = quote.length;
if (animate) {
transcribeSearchbox.value += quote[charIndex]; // Type next character
if(charIndex < l-1) {
charIndex++;
setTimeout(function() {
write_text(quoteIndex, charIndex)
}, writeSpeed);
} else { // Start text deletion simulation
setTimeout(function() {
delete_text(quoteIndex, charIndex)
}, 20 * writeSpeed); // Delay before delete
}
}
}
// Start animation
setTimeout(function() { write_text(0, 0) }, writeSpeed);
}
// Start!
startAnimation(andaluh, writeSpeed, deleteSpeed);
</script>
</html>
@sjardim
Copy link
Author

sjardim commented Jun 27, 2023

// Simplify to only type and not delete.

var mytext = [
  "La comunidad del andaluz escrito. \n Aprende a escribir en andaluz. Es tu derecho, es tu lengua"
];

// Miliseconds between each letter
// when typing/deleting. The higher, the slower.
var writeSpeed = 80;

// Global var to control animation start/stop
var animate = true;


// Listen to click to stop animation
var transcribeSearchbox = document.getElementById("comment");

// Main function
function startAnimation(quotes, writeSpeed=100) {
  

  // Intialize
  transcribeSearchbox.select();
  transcribeSearchbox.value="";


  // Simulates text typing
  var write_text = function(quoteIndex, charIndex) {
    var quote = quotes[quoteIndex];
    var l = quote.length;

    if (animate) {
      transcribeSearchbox.value += quote[charIndex]; // Type next character

      if(charIndex < l-1) {
        charIndex++;
        setTimeout(function() {
          write_text(quoteIndex, charIndex)
        }, writeSpeed);
      }
    }
  }

  // Start animation
  setTimeout(function() { write_text(0, 0) }, writeSpeed);
}

// Start!
startAnimation(mytext, writeSpeed);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment