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

前端 未结 2 905
渐次进展
渐次进展 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: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.

提交回复
热议问题