I want the effect to run immediately when I call the function, but when I call the function I have to wait 3-4 seconds for the effect to work.
I use canvas in HTML
const canvas = document.querySelector("#draw-pad"); const ctx = canvas.getContext("2d"); function runTextAnimation(text) { var x = canvas.width / 2; var y = canvas.height / 2; var txt = text; var w = 0; var clearH = 40; var clearY = 5; var clearX = 8; ctx.font = 'bold 17px Arial'; ctx.lineWidth = 2; ctx.strokeStyle = 'black'; function runText() { if (w > 500) { ctx.clearRect(clearX, clearY, w, clearH); w = 0; } if (w === 0) { ctx.fillStyle = 'lightblue'; ctx.strokeText(txt, x, y); ctx.fillText(txt, x, y); ctx.fillStyle = 'red'; } ctx.save(); ctx.beginPath(); ctx.clearRect(clearX, clearY, w, clearH); ctx.rect(clearX, clearY, w, clearH); ctx.clip(); ctx.strokeStyle = 'white'; ctx.strokeText(txt, x, y); ctx.fillText(txt, x, y); ctx.restore(); w += 1.92; window.requestAnimationFrame(runText); } runText(); } runTextAnimation("chắc không phải một người như anh") <canvas id="draw-pad" width="578" height="50"></canvas> I want the effect to run immediately when I call the function, but when I call the function I have to wait 3-4 seconds for the effect to work.