question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load image assets for PIXI.js in conjunction with Webpack

See original GitHub issue

I’m trying to load image assets for PIXI.js in a React project of mine that uses Webpack. Here is my whole ‘Menu’ component class:

import React from 'react';
import styles from './Menu.css';
import backgroundUrl from './background.jpg';
require('pixi.js')

class Menu extends React.Component {    

    componentDidMount () {

        // Get reference to canvas element
        this.canvas = this.refs.canvas;        

        // Load assets                
        PIXI.loader.add([
            {
                name: 'background',
                url: backgroundUrl
            }
        ]).load(this.init());

    }

    init () {

        // Create background sprite
        const background = new PIXI.Sprite(PIXI.loader.resources.background.texture);

        // DEBUG: Height and width is always '1'
        console.log('Background width: ' + background.width);
        console.log('Background height: ' + background.height);

        // Setup renderer
        this.renderer = PIXI.autoDetectRenderer(background.width, background.height);
        this.renderer.autoResize = true;
        this.renderer.resize(window.innerWidth, window.innerHeight);
        this.canvas.appendChild(this.renderer.view);

        // Create stage
        this.stage = new PIXI.Container();                

        // Add background to stage
        this.stage.addChild(background)

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

    }

    render () {

        return (
           <div className={styles.canvas} ref='canvas'></div>
        );
    }

}

export default Menu;

Now as you can see, I’m trying to load the image background.jpg into PIXI with the loader.add() function. It just doesn’t work. In my debug console logs I can see that the height and width of the image is 1. Also, when I view the URL for the background image, it all seems fine. Any tips?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

5reactions
hayesmakercommented, Aug 6, 2018

this is obsolete now… and we need a way to load any asset which we’re bundling with webpack

5reactions
staff0rdcommented, Dec 5, 2016

load expects a function as its parameter, however you were actually calling init() thereby passing the result of it (void) to load. If init didn’t have references to this inside it, you could use the following to pass the function;

.load(this.init);

Notice the lack of parentheses.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load image assets for PIXI.js in conjunction with Webpack
I'm trying to load image assets for PIXI.js in a React project of mine that uses Webpack. Here is my whole 'Menu' component...
Read more >
Getting Started with Pixi.js and webpack - James Kiefer
Using webpack and Pixi.js together is very simple. ... Install webpack; Configure webpack plugins; Install Pixi.js; Pixi.js Basic Usage ...
Read more >
Pixi's docs - PixiJS API Documentation
Documentation for PixiJS library.
Read more >
typescript - Creating Textures loaded via file loader in pixi.js
I tried to load the image using the filepath of the png created by webpack, still the texture remains undefined . I am...
Read more >
Basic elements with Pixi.js: Primitives, Text and Sprites
The best way to load one or more external resources is by using the Loader class offered by Pixi.js. For our convenience, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found