type-racer / index.html
andreaschandra's picture
Add 3 files
78ba5af verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typing Speed Test</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.typing-text {
background-color: rgba(243, 244, 246, 0.5);
border-radius: 0.5rem;
padding: 1.5rem;
font-size: 1.25rem;
line-height: 1.75rem;
min-height: 150px;
position: relative;
}
.typing-text span {
position: relative;
}
.typing-text span.active {
background-color: #3b82f6;
color: white;
border-radius: 0.25rem;
}
.typing-text span.correct {
color: #10b981;
}
.typing-text span.incorrect {
color: #ef4444;
text-decoration: underline;
}
.timer-progress {
height: 8px;
transition: width 0.1s linear;
}
.result-card {
transform: scale(0.95);
opacity: 0;
transition: all 0.3s ease-out;
}
.result-card.show {
transform: scale(1);
opacity: 1;
}
.typing-input {
opacity: 0;
position: absolute;
left: -9999px;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4">
<div class="w-full max-w-3xl mx-auto">
<h1 class="text-4xl font-bold text-center text-blue-600 mb-2">
<i class="fas fa-keyboard mr-2"></i>Typing Speed Test
</h1>
<p class="text-center text-gray-600 mb-8">Test your typing speed in different time intervals</p>
<div class="bg-white rounded-xl shadow-lg p-6 mb-6">
<div class="flex flex-wrap justify-center gap-4 mb-6">
<button id="30s" class="time-btn px-6 py-2 rounded-full bg-blue-100 text-blue-600 font-medium hover:bg-blue-200 transition">
<i class="far fa-clock mr-2"></i>30 Seconds
</button>
<button id="45s" class="time-btn px-6 py-2 rounded-full bg-blue-100 text-blue-600 font-medium hover:bg-blue-200 transition">
<i class="far fa-clock mr-2"></i>45 Seconds
</button>
<button id="60s" class="time-btn px-6 py-2 rounded-full bg-blue-100 text-blue-600 font-medium hover:bg-blue-200 transition">
<i class="far fa-clock mr-2"></i>60 Seconds
</button>
</div>
<div class="mb-4 flex justify-between items-center">
<div class="text-gray-600">
<span id="timer" class="text-2xl font-bold text-blue-600">00:00</span>
</div>
<div class="text-gray-600">
<span id="wpm" class="text-2xl font-bold text-blue-600">0</span> WPM
</div>
</div>
<div class="timer-progress bg-gray-200 rounded-full mb-6">
<div id="progress-bar" class="bg-blue-500 rounded-full h-full" style="width: 100%"></div>
</div>
<div class="typing-text mb-6" id="typingText">
Click on a time option above to start the test. The text will appear here when you begin.
</div>
<input type="text" id="typingInput" class="typing-input" autocomplete="off">
<div class="text-center">
<button id="startBtn" class="px-8 py-3 bg-blue-600 text-white font-bold rounded-lg hover:bg-blue-700 transition">
<i class="fas fa-play mr-2"></i>Start Test
</button>
<button id="retryBtn" class="px-8 py-3 bg-gray-200 text-gray-700 font-bold rounded-lg hover:bg-gray-300 transition ml-4 hidden">
<i class="fas fa-redo mr-2"></i>Try Again
</button>
</div>
</div>
<div id="resultCard" class="result-card bg-white rounded-xl shadow-lg p-6 w-full max-w-3xl mx-auto">
<h2 class="text-2xl font-bold text-center text-blue-600 mb-4">Your Results</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<div class="bg-blue-50 p-4 rounded-lg text-center">
<div class="text-4xl font-bold text-blue-600 mb-2" id="resultWPM">0</div>
<div class="text-gray-600">Words Per Minute</div>
</div>
<div class="bg-green-50 p-4 rounded-lg text-center">
<div class="text-4xl font-bold text-green-600 mb-2" id="resultAccuracy">0%</div>
<div class="text-gray-600">Accuracy</div>
</div>
<div class="bg-purple-50 p-4 rounded-lg text-center">
<div class="text-4xl font-bold text-purple-600 mb-2" id="resultChars">0</div>
<div class="text-gray-600">Characters Typed</div>
</div>
</div>
<div class="mt-6 text-center">
<button id="shareBtn" class="px-6 py-2 bg-blue-100 text-blue-600 font-medium rounded-full hover:bg-blue-200 transition">
<i class="fas fa-share-alt mr-2"></i>Share Your Score
</button>
</div>
</div>
</div>
<script>
// Sample texts for typing test
const sampleTexts = [
"The quick brown fox jumps over the lazy dog. This sentence contains all the letters in the English alphabet. Typing is an essential skill in today's digital world.",
"Programming is the process of creating a set of instructions that tell a computer how to perform a task. It requires logical thinking and problem-solving skills.",
"The best way to predict the future is to invent it. Technology has transformed our lives in countless ways, making communication faster and more efficient.",
"Practice makes perfect. Regular typing practice can significantly improve your speed and accuracy over time. Consistency is key to mastering any skill.",
"The internet has revolutionized how we access information. With just a few keystrokes, we can find answers to almost any question imaginable."
];
// DOM elements
const typingText = document.getElementById('typingText');
const typingInput = document.getElementById('typingInput');
const timer = document.getElementById('timer');
const wpmDisplay = document.getElementById('wpm');
const progressBar = document.getElementById('progressBar');
const startBtn = document.getElementById('startBtn');
const retryBtn = document.getElementById('retryBtn');
const timeButtons = document.querySelectorAll('.time-btn');
const resultCard = document.getElementById('resultCard');
const resultWPM = document.getElementById('resultWPM');
const resultAccuracy = document.getElementById('resultAccuracy');
const resultChars = document.getElementById('resultChars');
const shareBtn = document.getElementById('shareBtn');
// Variables
let timeLeft = 0;
let timerInterval;
let totalTime = 0;
let isTyping = false;
let correctChars = 0;
let totalTyped = 0;
let currentText = '';
let currentIndex = 0;
let startTime;
let wpm = 0;
// Initialize
function init() {
typingInput.value = '';
typingInput.focus();
currentIndex = 0;
correctChars = 0;
totalTyped = 0;
wpm = 0;
updateWPM();
// Generate random text
currentText = sampleTexts[Math.floor(Math.random() * sampleTexts.length)];
// Display text with spans
typingText.innerHTML = '';
currentText.split('').forEach((char, index) => {
const span = document.createElement('span');
span.textContent = char;
span.id = `char-${index}`;
typingText.appendChild(span);
});
// Highlight first character
document.getElementById(`char-0`).classList.add('active');
}
// Start test
function startTest(seconds) {
if (isTyping) return;
totalTime = seconds;
timeLeft = seconds;
isTyping = true;
startTime = new Date().getTime();
init();
// Update timer display
updateTimer();
// Start timer
timerInterval = setInterval(() => {
timeLeft--;
updateTimer();
if (timeLeft <= 0) {
endTest();
}
}, 1000);
// UI changes
startBtn.classList.add('hidden');
retryBtn.classList.remove('hidden');
resultCard.classList.remove('show');
typingInput.focus();
}
// Update timer display
function updateTimer() {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timer.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// Update progress bar
const progressPercent = (timeLeft / totalTime) * 100;
progressBar.style.width = `${progressPercent}%`;
// Change color when time is running out
if (progressPercent < 30) {
progressBar.classList.remove('bg-blue-500');
progressBar.classList.add('bg-red-500');
} else {
progressBar.classList.remove('bg-red-500');
progressBar.classList.add('bg-blue-500');
}
}
// Update WPM display
function updateWPM() {
wpmDisplay.textContent = Math.round(wpm);
}
// Calculate WPM
function calculateWPM() {
if (!startTime) return 0;
const now = new Date().getTime();
const timeElapsed = (now - startTime) / 1000 / 60; // in minutes
const wordsTyped = correctChars / 5; // standard word length
return wordsTyped / timeElapsed || 0;
}
// Handle typing input
typingInput.addEventListener('input', (e) => {
if (!isTyping) return;
const inputChar = typingInput.value;
const currentChar = currentText[currentIndex];
// Remove active class from previous character
document.getElementById(`char-${currentIndex}`).classList.remove('active');
// Check if character is correct
if (inputChar === currentChar) {
document.getElementById(`char-${currentIndex}`).classList.add('correct');
correctChars++;
} else {
document.getElementById(`char-${currentIndex}`).classList.add('incorrect');
}
totalTyped++;
currentIndex++;
typingInput.value = '';
// Add active class to next character
if (currentIndex < currentText.length) {
document.getElementById(`char-${currentIndex}`).classList.add('active');
} else {
// If reached end of text, generate new text
currentText = sampleTexts[Math.floor(Math.random() * sampleTexts.length)];
typingText.innerHTML = '';
currentText.split('').forEach((char, index) => {
const span = document.createElement('span');
span.textContent = char;
span.id = `char-${index + currentIndex}`;
typingText.appendChild(span);
});
document.getElementById(`char-${currentIndex}`).classList.add('active');
}
// Calculate and update WPM
wpm = calculateWPM();
updateWPM();
});
// End test
function endTest() {
clearInterval(timerInterval);
isTyping = false;
// Calculate final results
const accuracy = Math.round((correctChars / totalTyped) * 100) || 0;
// Display results
resultWPM.textContent = Math.round(wpm);
resultAccuracy.textContent = `${accuracy}%`;
resultChars.textContent = totalTyped;
// Show result card
resultCard.classList.add('show');
// UI changes
typingInput.blur();
}
// Event listeners
startBtn.addEventListener('click', () => {
// Default to 60s if no time selected
const selectedTime = document.querySelector('.time-btn.active');
const time = selectedTime ? parseInt(selectedTime.id) : 60;
startTest(time);
});
retryBtn.addEventListener('click', () => {
const selectedTime = document.querySelector('.time-btn.active');
const time = selectedTime ? parseInt(selectedTime.id) : 60;
startTest(time);
});
timeButtons.forEach(button => {
button.addEventListener('click', () => {
timeButtons.forEach(btn => btn.classList.remove('bg-blue-600', 'text-white'));
button.classList.add('bg-blue-600', 'text-white');
});
});
shareBtn.addEventListener('click', () => {
if (navigator.share) {
navigator.share({
title: 'My Typing Test Results',
text: `I typed at ${resultWPM.textContent} WPM with ${resultAccuracy.textContent} accuracy! Can you beat my score?`,
url: window.location.href
}).catch(err => {
console.log('Error sharing:', err);
});
} else {
// Fallback for browsers that don't support Web Share API
const shareText = `I typed at ${resultWPM.textContent} WPM with ${resultAccuracy.textContent} accuracy! Try it yourself: ${window.location.href}`;
alert(shareText);
}
});
// Set default active time button
document.getElementById('60s').classList.add('bg-blue-600', 'text-white');
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=andreaschandra/type-racer" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>