lokhmakov

Extract with smooth edges

Recommended Posts

Hello,

Is there way to extract webgl canvas to image with smooth edges?

https://codesandbox.io/s/r54q7365yq

import "./styles.css";

import * as PIXI from "pixi.js";
import * as Viewport from "pixi-viewport";

const app = new PIXI.Application(window.innerWidth, window.innerHeight, {
  antialias: true,
  resolution: 1,
  backgroundColor: "gray",
  view: document.getElementById("app-canvas")
});

const texture = PIXI.Texture.from("/public/bg.jpg");
const sprite = new PIXI.Sprite(texture);
app.stage.addChild(sprite);

const sampleTexture = new PIXI.Texture.from("public/wall.png");
const sample = new PIXI.Sprite(sampleTexture);
sample.x = 100;
sample.y = 100;
sample.width = 300;
sample.height = 300;

const mask = new PIXI.Graphics();
mask.beginFill(0xffffff);
mask.drawCircle(window.innerWidth / 2, window.innerWidth / 2, 100);
mask.endFill();

sample.mask = mask;

app.stage.addChild(sample);

// download canvas
const downloadBtn = document.getElementById("download-btn");
downloadBtn.addEventListener("click", e => {
  const imageUrl = app.renderer.plugins.extract.base64(app.stage);
  let link = document.createElement("a");
  link.href = imageUrl;
  link.download = "picture.png";
  link.style.display = "none";
  document.body.appendChild(link);
  link.click();
  link.parentNode.removeChild(link);
});

 

Share this post


Link to post
Share on other sites

yeah, dont pass anything to base64() and it'll take image from the screen framebuffer that has AA enabled.

The problem is that you can take it only immidiately after app.render() because of special cases regarding browsers.

I'm afraid that full answer on that will take 30 minutes from me and i dont have that time. Please read the extract plugin code in pixijs github if what i said is not enough.

Share this post


Link to post
Share on other sites

Thanks.

Next solution works fine:

downloadBtn.addEventListener("click", e => {
  app.render();
  const imageUrl = app.renderer.plugins.extract.base64();
  let link = document.createElement("a");
  link.href = imageUrl;
  link.download = "picture.png";
  link.style.display = "none";
  document.body.appendChild(link);
  link.click();
  link.parentNode.removeChild(link);
});

 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.