Skip to content

Commit

Permalink
abstract pixi & implement eresize
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDex committed Oct 6, 2023
1 parent c26b669 commit 05d0dfc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
61 changes: 51 additions & 10 deletions src/tests/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
/* eslint-disable no-restricted-imports */
import { Application } from 'pixi.js';
import { Container, Text } from 'pixi.js';
import { initPixi } from './utils/pixi';

const app = new Application();

app.init({
resizeTo: window,
background: 0x000000,
}).then(() =>
// eslint-disable-next-line no-new
new class App
{
document.body.appendChild(app.canvas as any);
});
private view: Container;

constructor()
{
this.init();
}

private async init()
{
const pixiApp = await initPixi();

this.view = new Container();

pixiApp.stage.addChild(this.view);

this.createElements();

this.addSubscriptions();
}

private addSubscriptions()
{
window.addEventListener('resize', () => this.resize());
window.addEventListener('deviceorientation', () => this.resize());
this.resize();
}

private createElements()
{
const text = new Text({ text: 'Pixi 8' });

text.anchor.set(0.5);
text.style = {
fontSize: 100,
fill: 0xffffff
};

this.view.addChild(text);
}

private resize(width = window.innerWidth, height = window.innerHeight)
{
this.view.x = width / 2;
this.view.y = height / 2;
}
}();

21 changes: 21 additions & 0 deletions src/tests/utils/pixi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Application, ApplicationOptions } from 'pixi.js';

export async function initPixi(options?: Partial<ApplicationOptions>): Promise<Application>
{
const app = new Application();

await app.init({
resizeTo: window,
background: 0x000000,
...options
});

document.body.appendChild(app.canvas as any);

// #v-ifdef MODE=production
// Pixi inspector
(globalThis as any).__PIXI_APP__ = app;
// #v-endif

return app;
}

0 comments on commit 05d0dfc

Please sign in to comment.