Combining pixijs with TypeScript in Ionic 2 using npm

  • To begin, I ran the command npm install -g ionic
  • Followed by ionic start pixiApp blank --v2
  • Navigated to the pixiApp directory with cd pixiApp
  • Installed necessary dependencies using npm install
  • Added a specific version of pixi.js (4.1.0) with npm install pixi.js --save
  • Despite @types/pixi.js being intended for v3, I included it using npm install --save @types/pixi.js
  • Manually updated Indexed.d.ts in pixiApp\node_modules\@types\pixi.js with code from https://github.com/pixijs/pixi-typescript/blob/v4.x/pixi.js.d.ts
  • Incorporated the following snippet into home.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import * as PIXI from 'pixi.js';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  constructor(public navCtrl: NavController) {}

  ngOnInit() {
    PIXI.autoDetectRenderer(800, 600, {backgroundColor: 0x1099bb});
  }
}

  • Finally, I launched the app using the command: ionic serve

Unfortunately, the results were not what I expected

[16:56:52] rollup: Export 'autoDetectRenderer' is not defined by 'D:\labs\ionic2\pixiApp\src\pages\home\home.ts'

Despite my attempts, I have been unable to resolve the issue :(:(

Any assistance you can provide would be greatly appreciated.

Thank you and best regards

Answer №1

Everything fell into place smoothly for me right from the beginning. I followed your instructions to create a new ionic project, install the pixi.js package and types, and import them as you directed.

Below are my source files for the "home" page:

home.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import * as PIXI from 'pixi.js';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  @ViewChild("pixi")
  public pixiContainer: ElementRef;
  private pixi:PIXI.Application;

  constructor(public navCtrl: NavController) { }

  ionViewDidEnter() {
    this.pixi = new PIXI.Application(640,480,{backgroundColor:0x112266});
    let aspectRatio = (this.pixi.renderer.height / this.pixi.renderer.width);
    this.pixi.view.style.width = "100%";
    this.pixi.view.style.height = `${aspectRatio*100}%`;
    this.pixiContainer.nativeElement.appendChild(this.pixi.view);
  }
}

home.scss

page-home {
    .pixiContainer {
        width: 100%;
        height: 100%;
        display: block;
    }
}

home.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <div #pixi class="pixiContainer">
  </div>
</ion-content>

And it all came together beautifully. My pixijs canvas is now a stunning dark-blue color.

For reference, here is my current version information for ionic:

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.9.2
    ionic (Ionic CLI) : 3.9.2

local packages:

    @ionic/app-scripts : 2.1.3
    Ionic Framework    : ionic-angular 3.6.0

System:

    Node : v7.10.0
    npm  : 4.2.0 
    OS   : macOS Sierra

I hope this provides some insight for you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Access Static Method from Non-Static Method in TypeScript

Within my hierarchy, there is some static content present in all levels (for example, the _image). It would be ideal to access the corresponding _image without repeating code: Here's what I envision: class Actor { static _image; // Needs to be s ...

Upon completion of a promise in an express middleware and breaking out of a loop, a 404 error is returned

In my efforts to retrieve an array of object (car) from express using database functions in conjunction with the stolenCarDb object, everything seems to be working fine. However, when attempting the following code snippet, it results in a 404 error w ...

Tips for showcasing unique validation error messages

My form includes a text area for the user to input JSON Code. If the entered text is not valid JSON, an error message should be displayed but unfortunately, it's not working as expected. Below is my custom validator code: import { AbstractControl, V ...

Error: Unable to execute function abc, it is not defined as a function in this context

Having trouble with a fronted or JavaScript issue where it can't find the defined function in the file. I'm working on integrating Apple Pay and need to call the back-end API based on a specific event. Here is my code. ACC.payDirect = { _autoload ...

How can you extract the property names of the first object in an array of objects?

I have an array of objects with the following structure and I want to extract the property names of the first object from this array without including the values. The desired result should only be ["Name", "Account", "Status"] ...

Leveraging WebWorkers in Typescript alongside Webpack and worker-loader without the need for custom loader strings

I've been trying to implement web workers with Typescript and Webpack's worker-loader smoothly. The documentation shows an example of achieving this using a custom module declaration, but it requires the webpack syntax worker-loader!./myWorker. ...

Using a generic name as a JSON key in a TypeScript file

I received data from an API call const tradingApiResponse = { Timestamp: "2024-01-15T12:00:00", Ack: "Success", Version: 1, Build: "1.0.0", UserDeliveryPreferenceArray: { NotificationEnable: [ { EventType: ...

Exploring TypeScript: Ensuring Compatibility of Types

Given two sets of TypeScript type definitions in string format: Set A: { a: string b: number } Set B: { a: string } Is there a way to programmatically determine if these two sets are compatible? In other words, can we assign variables defi ...

The absence of the 'classes' property in the MUI component type is causing an issue in Typescript Material UI

Simply put, typescript is giving me a hard time by complaining about the missing property classes on every material-ui component. Essentially, Typescript requires the presence of the classes property in nearly all material-ui components. Here is the error ...

Converting text data into JSON format using JavaScript

When working with my application, I am loading text data from a text file: The contents of this txt file are as follows: console.log(myData): ### Comment 1 ## Comment two dataone=1 datatwo=2 ## Comment N dataThree=3 I am looking to convert this data to ...

Encountering issues while retrieving date data from Firebase in Angular 6

this.qS = this.afDatabase.list('path', ref => { return ref.limitToLast(1000); }).snapshotChanges().map(changes => { return changes.map(c => ({ key1: c.payload.key,value1:c.payload.val() })); }); this.qS.subscribe(values =&g ...

Requirements for Method Decorators - The complete path of the class module using the decorator must be provided

Upon running the decorator (method decorators) function, we are provided with access to target, methodName, and descriptor. I am seeking the module path for target in this specific scenario. Essentially, I want the file path that points to the module that ...

Saving a JSON object to multiple JSON objects in TypeScript - The ultimate guide

Currently, I am receiving a JSON object named formDoc containing data from the backend. { "components": [ { "label": "Textfield1", "type": "textfield", "key": "textfield1", ...

The absence of color gradations in the TypeScript definition of MUI5 createTheme is worth noting

Seeking to personalize my theme colors in MUI5 using TypeScript, I am utilizing the createTheme function. This function requires a palette entry in its argument object, which TypeScript specifies should be of type PaletteOptions: https://i.stack.imgur.com ...

The module called "discord.js" does not have a component named "Intents" available for export

Issue with importing module '"discord.js"' - 'Intents' not exported. Here is the code in question: import dotenv from 'dotenv'; const bot = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents. ...

Dealing with Axios errors and handling the response object from the err.response

My React/Redux action creator sends a post request when creating a new user account. During server validation testing, I expect the err.response object to be defined in the catch block and return the validation error message. However, I am seeing the gene ...

The type '{} is not compatible with the type 'IProps'

In my current project, I am utilizing React alongside Formik and TypeScript. The code snippet below demonstrates my usage of the withFormik Higher Order Component (HOC) in my forms: import React from 'react'; // Libraries import........ import { ...

How come Typescript claims that X could potentially be undefined within useMemo, even though it has already been defined and cannot be undefined at this stage

I am facing an issue with the following code snippet: const productsWithAddonPrice = useMemo(() => { const addonsPrice = addonsSelected .map(id => { if (addons === undefined) { return 0} return addons.find(addon => addo ...

The ngOnInit child function fails to activate when trying to assign a fresh object

On the parent page, I am passing user data to a child component in this way: <ng-container *ngIf="leaderboard"> <app-leaderboard-preview [user]="user" (click)="goToLeaderboard()"></app-leaderboard-preview> </ng-container> In t ...

What is the process for setting the value of a TextField based on a Dropdown Selection?

I have a question regarding the code snippet below. I am wondering how to set the value of a specific TextField based on the selected item in a Dropdown component named ChildComponent. import * as React from "react"; import ChildComponent from './Ope ...