Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
var data = imageData.data;

for (var i = data.length; i >= 0; i -= 4) {
    if (data[i + 3] > 0) {
        data[i] = this.colour.R;
        data[i + 1] = this.colour.G;
        data[i + 2] = this.colour.B;
    }
}

How do I calculate the current X and Y pixel coordinates that I am at?

like image 662
jcvandan Avatar asked Dec 01 '12 15:12

jcvandan


People also ask

How do I get Photodata from canvas?

The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas. Note: The ImageData object is not a picture, it specifies a part (rectangle) on the canvas, and holds information of every pixel inside that rectangle.

How do you find the pixel coordinate?

In terms of coordinates, a pixel can be identified by a pair of integers giving the column number and the row number. For example, the pixel with coordinates (3,5) would lie in column number 3 and row number 5. Conventionally, columns are numbered from left to right, starting with zero.

How do you find pixels in HTML?

To get the pixel of any specific portion from HTML canvas you can use the HTML canvas getImageData() Method. The getImageData() method usually returns an ImageData object that contains the pixel information of the specified object on the HTML canvas.

What are pixel coordinates?

The pixel coordinate is a number that identifies the location of a pixel in the image array.

How to get the image data for each pixel of canvas?

To get the image data for each pixel of a rectangular area on the canvas, we can get the image data object with the getImageData () method of the canvas context and then access the pixel data from the data property. Each pixel in the image data contains four components, a red, green, blue, and alpha component.

How do I find the coordinates of a canvas in HTML?

The HTML canvas is a two-dimensional grid. The upper-left corner of the canvas has the coordinates (0,0) In the previous chapter, you saw this method used: fillRect (0,0,150,75). This means: Start at the upper-left corner (0,0) and draw a 150x75 pixels rectangle. Mouse over the rectangle below to see its x and y coordinates:

How do I use the imagedata object?

The ImageData object contains an array of pixels. By accessing this array you can manipulate the pixels. When you are done with the pixel manipulation, you need to copy the pixels onto a canvas to display them. Creating an ImageData object is done using the 2D Context function createImageData ().

What is the use of getimagedata?

Definition and Usage. The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas.


2 Answers

Corrected and tested version of code:

var x = (i / 4) % this.el.width;
var y = Math.floor((i / 4) / this.el.width);
like image 163
user11153 Avatar answered Oct 06 '22 11:10

user11153


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.

like image 30
bill automata Avatar answered Oct 06 '22 10:10

bill automata