From 986fdecf1bd74b018cde0cb219fddb1de7a5c761 Mon Sep 17 00:00:00 2001 From: tga Date: Tue, 10 Oct 2023 10:27:56 +0800 Subject: [PATCH] fix toImageData flipped --- src/kaboom.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/kaboom.ts b/src/kaboom.ts index c04952c09..45e8384b3 100644 --- a/src/kaboom.ts +++ b/src/kaboom.ts @@ -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) }