Skip to content

Instantly share code, notes, and snippets.

@davschne
Created August 23, 2019 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davschne/ffc85b4e79e8f326283caee4303f9583 to your computer and use it in GitHub Desktop.
Save davschne/ffc85b4e79e8f326283caee4303f9583 to your computer and use it in GitHub Desktop.
// create a DataView for obtaining a value from the binary pixel data
const tileDataView = new DataView(pixelData.buffer);
// To find the byte index:
// (1) get the index of the start of the row in which the pixel is located
// (row index * number of columns)
// (2) add to that the column index
// (3) multiply by the number of bytes used for each pixel (4)
const byteIndex = (coordsInTile.y * tileSize + coordsInTile.x) * BYTES_PER_WORD;
// use the byte index and the machine's endianness to obtain the pixel value
const pixelValue = tileDataView.getFloat32(byteIndex, littleEndian);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment