Absolutely! The issue is that the text's left edge starts at the center, not the text's center. Let's fix this so the text is perfectly centered when it begins scrolling.
✅ Here's the fix:
1. Updated CSS (styles.css) - Minor adjustment
Change this part in .teleprompter p:
.teleprompter p {
margin: 0;
position: absolute;
will-change: transform;
left: 0; /* Changed from 50% to 0 for clearer positioning */
transform: translateX(0);
white-space: nowrap;
}
2. Updated JavaScript (script.js) - Calculate center properly
Replace your current script.js with this updated version:
// --- Variables ---
let speed = 1;
let fontSize = 30;
let fontColor = "#ffffff";
let bgColor = "#000000";
let isScrolling = false;
let isPaused = false;
let animationFrameId = null;
let currentPosition = 0;
const teleprompter = document.getElementById('teleprompter');
const textElement = document.getElementById('text');
const textInput = document.getElementById('textInput');
const speedInput = document.getElementById('speed');
const fontSizeInput = document.getElementById('fontSize');
const fontColorInput = document.getElementById('fontColor');
const bgColorInput = document.getElementById('bgColor');
const startButton = document.getElementById('start');
const pauseButton = document.getElementById('pause');
const resetButton = document.getElementById('reset');
// --- Update styles ---
function updateStyles() {
fontSize = parseInt(fontSizeInput.value);
fontColor = fontColorInput.value;
bgColor = bgColorInput.value;
speed = parseFloat(speedInput.value);
textElement.style.fontSize = `${fontSize}px`;
textElement.style.color = fontColor;
teleprompter.style.backgroundColor = bgColor;
}
// --- Calculate starting position (center of screen) ---
function getStartingPosition() {
const textWidth = textElement.offsetWidth;
const screenCenter = window.innerWidth / 2;
const textCenter = textWidth / 2;
// Position where text's center aligns with screen's center
return screenCenter - textCenter;
}
// --- Start scrolling ---
function startScrolling() {
const inputText = textInput.value.trim();
if (!inputText) {
alert("Please enter text to display.");
return;
}
if (!isScrolling && !isPaused) {
textElement.textContent = inputText;
updateStyles();
// Wait for next frame to ensure text dimensions are calculated
requestAnimationFrame(() => {
currentPosition = getStartingPosition();
textElement.style.transform = `translateX(${currentPosition}px)`;
});
}
if (!isScrolling) {
isScrolling = true;
isPaused = false;
cancelAnimationFrame(animationFrameId);
animationFrameId = requestAnimationFrame(scrollStep);
}
}
// --- Pause scrolling ---
function pauseScrolling() {
isPaused = true;
isScrolling = false;
}
// --- Reset everything ---
function resetScrolling() {
isPaused = false;
isScrolling = false;
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
textElement.textContent = "";
textElement.style.transform = "";
currentPosition = 0;
}
// --- Scroll animation loop ---
function scrollStep() {
if (!isScrolling) {
animationFrameId = null;
return;
}
const textWidth = textElement.offsetWidth;
currentPosition -= speed;
// When text completely leaves the left side, restart from center
if (currentPosition < -textWidth) {
currentPosition = getStartingPosition();
}
textElement.style.transform = `translateX(${currentPosition}px)`;
animationFrameId = requestAnimationFrame(scrollStep);
}
// --- Event listeners ---
speedInput.addEventListener('input', updateStyles);
fontSizeInput.addEventListener('input', updateStyles);
fontColorInput.addEventListener('input', updateStyles);
bgColorInput.addEventListener('input', updateStyles);
startButton.addEventListener('click', startScrolling);
pauseButton.addEventListener('click', pauseScrolling);
resetButton.addEventListener('click', resetScrolling);
// --- Initial setup ---
updateStyles();
speedInput.value = speed;
fontSizeInput.value = fontSize;
fontColorInput.value = fontColor;
bgColorInput.value = bgColor;
console.log("Teleprompter ready.");
🎯 Key changes:
getStartingPosition() function: Calculates the exact position where the text's center aligns with the screen's center
requestAnimationFrame wrapper: Ensures text dimensions are properly calculated before setting the initial position
- Loop reset: When text disappears on the left, it restarts from the center (not the right edge)
Now your text will start scrolling with its center perfectly aligned to the center of the screen! 🎉