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

181
Views
Mouseover solo activo una vez que estoy en mi lienzo y no en mi forma

soy un principiante que usa canvas y también javascript y estoy tratando de usar el mouseover. Tengo que hacer que funcione un poco, pero solo cuando el mouse está sobre el lienzo, me gustaría hacer que mi cuadrado cambie de color una vez que mi mouse esté sobre él.

¿Cómo puedo hacer que funcione?

Aquí está mi código:

 window.onload = function() { canvasEvent(); } function canvasEvent() { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.rect(150, 150, 100, 100); ctx.fillStyle = "blue"; ctx.fill(); ctx.stroke(); c.addEventListener("mouseover", hover, false); c.addEventListener("mouseout", hoverOut, false); } function hover(e) { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.fillStyle = 'green'; ctx.fill(); ctx.stroke(); } function hoverOut(e) { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.fillStyle = 'blue'; ctx.fill(); }
 canvas#canvas{ background-color:#DDDDDD; display: block; margin: 0 auto; }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>TEST</title> <script type="text/javascript" src="script.js"></script> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <center> <canvas id="canvas" width="550" height="400"></canvas> </center> </body> </html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

solo verifica si el mouse está en el rectángulo

 window.onload = function() { canvasEvent(); } function canvasEvent() { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.rect(150, 150, 100, 100); ctx.fillStyle = "blue"; ctx.fill(); ctx.stroke(); c.addEventListener("mousemove", e => { if ( e.offsetX >=150 && e.offsetX <= 250 && e.offsetY >= 150 && e.offsetY<=250){ hover(); } else{ hoverOut(); } }); } function hover(e) { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.fillStyle = 'green'; ctx.fill(); ctx.stroke(); } function hoverOut(e) { var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.fillStyle = 'blue'; ctx.fill(); }
 canvas#canvas{ background-color:#DDDDDD; display: block; margin: 0 auto; }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>TEST</title> <script type="text/javascript" src="script.js"></script> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <center> <canvas id="canvas" width="550" height="400"></canvas> </center> </body> </html>

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!