@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
58 lines (43 loc) • 1.76 kB
JavaScript
async function generatePoster() {
try {
const { screenshot2, onStart } = await import("@needle-tools/engine");
// Keep in sync with og:image:width meta tags
// https://developers.facebook.com/docs/sharing/best-practices/
const width = 1080;
const height = 1080;
return new Promise(res => {
onStart(async context => {
if (context.lodsManager.manager) {
await context.lodsManager.manager.awaitLoading({ frames: 5, maxPromisesPerObject: 2, waitForFirstCapture: true });
}
const mimeType = "image/webp";
// We're reading back as a blob here because that's async, and doesn't seem
// to stress the GPU so much on memory-constrained devices.
const blob = await screenshot2({ context, width, height, mimeType, type: "blob" });
// We can only send a DataURL, so we need to convert it back here.
const dataUrl = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = function () {
resolve(reader.result);
};
reader.onloadend = function () {
resolve(null);
};
reader.readAsDataURL(blob);
});
res(dataUrl);
}, { once: true });
})
}
catch (e) {
console.error(e);
return null;
}
}
if (import.meta.hot) {
async function run() {
const blob = await generatePoster();
import.meta.hot.send("needle:screenshot", { data: blob });
}
run();
}