• <legend id='rwEjK'><style id='rwEjK'><dir id='rwEjK'><q id='rwEjK'></q></dir></style></legend>

    <small id='rwEjK'></small><noframes id='rwEjK'>

  • <i id='rwEjK'><tr id='rwEjK'><dt id='rwEjK'><q id='rwEjK'><span id='rwEjK'><b id='rwEjK'><form id='rwEjK'><ins id='rwEjK'></ins><ul id='rwEjK'></ul><sub id='rwEjK'></sub></form><legend id='rwEjK'></legend><bdo id='rwEjK'><pre id='rwEjK'><center id='rwEjK'></center></pre></bdo></b><th id='rwEjK'></th></span></q></dt></tr></i><div id='rwEjK'><tfoot id='rwEjK'></tfoot><dl id='rwEjK'><fieldset id='rwEjK'></fieldset></dl></div>
    <tfoot id='rwEjK'></tfoot>
          <bdo id='rwEjK'></bdo><ul id='rwEjK'></ul>

        读取画布像素的 RGB 值

        时间:2023-06-20

        <small id='3fZVS'></small><noframes id='3fZVS'>

            <tbody id='3fZVS'></tbody>
          <tfoot id='3fZVS'></tfoot>
          • <bdo id='3fZVS'></bdo><ul id='3fZVS'></ul>
                1. <legend id='3fZVS'><style id='3fZVS'><dir id='3fZVS'><q id='3fZVS'></q></dir></style></legend>

                  <i id='3fZVS'><tr id='3fZVS'><dt id='3fZVS'><q id='3fZVS'><span id='3fZVS'><b id='3fZVS'><form id='3fZVS'><ins id='3fZVS'></ins><ul id='3fZVS'></ul><sub id='3fZVS'></sub></form><legend id='3fZVS'></legend><bdo id='3fZVS'><pre id='3fZVS'><center id='3fZVS'></center></pre></bdo></b><th id='3fZVS'></th></span></q></dt></tr></i><div id='3fZVS'><tfoot id='3fZVS'></tfoot><dl id='3fZVS'><fieldset id='3fZVS'></fieldset></dl></div>
                  本文介绍了读取画布像素的 RGB 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在画布中放置了一个图像,当用户将鼠标移到图像上时,我想获取该图像像素的 RGB 值.这是我写的代码:

                  I have put an image in canvas and I want to get the RGB value of the pixels of that image when the user moves the mouse over the image. Here is the code which I have written:

                  <canvas id="myCanvas" width="200" height="200" style="border: red;border-style: dotted">
                  Your browser does not support the canvas element.
                  </canvas>
                  <script>
                  var canvas = document.getElementById("myCanvas");
                  var context = canvas.getContext("2d");
                  
                  var destX = 0;
                  var destY = 0;
                  
                  var imageObj = new Image();
                  imageObj.onload = function()
                  {
                  context.drawImage(imageObj, destX, destY);
                  };
                  imageObj.src = "zain.jpg";
                  
                  canvas.onclick = function(e) {
                      var x = e.pageX;
                      var y = e.pageY;
                      var canvasColor = context.getImageData(x, y, 1,1); // rgba e [0,255]
                      var pixels = canvasColor.data;
                      var r = pixels[0];
                      var g = pixels[1];
                      var b = pixels[2];
                      document.body.style.backgroundColor = "rgb("+r+','+g+','+b+")";
                  }
                  

                  推荐答案

                  试试这个:

                  var color = document.getElementById("color");
                  var canvas = document.getElementById("myCanvas");
                  var context = canvas.getContext("2d");
                  
                  var imageObj = new Image();
                  imageObj.onload = function(){
                      context.drawImage(imageObj, destX, destY);
                  };
                  imageObj.src = "zain.jpg";
                  
                  canvas.onmousemove = function(e) {
                      // not so sure about these... might need to offset them or so
                      var x = e.x;
                      var y = e.y;
                  
                      // set color now
                      var canvasColor = context.getImageData(x, y, 1, 1).data; // rgba e [0,255]
                      var r = canvasColor[0];
                      var g = canvasColor[1];
                      var b = canvasColor[2];
                  
                      color.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
                  }
                  

                  请注意,该片段希望您在某处有一个 id 为color"的 div.它在那里设置像素颜色.

                  Note that the snippet expects you have a div with id "color" somewhere. It sets the pixel color there.

                  这篇关于读取画布像素的 RGB 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用户在 HTML5 画布应用程序中绘制的平滑锯齿线? 下一篇:HTML5 Draggable setDragImage 不适用于 Chrome 上的画布

                  相关文章

                      <tfoot id='wEF3a'></tfoot>

                      <small id='wEF3a'></small><noframes id='wEF3a'>

                      • <bdo id='wEF3a'></bdo><ul id='wEF3a'></ul>
                    1. <i id='wEF3a'><tr id='wEF3a'><dt id='wEF3a'><q id='wEF3a'><span id='wEF3a'><b id='wEF3a'><form id='wEF3a'><ins id='wEF3a'></ins><ul id='wEF3a'></ul><sub id='wEF3a'></sub></form><legend id='wEF3a'></legend><bdo id='wEF3a'><pre id='wEF3a'><center id='wEF3a'></center></pre></bdo></b><th id='wEF3a'></th></span></q></dt></tr></i><div id='wEF3a'><tfoot id='wEF3a'></tfoot><dl id='wEF3a'><fieldset id='wEF3a'></fieldset></dl></div>
                    2. <legend id='wEF3a'><style id='wEF3a'><dir id='wEF3a'><q id='wEF3a'></q></dir></style></legend>