Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
¿Por qué mi código no repite la animación correctamente con Javascript y lienzo HTML5?

Soy muy nuevo en la codificación y he estado tratando de descifrar animaciones con Javascript y HTML5. Tengo esta "barra de carga" como animación donde un rectángulo se expande hasta que llena el lienzo. La idea es que una vez tapada la lona se despeje la caja y se vuelva a empezar. En cambio, el rectángulo llena el lienzo y comienza a parpadear. ¿Algunas ideas?

 window.onload = function() { var wipeWidth = 0 var canvas = document.getElementById('canvas'); var context = canvas.getContext("2d"); console.log(context); function drawRect() { context.clearRect(0,0,300,150) context.fillStyle = "#305ef2"; context.rect(0, 0, wipeWidth, 150); context.fill() wipeWidth += 10 if(wipeWidth > 300) { wipeWidth = 0; } } setInterval(drawRect,50) }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Olvidaste cerrar el camino. Puede usar fillRect en su lugar para evitar esto.

 window.onload = function() { var wipeWidth = 0 var canvas = document.getElementById('canvas'); var context = canvas.getContext("2d"); console.log(context); function drawRect() { context.clearRect(0, 0, 300, 150) context.fillStyle = "#305ef2"; // this context.beginPath() context.rect(0, 0, wipeWidth, 150); context.fill() context.closePath() // or just this // context.fillRect(0, 0, wipeWidth, 150); wipeWidth += 10 if (wipeWidth > 300) { wipeWidth = 0; } } setInterval(drawRect, 50) }
 <canvas id="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Report

0

fillRect , requestAnimationFrame

 var wipeWidth = 0 var canvas = document.getElementById('canvas'); var context = canvas.getContext("2d"); var direction = +10; function drawRect() { context.clearRect(0, 0, canvas.width, canvas.height); context.fillStyle = "#305ef2"; context.fillRect(0, 0, wipeWidth, 150); wipeWidth += direction if (wipeWidth > canvas.width || wipeWidth < 0) { direction *= -1; } requestAnimationFrame(drawRect) } drawRect()
 canvas { background: gray; }
 <canvas id="canvas" width="600" height="50"></canvas>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!