Skip to content

Commit

Permalink
fix(canvas): fix external canvas resize on dpi change
Browse files Browse the repository at this point in the history
  • Loading branch information
lslzl3000 committed Jun 9, 2024
1 parent 8c5e2b3 commit 2e54053
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/gfx/graphics/webGpu/Context3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Context3D extends CEventDispatcher {
public windowWidth: number;
public windowHeight: number;
public canvasConfig: CanvasConfig;
public super: number = 1.0;
private _pixelRatio: number = 1.0;
private _resizeEvent: CEvent;
// initSize: number[];
Expand All @@ -38,12 +37,11 @@ class Context3D extends CEventDispatcher {
throw new Error('no Canvas')

// check if external canvas has initial with and height style
const _width = this.canvas.clientWidth, _height = this.canvas.clientHeight
// set a initial style if size changed
if (_width != this.canvas.clientWidth)
this.canvas.style.width = _width + 'px'
if (_height != this.canvas.clientHeight)
this.canvas.style.height = _height + 'px'
// TODO: any way to check external css style?
if(!this.canvas.style.width)
this.canvas.style.width = this.canvas.width + 'px';
if(!this.canvas.style.height)
this.canvas.style.height = this.canvas.height + 'px';
} else {
this.canvas = document.createElement('canvas');
// this.canvas.style.position = 'fixed';
Expand Down Expand Up @@ -122,8 +120,8 @@ class Context3D extends CEventDispatcher {
}

public updateSize() {
let w = Math.floor(this.canvas.clientWidth * this.pixelRatio * this.super);
let h = Math.floor(this.canvas.clientHeight * this.pixelRatio * this.super);
let w = Math.floor(this.canvas.clientWidth * this.pixelRatio);
let h = Math.floor(this.canvas.clientHeight * this.pixelRatio);
if (w != this.windowWidth || h != this.windowHeight) {
this.canvas.width = this.windowWidth = w;
this.canvas.height = this.windowHeight = h;
Expand Down

0 comments on commit 2e54053

Please sign in to comment.