Skip to content

Commit 1526386

Browse files
authored
Merge pull request #31 from codewithkyle/webgl-rewrite
Forcing resize due to GPU limits on some devices
2 parents a5f210b + 24aeae6 commit 1526386

File tree

1 file changed

+31
-8
lines changed
  • client/src/pages/tabletop-page/tabletop-component/table-canvas

1 file changed

+31
-8
lines changed

client/src/pages/tabletop-page/tabletop-component/table-canvas/table-canvas.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,31 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
352352
this.image.crossOrigin = "anonymous";
353353
this.image.src = imageSrc;
354354
this.image.onload = () => {
355-
this.pos.x = (this.w * 0.5) - (this.image.width * 0.5);
356-
this.pos.y = ((this.h - 28) * 0.5) - (this.image.height * 0.5);
355+
356+
let canvas = null;
357+
let width = this.image.width;
358+
let height = this.image.height;
359+
if (this.image.height > 8000 || this.image.width > 8000) {
360+
console.log("too big, doing resize");
361+
const scaleFactor = 8000 / Math.max(this.image.width, this.image.height);
362+
width = Math.floor(this.image.width * scaleFactor);
363+
height = Math.floor(this.image.height * scaleFactor);
364+
365+
canvas = document.createElement('canvas');
366+
canvas.width = width;
367+
canvas.height = height;
368+
369+
const ctx = canvas.getContext('2d');
370+
ctx.drawImage(this.image, 0, 0, width, height);
371+
372+
console.log("switched to canvas", width, height);
373+
374+
this.image.width = width;
375+
this.image.height = height;
376+
}
377+
378+
this.pos.x = (this.w * 0.5) - (width * 0.5);
379+
this.pos.y = ((this.h - 28) * 0.5) - (height * 0.5);
357380

358381
this.imgProgram = new Program(this.gl)
359382
.add_vertex_shader(map_vert_shader)
@@ -363,9 +386,9 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
363386
.build_attributes(["a_position", "a_texCoord"])
364387
.set_verticies(new Float32Array([
365388
0, 0, 0.0, 0.0, // top-left
366-
this.image.width, 0, 1.0, 0.0, // top-right
367-
0, this.image.height, 0.0, 1.0, // bottom-left
368-
this.image.width, this.image.height, 1.0, 1.0 // bottom-right
389+
width, 0, 1.0, 0.0, // top-right
390+
0, height, 0.0, 1.0, // bottom-left
391+
width, height, 1.0, 1.0 // bottom-right
369392
]))
370393
.set_indices(new Uint16Array([
371394
0, 1, 2, // First triangle
@@ -395,9 +418,9 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
395418

396419
this.gl.activeTexture(this.gl.TEXTURE0);
397420
this.gl.bindTexture(this.gl.TEXTURE_2D, this.imgProgram.get_texture());
398-
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, this.image);
421+
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, canvas || this.image);
399422

400-
if (this.isPowerOfTwo(this.image.width) && this.isPowerOfTwo(this.image.height)) {
423+
if (this.isPowerOfTwo(width) && this.isPowerOfTwo(height)) {
401424
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_NEAREST);
402425
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.NEAREST);
403426
this.gl.generateMipmap(this.gl.TEXTURE_2D);
@@ -426,7 +449,7 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
426449

427450
this.doMove = true;
428451
this.animationId = window.requestAnimationFrame(this.firstFrame.bind(this));
429-
return resolve([this.image.width, this.image.height]);
452+
return resolve([width, height]);
430453
};
431454
});
432455
}

0 commit comments

Comments
 (0)