Get a pixel from HTML Canvas?

后端 未结 10 2119
情深已故
情深已故 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:45

    You can use i << 2.

    const data = context.getImageData(x, y, width, height).data;
    const pixels = [];
    
    for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
        pixels.push({
            r: data[dx  ],
            g: data[dx+1],
            b: data[dx+2],
            a: data[dx+3]
        });
    }
    

提交回复
热议问题