• <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 值

        Reading the RGB value of a canvas pixel(读取画布像素的 RGB 值)

        <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 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

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

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

                      • <bdo id='wEF3a'></bdo><ul id='wEF3a'></ul>
                      • <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>
                        • <legend id='wEF3a'><style id='wEF3a'><dir id='wEF3a'><q id='wEF3a'></q></dir></style></legend>
                              <tbody id='wEF3a'></tbody>