Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
fix toImageData flipped
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Oct 10, 2023
1 parent 0069525 commit 986fdec
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,21 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
return this.tex.height
}

// TODO: flipped on Y
toImageData() {
const data = new Uint8ClampedArray(this.width * this.height * 4)
this.bind()
gl.readPixels(0, 0, this.width, this.height, gl.RGBA, gl.UNSIGNED_BYTE, data)
this.unbind()
// flip vertically
const bytesPerRow = this.width * 4
const temp = new Uint8Array(bytesPerRow)
for (let y = 0; y < (this.height / 2 | 0); y++) {
const topOffset = y * bytesPerRow
const bottomOffset = (this.height - y - 1) * bytesPerRow
temp.set(data.subarray(topOffset, topOffset + bytesPerRow))
data.copyWithin(topOffset, bottomOffset, bottomOffset + bytesPerRow)
data.set(temp, bottomOffset)
}
return new ImageData(data, this.width, this.height)
}

Expand Down

0 comments on commit 986fdec

Please sign in to comment.