javascript - 我如何将我的绘制图像与我的填充矩形重叠

标签 javascript html canvas html5-canvas

当我将鼠标悬停在城市上时,我尝试在丹麦 map 上设置信息。 我试图弄清楚如何在我的绘图图像上设置我的 fillRect ,因为它在它后面,以及如何在将鼠标悬停在它上面后设置事件。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>

              <script type="text/javascript">
                      // add map to div               
                      function startCanvas() {
                      var c = document.getElementById("myCanvas"); //get element by id
                      var ctx = c.getContext("2d");



                      //add new image to canavas
                      var image = new Image();

                      image.onload = function () {

                          // draw image
                          ctx.drawImage(image, 69, 50); 

                      };

                      // the image file, want this image to be set to back
                      image.src = 'denmark.jpg';



                           // draw rectangle, want this to be st to front
                      ctx.fillStyle = "#FF0000";
                      ctx.fillRect(0, 0, 150, 75);


                  }





          </script>

        </head>
        <body onload="startCanvas()">

    //canvas
        <canvas id="myCanvas" width="600" height="600";">
        Your browser does not support the HTML5 canvas tag.</canvas>


        </body>
        </html>

最佳答案

如果你想让它在前面,你需要在图像后面绘制矩形。尝试这个:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>

          <script type="text/javascript">
                  // add map to div               
                  function startCanvas() {
                  var c = document.getElementById("myCanvas"); //get element by id
                  var ctx = c.getContext("2d");



                  //add new image to canavas
                  var image = new Image();

                  image.onload = function () {

                      // draw image
                      ctx.drawImage(image, 69, 50); 
                       // draw rectangle, want this to be st to front
                      ctx.fillStyle = "#FF0000";
                      ctx.fillRect(0, 0, 150, 75);

                  };

                  // the image file, want this image to be set to back
                  image.src = 'denmark.jpg';





              }





      </script>

    </head>
    <body onload="startCanvas()">

//canvas
    <canvas id="myCanvas" width="600" height="600";">
    Your browser does not support the HTML5 canvas tag.</canvas>


    </body>
    </html>

关于javascript - 我如何将我的绘制图像与我的填充矩形重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867958/

相关文章:

javascript - 点击按钮后 CSS 和 javascript 就消失了

python - 如何在 Django 中重复调用views.py 中的函数?

javascript - 流畅的按键输入? Javascript 中的(持续的按键输入流)

javascript - 在 Canvas 中填充奇偶 react

javascript - jQuery - 切换多个图像并保持 div 高度

javascript - 在 tensorflow js api 0.15.3 中设置卷积层的配置

javascript - 带有 Webbrowser 控件和 Javascript 的 VBS 应用程序

javascript - jQuery根据页面滚动添加/删除类

html - CSS中心标题图片

javascript - 指定 infoVis JIT 图中节点之间的最小距离