Update HTML5 canvas rectangle on hover?

后端 未结 8 746
暗喜
暗喜 2020-12-03 01:57

I\'ve got some code which draws a rectangle on a canvas, but I want that rectangle to change color when I hover the mouse over it.

The problem is after I\'ve drawn t

相关标签:
8条回答
  • 2020-12-03 02:19

    You could use canvas.addEventListener

    var canvas = document.getElementById('canvas0');
    canvas.addEventListener('mouseover', function() { /*your code*/ }, false);
    

    It worked on google chrome

    0 讨论(0)
  • 2020-12-03 02:20

    Consider this following code:

    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.rect(20,20,150,100);
    ctx.stroke();
    
    c.addEventListener("mouseover", doMouseOver, false);//added event to canvas
    
    function doMouseOver(e){
        ctx.fillStyle = 'red';
        ctx.fill();
    }
    

    DEMO

    0 讨论(0)
  • 2020-12-03 02:21

    You can't do this out-of-the-box with canvas. Canvas is just a bitmap, so the hover logic has to be implemented manually.

    Here is how:

    • Store all the rectangles you want as simple object
    • For each mouse move on the canvas element:
      • Get mouse position
      • Iterate through the list of objects
      • use isPointInPath() to detect a "hover"
      • Redraw both states

    Example

    var canvas = document.querySelector("canvas"),
        ctx = canvas.getContext("2d"),
        rects = [
            {x: 10, y: 10, w: 200, h: 50},
            {x: 50, y: 70, w: 150, h: 30}    // etc.
        ], i = 0, r;
    
    // render initial rects.
    while(r = rects[i++]) ctx.rect(r.x, r.y, r.w, r.h);
    ctx.fillStyle = "blue"; ctx.fill();
    
    canvas.onmousemove = function(e) {
    
      // important: correct mouse position:
      var rect = this.getBoundingClientRect(),
          x = e.clientX - rect.left,
          y = e.clientY - rect.top,
          i = 0, r;
      
      ctx.clearRect(0, 0, canvas.width, canvas.height); // for demo
       
      while(r = rects[i++]) {
        // add a single rect to path:
        ctx.beginPath();
        ctx.rect(r.x, r.y, r.w, r.h);    
        
        // check if we hover it, fill red, if not fill it blue
        ctx.fillStyle = ctx.isPointInPath(x, y) ? "red" : "blue";
        ctx.fill();
      }
    
    };
    <canvas/>

    0 讨论(0)
  • 2020-12-03 02:24

    Below code adds shadow to canvas circle on hovering it.

    <html>
    
    <body>
      <canvas id="myCanvas" width="1000" height="500" style="border:1px solid #d3d3d3;">
        Your browser does not support the HTML5 canvas tag.</canvas>
    </body>
    
    <script>
      var canvas = document.getElementById("myCanvas"),
        ctx = canvas.getContext("2d"),
        circle = [{
            x: 60,
            y: 50,
            r: 40,
          },
          {
            x: 100,
            y: 150,
            r: 50,
          } // etc.
        ];
    
      // render initial rects.
      for (var i = 0; i < circle.length; i++) {
    
        ctx.beginPath();
        ctx.arc(circle[i].x, circle[i].y, circle[i].r, 0, 2 * Math.PI);
        ctx.fillStyle = "blue";
        ctx.fill();
      }
    
      canvas.onmousemove = function(e) {
        var x = e.clientX,
          y = e.clientY,
          i = 0,
          r;
    
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    
        for (let i = 0; i < circle.length; i++) {
          if ((x > circle[i].x - circle[i].r) && (y > circle[i].y - circle[i].r) && (x < circle[i].x + circle[i].r) && (y < circle[i].y + circle[i].r)) {
    
    
            ctx.beginPath();
            ctx.arc(circle[i].x, circle[i].y, circle[i].r, 0, 2 * Math.PI);
            ctx.fillStyle = "blue";
            ctx.fill();
            ctx.shadowBlur = 10;
            ctx.lineWidth = 3;
            ctx.strokeStyle = 'rgb(255,255,255)';
            ctx.shadowColor = 'grey';
            ctx.stroke();
            ctx.shadowColor = 'white';
            ctx.shadowBlur = 0;
    
          } else {
    
            ctx.beginPath();
            ctx.arc(circle[i].x, circle[i].y, circle[i].r, 0, 2 * Math.PI);
            ctx.fillStyle = "blue";
            ctx.fill();
            ctx.shadowColor = 'white';
            ctx.shadowBlur = 0;
          }
    
        }
    
      };
    </script>
    
    </html>

    0 讨论(0)
  • 2020-12-03 02:35

        var c=document.getElementById("myCanvas");
        var ctx=c.getContext("2d");
        ctx.rect(20,20,150,100);
        ctx.stroke();
        
        $(c).hover(function(e){
            ctx.fillStyle = 'red';
            ctx.fill();
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <canvas id="myCanvas"/>

    0 讨论(0)
  • 2020-12-03 02:37

    This is a stable code in base of @K3N answer. The basic problem of his code is because when one box is over the another the two may get mouse hover at same time. My answer perfectly solves that adding a 'DESC' to 'ASC' loop.

    var canvas = document.getElementById("canvas"),
        ctx = canvas.getContext("2d");
    
    var map = [
        {x: 20, y: 20, w: 60, h: 60},
        {x: 30, y: 50, w: 76, h: 60}
    ];
    
    var hover = false, id;
    var _i, _b;
    function renderMap() {
        for(_i = 0; _b = map[_i]; _i ++) {
            ctx.fillStyle = (hover && id === _i) ? "red" : "blue";
            ctx.fillRect(_b.x, _b.y, _b.w, _b.h);
        }
    }
    // Render everything
    renderMap();
    canvas.onmousemove = function(e) {
        // Get the current mouse position
        var r = canvas.getBoundingClientRect(),
            x = e.clientX - r.left, y = e.clientY - r.top;
        hover = false;
    
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    
        for(var i = map.length - 1, b; b = map[i]; i--) {
            if(x >= b.x && x <= b.x + b.w &&
               y >= b.y && y <= b.y + b.h) {
                // The mouse honestly hits the rect
                hover = true;
                id = i;
                break;
            }
        }
        // Draw the rectangles by Z (ASC)
        renderMap();
    }
    <canvas id="canvas"></canvas>

    0 讨论(0)
提交回复
热议问题