Get a pixel from HTML Canvas?

后端 未结 10 1998
情深已故
情深已故 2020-11-22 12:41

Is it possible to query a HTML Canvas object to get the color at a specific location?

相关标签:
10条回答
  • 2020-11-22 13:21

    If you want to extract a particular color of pixel by passing the coordinates of pixel into the function, this will come in handy:

    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    function detectColor(x, y){
      data=ctx.getImageData(x, y, 1, 1).data;
      col={
        r:data[0],
        g:data[1],
        b:data[2]
      };
      return col;
    }
    

    x, y is the coordinate you want to filter out color.

    var color = detectColor(x, y)
    

    The color is the object, you will get the RGB value by color.r, color.g, color.b.

    0 讨论(0)
  • 2020-11-22 13:22

    Try the getImageData method:

    var data = context.getImageData(x, y, 1, 1).data;
    var rgb = [ data[0], data[1], data[2] ];
    
    0 讨论(0)
  • 2020-11-22 13:23

    Yes sure, provided you have its context. (See how to get canvas context here.)

    var imgData = context.getImageData(0,0,canvas.width,canvas.height);
    // { data: [r,g,b,a,r,g,b,a,r,g,..], ... }
    
    function getPixel(imgData, index) {
      var i = index*4, d = imgData.data;
      return [d[i],d[i+1],d[i+2],d[i+3]] // Returns array [R,G,B,A]
    }
    
    // AND/OR
    
    function getPixelXY(imgData, x, y) {
      return getPixel(imgData, y*imgData.width+x);
    }
    



    PS: If you plan to mutate the data and draw them back on the canvas, you can use subarray

    var
      idt = imgData, // See previous code snippet
      a = getPixel(idt, 188411), // Array(4) [0, 251, 0, 255]
      b = idt.data.subarray(188411*4, 188411*4 + 4) // Uint8ClampedArray(4) [0, 251, 0, 255]
    
    a[0] = 255 // Does nothing
    getPixel(idt, 188411), // Array(4) [0, 251, 0, 255]
    
    b[0] = 255 // Mutates the original imgData.data
    getPixel(idt, 188411), // Array(4) [255, 251, 0, 255]
    
    // Or use it in the function
    function getPixel(imgData, index) {
      var i = index*4, d = imgData.data;
      return imgData.data.subarray(index, index+4) // Returns subarray [R,G,B,A]
    }
    

    You can experiment with this on http://qry.me/xyscope/, the code for this is in the source, just copy/paste it in the console.

    0 讨论(0)
  • 2020-11-22 13:24

    Note that getImageData returns a snapshot. Implications are:

    • Changes will not take effect until subsequent putImageData
    • getImageData and putImageData calls are relatively slow
    0 讨论(0)
  • 2020-11-22 13:25
    // Get pixel data
    var imageData = context.getImageData(x, y, width, height);
    
    // Color at (x,y) position
    var color = [];
    color['red'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 0];
    color['green'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 1];
    color['blue'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 2];
    color['alpha'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 3];
    
    0 讨论(0)
  • 2020-11-22 13:26

    There's a section about pixel manipulation in the W3C documentation.

    Here's an example on how to invert an image:

    var context = document.getElementById('myCanvas').getContext('2d');
    
    // Get the CanvasPixelArray from the given coordinates and dimensions.
    var imgd = context.getImageData(x, y, width, height);
    var pix = imgd.data;
    
    // Loop over each pixel and invert the color.
    for (var i = 0, n = pix.length; i < n; i += 4) {
        pix[i  ] = 255 - pix[i  ]; // red
        pix[i+1] = 255 - pix[i+1]; // green
        pix[i+2] = 255 - pix[i+2]; // blue
        // i+3 is alpha (the fourth element)
    }
    
    // Draw the ImageData at the given (x,y) coordinates.
    context.putImageData(imgd, x, y);
    
    0 讨论(0)
提交回复
热议问题