Get X and Y pixel coordinates when iterating over HTML5 canvas getImageData

前端 未结 2 904
渐次进展
渐次进展 2020-12-13 14:04

I am iterating over some image data pulled from a canvas like so:

var imageData = this.context.getImageData(0, 0, this.el.width, this.el.height)         


        
相关标签:
2条回答
  • 2020-12-13 14:35

    Corrected and tested version of code:

    var x = (i / 4) % this.el.width;
    var y = Math.floor((i / 4) / this.el.width);
    
    0 讨论(0)
  • 2020-12-13 14:47

    A Simple Arithmetic Sequence:

    Divide the linear position by the width. That's your Y-coordinate. Multiply that Y-coordinate by the width, and subtract that value from the linear position. The result is the X coordinate.

    Also note, you will have to divide the linear position by 4 since it is RGBA.

    0 讨论(0)
提交回复
热议问题