Absolutely! Let's make sure the text starts scrolling from the center of the screen. We need to adjust the initial position of the text so that it appears in the middle of the screen before it starts scrolling.
Here's the updated code:
✅ 1. ✅ Updated JavaScript (script.js)
We need to adjust the initial position of the text so that it starts from the center of the screen.
javascript Copy code
// --- Variables ---
let speed = 1;
let fontSize = 30;
let fontColor = "#ffffff";
let bgColor = "#000000";
let isScrolling = false;
let isPaused = false;
let animationFrameId = null;
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');
// --- Initial state ---
let currentPosition = 0; // Start from the center
let startTime = 0;
let elapsed = 0;
// --- 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;
}
// --- 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();
currentPosition = window.innerWidth / 2; // Start from the center
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 = `translateX(${window.innerWidth / 2}px)`;
currentPosition = window.innerWidth / 2;
}
// --- Scroll animation loop ---
function scrollStep(timestamp) {
if (!startTime) startTime = timestamp;
elapsed = timestamp - startTime;
if (!isScrolling) {
animationFrameId = null;
return;
}
const textWidth = textElement.offsetWidth;
currentPosition -= speed;
if (currentPosition < -textWidth / 2) {
currentPosition = window.innerWidth / 2;
}
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.");
✅ 2. ✅ Updated CSS (styles.css)
Ensure the text starts from the center of the screen.
css Copy code
/* Global styles */
body {
margin: 0;
padding: 0;
font-family: sans-serif;
height: 100vh;
background-color: #222;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
}
/* Scrolling text area */
.teleprompter {
overflow: hidden;
white-space: nowrap;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
background-color: #000;
z-index: 100;
box-sizing: border-box;
}
.teleprompter p {
margin: 0;
position: absolute;
will-change: transform;
left: 50%; /* Start from horizontal center */
transform: translateX(-50%); /* Center the text initially */
white-space: nowrap;
}
/* Control buttons below teleprompter */
.controls {
margin-top: 60px; /* Space below teleprompter */
display: flex;
gap: 15px;
justify-content: center;
z-index: 99;
}
.controls button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#start { background-color: #4CAF50; color: white; }
#start:hover { background-color: #45a049; }
#pause { background-color: #ffcc00; color: black; }
#pause:hover { background-color: #e6b800; }
#reset { background-color: #f44336; color: white; }
#reset:hover { background-color: #da190b; }
/* Settings panel */
.settings {
margin-top: 30px;
display: flex;
flex-wrap: wrap;
gap: 15px;
justify-content: center;
align-items: center;
z-index: 99;
}
.settings label {
margin-right: 5px;
}
.settings input[type="number"],
.settings input[type="color"] {
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
.settings input[type="number"] {
width: 60px;
}
/* Text input area */
.input-area {
margin-top: 40px;
width: 80%;
max-width: 600px;
display: flex;
flex-direction: column;
align-items: center;
}
textarea {
width: 100%;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
✅ Final Behavior
Buttons : "Start", "Pause", and "Reset" are centered directly under the scrolling text.
Settings : Below the buttons, still centered.
Text Animation : Starts from the center of the screen , scrolls left, and loops.
Resume : Clicking "Start" again resumes from where it was paused.
This should ensure that the text starts scrolling from the center of the screen and behaves as expected. If you need any further adjustments or additional features, feel free to ask! 😊