Jump to content

extract canvas and stage


benoit-pixi
 Share

Recommended Posts

Hi,

I would like to extract my pixi+gsap animation to xx.png. It's work but png is bigger (in pixel) than my canvas, probably because I rotate some sprite.
With mask on stage/sprites/…, I get the same size problem.

I try to create a container and append my sprite, but my sprite are 'ParticleContainer', so as I understand it's not possible.
From the bunny example I make this, but I think there is a better way!

For now, I make a 8000x8000 centered masked graphic to fixe my extract ?
 

const app = new PIXI.Application({ backgroundColor: 0x111111,width:200,height:200});
document.body.appendChild(app.view);

function takeScreenshot() {
    wait = true;
    let img = app.renderer.extract.canvas(app.stage);
    let wx = (img.width - 200)/2;
    let wy = (img.height - 200)/2;
    console.log(wx,wy);
    let canvas1=document.createElement('canvas');
    let ctx1=canvas1.getContext('2d');
    canvas1.width=200;
    canvas1.height=200;
    ctx1.drawImage(img,wx,wy,200,200,0,0,200,200);
  

  canvas1.toBlob((b) => {
        const a = document.createElement('a');
        document.body.append(a);
        a.download = 'screenshot';
        a.href = URL.createObjectURL(b);
        a.click();
        a.remove();
    }, 'image/png');
}

const crop = new PIXI.Graphics();
crop.beginFill(0x000000);
crop.drawRect(0, 0, 200, 200);
crop.endFill();

app.stage.mask = crop;

const texture = PIXI.Texture.from('bunny.png');
const bunnyContainer = new PIXI.Container();
bunnyContainer.pivot.set(0.5, 0.5);

for (let i = 0; i < 25; i++) {
    const bunny = new PIXI.Sprite(texture);
    bunny.anchor.set(0.5);
    bunny.x = (i % 5) * 40;
    bunny.y = Math.floor(i / 5) * 40;
    bunnyContainer.addChild(bunny);
}

bunnyContainer.x = 100;
bunnyContainer.y = 100;
bunnyContainer.pivot.x = bunnyContainer.width / 2;
bunnyContainer.pivot.y = bunnyContainer.height / 2;


bunnyContainer.rotation = 45*Math.PI/180;


app.stage.addChild(bunnyContainer);

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...