lokhmakov Posted March 22, 2019 Share Posted March 22, 2019 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); }); Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 22, 2019 Share Posted March 22, 2019 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. Quote Link to comment Share on other sites More sharing options...
lokhmakov Posted March 22, 2019 Author Share Posted March 22, 2019 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); }); ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 22, 2019 Share Posted March 22, 2019 Congratulations! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.