JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.7.1/pixi.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/default.css">
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>
CSS
* {
padding: 0;
margin: 0;
}
body {
background-color: rgb(0, 0, 0);
width: 100%;
height: 100%;
overflow: hidden;
}
#pixi-canvas {
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
/* Older Gecko browser */
-ms-transform: translate(-50%, -50%);
/* IE9+ */
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
JavaScript
const logicalWidth = 2048;
const logicalHeight = 1024;
// Init Application
let app = new PIXI.Application(logicalWidth, logicalHeight, { backgroundColor: 0x2c3e50, roundPixels: true, resolution: window.devicePixelRatio || 1, autoResize: true });
app.view.id = 'pixi-canvas';
// Add canvas to DOM
document.body.appendChild(app.view);
PIXI.loader
.add('bg', 'https://i.imgur.com/USY74KX.png')
.load(() => loadGame());
function loadGame()
{
const background = PIXI.Sprite.fromImage("bg");
app.stage.addChild(background);
// do initial resize
resizeHandler();
// listen to resize event
window.addEventListener('resize', resizeHandler);
}
function resizeHandler()
{
const w = Math.max(window.innerWidth, document.documentElement.clientWidth);
const h = Math.max(window.innerHeight, document.documentElement.clientHeight);
const scaleFactor = Math.min(
w / logicalWidth,
h / logicalHeight
);
const newWidth = Math.ceil(logicalWidth * scaleFactor);
const newHeight = Math.ceil(logicalHeight * scaleFactor);
app.renderer.resize(newWidth, newHeight);
app.stage.scale.set(scaleFactor);
};