Creating a Circle with Pixi.js v4 and Typerscript in IONIC 2

I have been attempting to create a custom class in TypeScript that utilizes PIXI.js to draw circles.

Below is the code for my home.ts class:

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CanvasAnimations } from '../../canvas/Canvas'

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

  canvas = new CanvasAnimations();
  @ViewChild('canvasWrapper') MyCanvas:ElementRef;
  @ViewChild('homeContent') HomeContent:ElementRef;

  constructor(public navCtrl: NavController) {}

  ionViewDidLoad() {
    this.canvas.setCanvas(this.MyCanvas, window.innerWidth, window.innerHeight);
    this.canvas.generateCircle();  
  }
}

Here is my CanvasAnimations class:

import { ElementRef } from '@angular/core';
import * as PIXI from 'pixi.js';

export class CanvasAnimations {

     stage = new PIXI.Container();

    constructor() { }

    setCanvas(canvasElement: ElementRef, windowWidth: number, windowHeight: number) {
        var renderer = PIXI.autoDetectRenderer(windowWidth, windowHeight, { backgroundColor: 0x00FF00, antialias: true });
        canvasElement.nativeElement.appendChild(renderer.view);
        renderer.render(this.stage);
    }

    generateCircle() {
        var circle = new PIXI.Graphics();
        circle.beginFill(0x000000);
        circle.drawCircle(0, 0, 100);
        circle.endFill();
        circle.x = 100;
        circle.y = 130;
        this.stage.addChild(circle);
    }
}

Despite successfully rendering the canvas, I am unable to see the circle and unsure of what may be causing this issue. Any advice or suggestions would be greatly appreciated.

Answer №1

After spending some time trying to figure it out, I finally solved the issue myself. It turns out that the code was perfectly fine all along. The mistake I made was forgetting to utilize the renderer method to draw the circle.

Eventually, I decided to move the renderer outside of the method and turned it into a class instance property, which resolved the issue completely.

Below is the updated code snippet:

export class canvasGenerator {

    canvasStage;
    renderer;

    constructor() {}

    createCanvas(anchorElement) {
        // Create the renderer
        this.renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight, { antialias: true, backgroundColor: 0xececec, resolution: 1 });
        this.renderer.view.style.position = "absolute";
        this.renderer.view.style.display = "block";

        // Add the canvas to the HTML document
        anchorElement.appendChild(this.renderer.view);
        this.canvasStage = new PIXI.Container();

        // Tell the `renderer` to `render` the `stage`
        this.renderer.render(this.canvasStage);

        this.generateCircle();
    }

    generateCircle() {
        var circle = new PIXI.Graphics();
        circle.beginFill(0x9966FF);
        circle.drawCircle(0, 0, 32);
        circle.endFill();
        circle.x = 64;
        circle.y = 130;
        this.canvasStage.addChild(circle);
        this.renderer.render(this.canvasStage);
    }
}

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

Experiencing difficulties when integrating the pdf-viewer-reactjs module within Next.js framework

I recently integrated the pdf-viewer-reactjs library into my Next.js project and encountered the following error: error - ./node_modules/pdfjs-dist/build/pdf.js 2094:26 Module parse failed: Unexpected token (2094:26) You may need an appropriate loader to h ...

Passing arrow functions for input validation rules to the Stancil component in Vue

I am attempting to implement an arrow function as input rules, similar to the setup in Vuetify at https://vuetifyjs.com/en/api/v-input/#rules. My approach involves passing rules within an array using the following code: <body> <div id="ap ...

The function "getElementsByClassName" in Javascript is malfunctioning

