Skip to content

Commit

Permalink
never use Uint8ClampedArray with texSubImage2D
Browse files Browse the repository at this point in the history
fix #8190

Chrome doesn't support using Uint8ClampedArray with texSubImage2D.
Work around that.
  • Loading branch information
ansis committed Apr 25, 2019
1 parent 2f54797 commit 0ff9d6e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Point = {
function createImage(image: *, {width, height}: Size, channels: number, data?: Uint8Array | Uint8ClampedArray) {
if (!data) {
data = new Uint8Array(width * height * channels);
} else if (data instanceof Uint8ClampedArray) {
data = new Uint8Array(data.buffer);
} else if (data.length !== width * height * channels) {
throw new RangeError('mismatched image size');
}
Expand Down Expand Up @@ -81,7 +83,7 @@ function copyImage(srcImg: *, dstImg: *, srcPt: Point, dstPt: Point, size: Size,
export class AlphaImage {
width: number;
height: number;
data: Uint8Array | Uint8ClampedArray;
data: Uint8Array;

constructor(size: Size, data?: Uint8Array | Uint8ClampedArray) {
createImage(this, size, 1, data);
Expand All @@ -105,7 +107,10 @@ export class AlphaImage {
export class RGBAImage {
width: number;
height: number;
data: Uint8Array | Uint8ClampedArray;

// data must be a Uint8Array instead of Uint8ClampedArray because texImage2D does not
// support Uint8ClampedArray in all browsers
data: Uint8Array;

constructor(size: Size, data?: Uint8Array | Uint8ClampedArray) {
createImage(this, size, 4, data);
Expand All @@ -118,6 +123,8 @@ export class RGBAImage {
replace(data: Uint8Array | Uint8ClampedArray, copy?: boolean) {
if (copy) {
this.data.set(data);
} else if (data instanceof Uint8ClampedArray) {
this.data = new Uint8Array(data.buffer);
} else {
this.data = data;
}
Expand Down

0 comments on commit 0ff9d6e

Please sign in to comment.