21rw
21rw

Reputation: 1126

How to convert a graphic to a sprite in pixijs

Is there a way to convert a graphic to a sprite? I have a graphic containing a single rectangle, and would like to convert it to a sprite to enable complex animations.

I have tried doing

let p= new Graphics();
p.beginFill(0x000000);
p.lineStyle(0);
p.drawCircle(100, 100, 10);
p.endFill();

const t = RenderTexture.create(p.width, p.height);
renderer.render(p, t);

const sprite = new Sprite(t);

However this is not working.

Upvotes: 7

Views: 9403

Answers (1)

Aivaras
Aivaras

Reputation: 176

var gr = new PIXI.Graphics();  
        gr.beginFill(0xFFFFFF);
        gr.lineStyle(0);
        gr.drawCircle(30, 30, 30);
        gr.endFill();

var texture = renderer.generateTexture(gr);
var circle = new PIXI.Sprite(texture);

app.stage.addChild(circle);

Upvotes: 15

Related Questions