Skip to content

Commit

Permalink
HighDPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshvarun committed May 12, 2023
1 parent d9152ae commit eab8ad9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
7 changes: 5 additions & 2 deletions netplayjs-client/src/defaultinput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class DefaultInput extends NetplayInput {

export class DefaultInputReader {
canvas: HTMLCanvasElement;
canvasSize: { width: number; height: number };

keysPressed: { [key: string]: boolean } = {};
keysHeld: { [key: string]: boolean } = {};
Expand All @@ -73,8 +74,8 @@ export class DefaultInputReader {
getCanvasScale(): { x: number; y: number } {
const rect = this.canvas.getBoundingClientRect();
return {
x: this.canvas.width / rect.width,
y: this.canvas.height / rect.height,
x: this.canvasSize.width / rect.width,
y: this.canvasSize.height / rect.height,
};
}

Expand All @@ -94,11 +95,13 @@ export class DefaultInputReader {
constructor(
root: HTMLElement = document.body,
canvas: HTMLCanvasElement,
canvasSize: { width: number; height: number },
pointerLock: boolean = false,
preventContextMenu: boolean = false,
touchControls: { [name: string]: TouchControl } = {}
) {
this.canvas = canvas;
this.canvasSize = canvasSize;
this.touchControls = touchControls;

root.addEventListener(
Expand Down
7 changes: 7 additions & 0 deletions netplayjs-client/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export type GameClass = {
*/
canvasSize: { width: number; height: number };

/**
* Should we scale up the canvas with the device pixel ratio?
* If enabled, it is up to the game code to draw correctly
* using window.devicePixelRatio.
*/
highDPI?: boolean;

/**
* Whether or not we should lock the pointer when the user clicks on the
* canvas. Use this for games like FPSs.
Expand Down
9 changes: 7 additions & 2 deletions netplayjs-client/src/wrappers/gamewrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ export abstract class GameWrapper {

// Create canvas for game.
this.canvas = document.createElement("canvas");
this.canvas.width = gameClass.canvasSize.width;
this.canvas.height = gameClass.canvasSize.height;

const pixelRatio = (gameClass.highDPI && window.devicePixelRatio) ?
window.devicePixelRatio : 1;

this.canvas.width = gameClass.canvasSize.width * pixelRatio;
this.canvas.height = gameClass.canvasSize.height * pixelRatio;

this.canvas.style.backgroundColor = "black";
this.canvas.style.position = "absolute";
Expand Down Expand Up @@ -114,6 +118,7 @@ export abstract class GameWrapper {
this.inputReader = new DefaultInputReader(
document.body,
this.canvas,
this.gameClass.canvasSize,
this.gameClass.pointerLock || false,
this.gameClass.preventContextMenu || false,
this.gameClass.touchControls || {}
Expand Down
11 changes: 9 additions & 2 deletions netplayjs-client/src/wrappers/localwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,26 @@ export class LocalWrapper {
for (let i = 0; i < numPlayers; ++i) {
// Create canvas for game.
const canvas = document.createElement("canvas");
canvas.width = gameClass.canvasSize.width;
canvas.height = gameClass.canvasSize.height;

const pixelRatio = (gameClass.highDPI && window.devicePixelRatio) ?
window.devicePixelRatio : 1;

canvas.width = gameClass.canvasSize.width * pixelRatio;
canvas.height = gameClass.canvasSize.height * pixelRatio;
canvas.tabIndex = 0;

canvas.style.backgroundColor = "black";
canvas.style.boxShadow = "0px 0px 10px black";
canvas.style.margin = "5px";
canvas.style.width = `${gameClass.canvasSize.width}px`;
canvas.style.height = `${gameClass.canvasSize.height}px`;
document.body.appendChild(canvas);

// Create input reader.
const inputReader = new DefaultInputReader(
canvas,
canvas,
this.gameClass.canvasSize,
this.gameClass.pointerLock || false,
this.gameClass.preventContextMenu || false,
{}
Expand Down
1 change: 1 addition & 0 deletions netplayjs-demo/src/examples/fps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const PLAYER_COLLIDER = new Capsule();
export class PhysicsGame extends Game {
static timestep = 1000 / 60;
static canvasSize = { width: 600, height: 300 };
static highDPI = true;

static octree = new Octree();
static level: THREE.Object3D = new THREE.Object3D();
Expand Down
9 changes: 7 additions & 2 deletions netplayjs-demo/src/examples/input-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {

export class InputTestGame extends Game {
static timestep = 1000 / 5;
static canvasSize = { width: 600, height: 1000 };
static canvasSize = { width: 600, height: 600 };
static deterministic = true;
static preventContextMenu = true;

static highDPI = true;

inputs: Map<NetplayPlayer, DefaultInput> = new Map();

tick(playerInputs: Map<NetplayPlayer, DefaultInput>): void {
Expand All @@ -25,6 +26,10 @@ import {
draw(canvas: HTMLCanvasElement) {
// Fill in a black background.
const ctx = canvas.getContext("2d")!;

ctx.resetTransform();
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);

Expand Down
4 changes: 4 additions & 0 deletions netplayjs-demo/src/examples/pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function rectOverlap(
export class Pong extends Game {
static timestep = 1000 / 60;
static canvasSize = { width: PONG_WIDTH, height: PONG_HEIGHT };
static highDPI = true;

leftPaddle: number = PONG_HEIGHT / 2 - PADDLE_HEIGHT / 2;
rightPaddle: number = PONG_HEIGHT / 2 - PADDLE_HEIGHT / 2;
Expand Down Expand Up @@ -161,6 +162,9 @@ export class Pong extends Game {
draw(canvas: HTMLCanvasElement) {
const ctx = canvas.getContext("2d")!;

ctx.resetTransform();
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);

Expand Down

0 comments on commit eab8ad9

Please sign in to comment.