I have redesigned my website by implementing an interactive feature where users can click on a tree image to remove it and replace it with a number using JavaScript. Initially, I targeted the specific tree with the following code: document.getElementById( ...

Guide to creating a vertical handler that can be resized

Did you know that you can resize tables in http://www.jsfiddle.net? I'm curious about how to resize just the "Vertical Handler". Could someone share the source code with me? If possible, please provide an example on http://www.jsfiddle.net. ...

Designing a flawless CSS pattern for the online world

Currently, I am working on creating a seamless CSS pattern for the web. Even though this may seem impractical and nonsensical, I find joy in experimenting with it. View my progress here After successfully designing my first tile, my next challenge is to ...

Separate HTTP responses into multiple Observables

I need assistance in splitting a HTTP response into different Variables/Observables. The returned data is in JSON format: [ { "status": [ { "id": 1, "value": "active"}, { "id": 2, "value": "inactive"} ] }, { "type": [ ...

Tips for accurately typing a "Type Mapping" function

In my code, I have a specific type designed for a function that takes one type I as input and returns another type O as output. Here is how it currently looks: export interface IFunctionalMapping<I, O, K extends keyof O> { [prop: Extract<O[K], ...

Error encountered when trying to access children components in Typescript, even though React.FC is being

I am facing an issue with a child component that has the following structure: interface ChildProps extends AnotherInterface{ route: string, exitAction: ActionCreatorWithoutPayload, } const ChildComponent:FC<ChildProps> = ({title, shape, rout ...

Is there a way to utilize nodemon without having to install it through npm?

I'm having trouble with my network not allowing me to use npm install. Is there another way for me to install and use nodemon? I've noticed that Node only runs after setting PATH variables on Windows. I attempted to set the path for nodemon, but ...

What is the best way to create a universal function that can return a promise while also passing along event

I created a specialized function that waits for an "EventEmitter" (although it's not completely accurate as I want to use it on other classes that have once but don't inherit from EventEmitter): export function waitEvent(emitter: { once: Function ...

Retrieving user input through JavaScript and transmitting information using Ajax

UPDATE: Issue resolved by changing name="demo_name" and similar attributes on my form to id="demo_name". Thanks @Jill I'm attempting to save contact form data into a MySQL database using jquery's ajax feature. I'm new to this, and while it& ...

How can I incorporate dynamic fields into a Typescript type/interface?

In my Typescript interface, I have a predefined set of fields like this: export interface Data { date_created: string; stamp: string; } let myData: Data; But now I need to incorporate "dynamic" fields that can be determined only at runtime. This me ...

The dimensions of the body are set to 100vh in height and width. However, the div inside this body has a width of either 100vh or 100%, but it is not

I am trying to create a div element that has a width equal to the viewport of the browser. However, I am encountering issues when applying the CSS property width:100vh to the body element. Here is my code snippet: body { font-family: Staatliches; fo ...

Embed a Telegram account onto an HTML page using the <iframe> element

Is there a way to display the personal Telegram account in an iframe tag? I experimented with the code below, but unfortunately it did not produce the desired result: <iframe src="https://telegram.me/joinchat/BfNEij9CbDh03kwXacO5OA"></iframe> ...

Utilize react-router-dom for conditional rendering based on button clicks

When the user types in "user" in the text box, they will be directed to the user page. If they type "admin", they will be redirected to the admin page. This code belongs to me. constructor(props) { super(props); this.state = { userType : 0 ...

Sending multiple text fields along with a file to an API using Angular

I'm currently facing a challenge with uploading a file to an API endpoint that I created using Laravel, using an HTML form. The upload works fine when only the file is involved. However, I also need to include some basic text fields in the request. T ...

What are the steps to integrate Bootswatch into an Angular application?

I have a project using Angular and I'm looking to incorporate Bootswatch into it. However, I'm facing difficulties with the installation process. Initially, I attempted to install the module by running the command: npm i bootswatch After that, I ...

Using Vue to cycle through an array of objects, and emphasize the chosen item by clicking on it

I have to display an array of objects in the DOM by using map method. Each item has a @click event listener where I want to highlight it with a CSS class 'hover'. The desired functionality is to mimic a menu system where only the clicked item sho ...

The filter functionality isn't functioning properly when attempting to filter an array of objects within a Vuex action

I have a collection of objects that is accessed through a getter. I am using this getter inside an action to filter the items, but no matter what I do, the filtering does not seem to work and it ends up returning all the mapped item IDs. filterItems({ gett ...

Context failing to refresh value upon route changes

My current context setup is as follows: import { createContext, ReactNode, useState } from "react"; type props = { children: ReactNode; }; type GlobalContextType = { name: string; setName: (value: string) => void; }; export const Glob ...