IonPhaser WebComponent

IonPhaser WebComponent

A web component to integrate Phaser Framework with Angular, React, Vue, etc

Free!

(1)
Juan David Nicholls

Juan David Nicholls

Member since 2014

Details

Version:
2.0
Size:
0mb
Ionic:
1.x,2.x,3.x,4.x
Platforms:
iOS, Android, Windows Phone
View ID:
fed7b239
Released:
8 years ago
Updated:
5 years ago
Category:
Plugins
Tags:
Ionic, Phaser, Angular, React, Vue.js, Javascript, Canvas, WebComponent, Stencil, Phaser 3

Built With Stencil

IonPhaser

Web Component built with Stencil.js to integrate Phaser with any other framework.

IonPhaser

Inspired by the old IonPhaser directive

Demo

Do you want to see this web component in action? Visit https://codepen.io/jdnichollsc/full/oRrwKM yay! 🎉

IonPhaser CE

Looking for Phaser Framework CE (Community Edition)? Check here!

Getting Started

Packages

Project Package Version Links
Core @ion-phaser/core version README.md
React @ion-phaser/react version README.md

Script tag

  • Put a script tag similar to this <script src='https://unpkg.com/@ion-phaser/core@1.2.0/dist/ionphaser.js'></script> in the head of your index.html
  • Then you can use the element anywhere in your template, JSX, html etc

Node Modules

  • Run npm install @ion-phaser/core --save
  • Put a script tag similar to this <script src='node_modules/@ion-phaser/core/dist/ionphaser.js'></script> in the head of your index.html
  • Then you can use the element anywhere in your template, JSX, html etc

In a stencil-starter app

  • Run npm install @ion-phaser/core --save
  • Add an import to the npm packages import @ion-phaser/core;
  • Then you can use the element anywhere in your template, JSX, html etc

Usage

Simply add this tag wherever you want in your project:

<ion-phaser [game]="game"></ion-phaser>

These properties are available on the component:

  • game (required)
  • initialize (optional)

Framework integrations

Angular

Using ion-phaser component within an Angular project:

Including the Custom Elements Schema

Including the CUSTOM_ELEMENTS_SCHEMA in the module allows the use of Web Components in the HTML files. Here is an example of adding it to AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {}

The CUSTOM_ELEMENTS_SCHEMA needs to be included in any module that uses IonPhaser.

Calling defineCustomElements

IonPhaser component includes a function used to load itself in the application window object. That function is called defineCustomElements() and needs to be executed once during the bootstrapping of your application. One convenient place to add it is in the main.ts file as follows:

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { defineCustomElements as defineIonPhaser } from '@ion-phaser/core/loader';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.log(err));
defineIonPhaser(window);

Using IonPhaser in an Angular component

<ion-phaser
  [game]="game"
  [initialize]="initialize"
></ion-phaser>
public game = {
  width?: integer | string;
  height?: integer | string;
  zoom?: number;
  resolution?: number;
  type?: number;
  parent: HTMLElement | string;
  canvas?: HTMLCanvasElement;
  canvasStyle?: string;
  context?: CanvasRenderingContext2D;
  scene?: object;
  seed?: string[];
  title?: string;
  url?: string;
  version?: string;
  autoFocus?: boolean;
  input?: boolean | InputConfig;
  disableContextMenu?: boolean;
  banner?: boolean | BannerConfig;
  dom?: DOMContainerConfig;
  fps?: FPSConfig;
  render?: RenderConfig;
  backgroundColor?: string | number;
  callbacks?: CallbacksConfig;
  loader?: LoaderConfig;
  images?: ImagesConfig;
  physics?: object;
  plugins?: PluginObject | PluginObjectItem[];
  scale?: ScaleConfig;,
  instance: Game // It's created internally when the game is initialized
};

public initialize: boolean;

constructor(private api : ApiService){}

initializeGame() {
  this.game = {
    width: "100%",
    height: "100%",
    type: Phaser.AUTO,
    scene: {}
  }
  this.initialize = true
}

getInstance(){
  return this.game.instance
}

from stencil documentation

React

Specific Wrapper

When using a wrapper component, It's not necessary to access the reference directly to configure the game. More details here.

import React, { Component } from 'react'
import Phaser from 'phaser'
import { IonPhaser } from '@ion-phaser/react'

class App extends Component {
  state = {
    initialize: false,
    game: {
      width: "100%",
      height: "100%",
      type: Phaser.AUTO,
      scene: {}
    }
  }
  render() {
    const { initialize, game } = this.state
    return (
      <IonPhaser game={game} initialize={initialize} />
    )
  }
}

Web Component

Other option is using the web component directly:

import React from 'react'
import ReactDOM from 'react-dom'
import { defineCustomElements as defineIonPhaser } from '@ion-phaser/core/loader'
import Phaser from 'phaser'

const game = {
  width: "100%",
  height: "100%",
  type: Phaser.AUTO,
  scene: {}
}

ReactDOM.render(<ion-phaser ref={el => el.game = game} />, document.getElementById('root'));

defineIonPhaser(window);

from stencil documentation

Vue

In order to use the ion-phaser Web Component inside of a Vue application, it should be modified to define the custom elements and to inform the Vue compiler which elements to ignore during compilation. This can all be done within the main.js file as follows:

import Vue from 'vue';
import { defineCustomElements as defineIonPhaser } from '@ion-phaser/core/loader'

