npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guydav/phaser-on-nodejs

v1.2.9

Published

Allows you to run Phaser 3 games (including Phaser's physics engines) on Node.js.

Downloads

65

Readme

Phaser on Node.js

Allows you to run Phaser 3 games (including Phaser's physics engines) on Node.js.

Github Workflow npm Downloads NPM Codecov

Arcade Physics

⚠️ If your goal is to run the Arcade Physics on the server, I highly recommend using arcade-physics.

Compatibility

Works with Phaser <3.50.0 and >=3.55.2.
Successfully tested with v3.60.0

Works on Node.js 16.x, 18.x and 20.x.

Install

npm install @geckos.io/phaser-on-nodejs

How to use

require('@geckos.io/phaser-on-nodejs')
// or with es6
import '@geckos.io/phaser-on-nodejs'

Features

  • Phaser Physics (Arcade and Matter)
  • Load Images and SpriteSheets
  • Load TileMaps
  • Adjustable Frame Rate
  • Allows to use Multiple Scenes

Examples

Basic Setup

Install and require phaser and @geckos.io/phaser-on-nodejs. Make sure you use Phaser in headless mode on the server { type: Phaser.HEADLESS }

require('@geckos.io/phaser-on-nodejs')
const Phaser = require('phaser')

// set the fps you need
const FPS = 30
global.phaserOnNodeFPS = FPS // default is 60

// your MainScene
class MainScene extends Phaser.Scene {
  constructor() {
    super('MainScene')
  }
  create() {
    console.log('it works!')
  }
}

// prepare the config for Phaser
const config = {
  type: Phaser.HEADLESS,
  width: 1280,
  height: 720,
  banner: false,
  audio: false,
  scene: [MainScene],
  fps: {
    target: FPS
  },
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 300 }
    }
  }
}

// start the game
new Phaser.Game(config)

Loading Assets

You can load textures (images, spritesheets etc.) on the server.

preload() {
  // use a relative path
  this.load.image('star', './assets/star.png')
}

create() {
  const star = this.physics.add.sprite(400, 300, 'star')
}

But to save some memory, I recommend the following approach instead:

class Star extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x, y) {
    // pass empty string for the texture
    super(scene, x, y, '')

    scene.add.existing(this)
    scene.physics.add.existing(this)

    // set the width and height of the sprite as the body size
    this.body.setSize(24, 22)
  }
}

Using node-fetch or axios?

If you are using node-fetch, you do not need to do anything.

If you are using axios, you have to make sure XMLHttpRequest will not break:
XMLHttpRequest is only use in the browser. Phaser.js is a browsers framework which uses XMLHttpRequest so phaser-on-nodejs has to provide a mock implementation. Unfortunately, axios is a isomorphic framework. On initialization, axios checks if XMLHttpRequest is available and will think it is running in the browser. To make sure axios works on nodejs, we just have to hide XMLHttpRequest from axios during its initialization. See the snipped below to make it work:

// remove fakeXMLHttpRequest
const tmp = XMLHttpRequest
XMLHttpRequest = undefined
// init axios
const axios = require('axios').default
// restore fakeXMLHttpRequest
XMLHttpRequest = tmp

Compatible Phaser versions

For now, it has not been tested with Phaser 2, but it works well with Phaser 3.