Skip to content

Commit e9a8c7d

Browse files
ExelooMartinFillon
authored andcommitted
feat(graphics): add dynamic size handle of canvas
1 parent 0a87b42 commit e9a8c7d

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

packages/graphics-2d/src/components/component.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export abstract class NfgComponent {
4040

4141
public async init(): Promise<typeof this> {
4242
await this._init();
43-
this._updateUniforms();
43+
this.updateUniforms();
4444
this._updatePipeline();
4545
return this;
4646
}
@@ -52,6 +52,27 @@ export abstract class NfgComponent {
5252
pass.draw(this._vertices.length / this._vertexLength, this._duplicate);
5353
}
5454

55+
public updateUniforms(): void {
56+
const uniformArray = new Float32Array([
57+
0,
58+
0,
59+
0,
60+
1,
61+
1,
62+
1,
63+
this._core.initContext.canvas.width,
64+
this._core.initContext.canvas.height,
65+
]);
66+
console.log(this._core.initContext.canvas.width, this._core.initContext.canvas.height);
67+
if (!this._uniformBuffer)
68+
this._uniformBuffer = this._core.device.createBuffer({
69+
label: "View Uniforms",
70+
size: uniformArray.byteLength,
71+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
72+
});
73+
this._core.device.queue.writeBuffer(this._uniformBuffer, 0, uniformArray);
74+
}
75+
5576
protected abstract _init(): Promise<void>;
5677

5778
protected _setVertices(raw: number[]): void {
@@ -96,25 +117,6 @@ export abstract class NfgComponent {
96117
this._updateBindGroup();
97118
}
98119

99-
protected _updateUniforms(): void {
100-
const uniformArray = new Float32Array([
101-
0,
102-
0,
103-
0,
104-
1,
105-
1,
106-
1,
107-
this._core.initContext.canvas.width,
108-
this._core.initContext.canvas.height,
109-
]);
110-
this._uniformBuffer = this._core.device.createBuffer({
111-
label: "View Uniforms",
112-
size: uniformArray.byteLength,
113-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, // Une uniform est une valeur constante pour le gpu
114-
});
115-
this._core.device.queue.writeBuffer(this._uniformBuffer, 0, uniformArray);
116-
}
117-
118120
protected _updateBindGroup(): void {
119121
this._bindGroup = this._core.device.createBindGroup({
120122
label: `${this._label} renderer bind group`,

packages/graphics-2d/src/render/window.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import { type GraphicsRender } from "../render";
44
export class NfgWindow {
55
private _components: NfgComponent[] = [];
66
private _render: GraphicsRender;
7+
private _uniformTimeout: boolean = false;
8+
private _uniformNeed: boolean = false;
79

810
constructor(render: GraphicsRender) {
911
this._render = render;
12+
13+
window.addEventListener("resize", () => {
14+
this._updateWindowSize();
15+
});
16+
this._updateWindowSize();
1017
}
1118

1219
get width(): number {
@@ -22,7 +29,27 @@ export class NfgWindow {
2229
}
2330

2431
public render(): void {
32+
if (this._uniformNeed) {
33+
this.updateAllUniforms();
34+
this._uniformNeed = false;
35+
}
2536
this._render.render(this._components);
2637
this._components = [];
2738
}
39+
40+
private _updateWindowSize(): void {
41+
this._render.canvas.width = window.innerWidth;
42+
this._render.canvas.height = window.innerHeight;
43+
// Uncomment if need performances
44+
// if (this._uniformTimeout) return;
45+
this._uniformTimeout = true;
46+
this._uniformNeed = true;
47+
setTimeout(() => {
48+
this._uniformTimeout = false;
49+
}, 100);
50+
}
51+
52+
private updateAllUniforms(): void {
53+
this._components.forEach((component) => component.updateUniforms());
54+
}
2855
}

0 commit comments

Comments
 (0)