P
P
Pavel Mikhalovsky2018-04-05 00:55:11
JavaScript
Pavel Mikhalovsky, 2018-04-05 00:55:11

How to load texture on canvas using PIXI.js?

I'm trying to load the texture, it's in the folder next to the script. I'm using nodejs using the vue framework. It turns out only to change the background color of the canvas.

var canvas = this.$refs['canvas']
    var renderer = new PIXI.CanvasRenderer(this.$refs['canvas'].width, this.$refs['canvas'].height, {view: canvas})
    renderer.backgroundColor = 0x061639
    var stage = new PIXI.Container()
    var texture = PIXI.Texture.fromImage('HertzLamp.png')
    console.log(texture)
    var lamp = new PIXI.Sprite(texture)
    stage.addChild(lamp)
    renderer.render(stage)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Solovyev, 2018-04-05
@pavel9609

Pixie has a handy PIXI.loaders

const loader = PIXI.loader;

loader
  .add('first', '1.jpg')
  .add('second', '2.jpg');

loader.load((loader, resources) => {
  let scene = new PIXI.Container();

  let bg = new PIXI.Sprite(resources.first.texture);

  bg.anchor.x = 0.5;
  bg.anchor.y = 0.5;
  bg.position.x = renderer.width / 2; 
  bg.position.y = renderer.height / 2;

  scene.addChild(bg);
  renderer.render(scene);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question