import App from './App.vue';

Vue.config.productionTip = false;
Vue.config.ignoredElements = [/ion-\w*/];

// Bind the IonPhaser custom element to the window object
defineIonPhaser(window);

new Vue({
  render: h => h(App)
}).$mount('#app');

Using IonPhaser in a Vue component

<template>
  <ion-phaser 
    v-bind:game.prop="game"
    v-bind:initialize.prop="initialize"
  />
</template>

<script>
import Phaser from 'phaser'
export default {
  name: 'HelloWorld',
  data() {
    return {
      initialize: false,
      game: {
        width: "100%",
        height: "100%",
        type: Phaser.AUTO,
        scene: {
          init: function() {
            this.cameras.main.setBackgroundColor('#24252A')
          },
          create: function() {
            this.helloWorld = this.add.text(
              this.cameras.main.centerX, 
              this.cameras.main.centerY, 
              "Hello World", { 
                font: "40px Arial", 
                fill: "#ffffff" 
              }
            );
            this.helloWorld.setOrigin(0.5);
          },
          update: function() {
            this.helloWorld.angle += 1;
          }
        }
      }
    }
  }
}
</script>

from stencil documentation

Supporting 🍻

I believe in Unicorns 🦄 Support me, if you do too.

Security contact information 🚨

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Happy coding 💯

Made with ❤️

Hey there! You'll need to log in before you can leave a comment here.

InLabs

InLabs   ·   8 years ago

Excelente!!

Jorge

Jorge   ·   8 years ago

Very Creative application template. Recommended

Jose Ripoll

Jose Ripoll   ·   8 years ago

Ionic and Phaser playing together? wow, I have to see it

Daniel Malkafly

Daniel Malkafly   ·   8 years ago

i need it!

Pedro Dias

Pedro Dias   ·   8 years ago

I'm having problems, to make the payment. Can't you use paypal or something to make this step easier.

Juan David Nicholls

Juan David Nicholls   ·   8 years ago

My email is jdnichollsc@hotmail.com :)

Dimitri

Dimitri   ·   7 years ago

7 months without an update? Now I'm sad. :( I wanted to buy this but... seeing the state it actually is, it makes me back out and wait. I really hope this moves forward.

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

7 months without any issue reported on GitHub... https://github.com/jdnichollsc/IonPhaser I want to add new features, what do you want to see in this project?

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

You can use the IonPhaser WebComponent, it's free!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Christopher Ridout

Christopher Ridout   ·   7 years ago

Are you thinking of support for Ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Yes! Let me check this weekend :)

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Done!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Ryan Pascal

Ryan Pascal   ·   7 years ago

Does it work for Ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Let me migrate the template

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Yes sir!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

M N

M N   ·   7 years ago

Is it compatible with ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Let me migrate the template

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Done, it support all versions and frameworks! (React, Vue.js, Angular, etc)

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

The countdown

The countdown   ·   7 years ago

Hi Juan David. Is it already compatible with ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

IonPhaser WebComponent is free and support all versions and frameworks (Angular, React, Vue.js, etc)!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

  ·   just now

{{ comment.comment }}

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

  ·   just now

{{ comment.comment }}

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

The countdown

The countdown   ·   7 years ago

Hi Juan David. Is it already compatible with ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

IonPhaser WebComponent is free and support all versions and frameworks (Angular, React, Vue.js, etc)!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

M N

M N   ·   7 years ago

Is it compatible with ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Let me migrate the template

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Done, it support all versions and frameworks! (React, Vue.js, Angular, etc)

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Ryan Pascal

Ryan Pascal   ·   7 years ago

Does it work for Ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Let me migrate the template

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Yes sir!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Christopher Ridout

Christopher Ridout   ·   7 years ago

Are you thinking of support for Ionic 2?

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

Yes! Let me check this weekend :)

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

Done!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Dimitri

Dimitri   ·   7 years ago

7 months without an update? Now I'm sad. :( I wanted to buy this but... seeing the state it actually is, it makes me back out and wait. I really hope this moves forward.

Juan David Nicholls

Juan David Nicholls   ·   7 years ago

7 months without any issue reported on GitHub... https://github.com/jdnichollsc/IonPhaser I want to add new features, what do you want to see in this project?

Juan David Nicholls

Juan David Nicholls   ·   5 years ago

You can use the IonPhaser WebComponent, it's free!

  ·   just now

{{ reply.comment }}



Hey there! You'll need to log in before you can leave a comment here.

Jorge

Jorge   ·   8 years ago

Very Creative application template. Recommended

Pedro Dias

Pedro Dias   ·   8 years ago

I'm having problems, to make the payment. Can't you use paypal or something to make this step easier.

Daniel Malkafly

Daniel Malkafly   ·   8 years ago

i need it!

Jose Ripoll

Jose Ripoll   ·   8 years ago

Ionic and Phaser playing together? wow, I have to see it

InLabs

InLabs   ·   8 years ago

Excelente!!

Juan David Nicholls

Juan David Nicholls   ·   8 years ago

My email is jdnichollsc@hotmail.com :)

Hey there! You'll need to log in before you can leave a rating here.

David Velasquez   ·     ·   5 years ago

Felicitaciones Juan David

  ·     ·   just now

{{ rating.comment }}

  ·     ·   just now

{{ rating.comment }}

David Velasquez    ·     ·   5 years ago

Felicitaciones Juan David