Load image assets for PIXI.js in conjunction with Webpack
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:1
- Comments:6
Top 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 >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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

this is obsolete now… and we need a way to load any asset which we’re bundling with webpack
loadexpects a function as its parameter, however you were actually callinginit()thereby passing the result of it (void) toload. Ifinitdidn’t have references tothisinside it, you could use the following to pass the function;Notice the lack of parentheses.