@needle-tools/engine 4.8.3-next.1a41273 → 4.8.3-next.66cfa29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,40 +1,46 @@
1
1
 
2
2
  async function generatePoster() {
3
- const { screenshot2 } = await import("@needle-tools/engine");
4
3
 
5
4
  try {
6
- const needleEngine = document.querySelector("needle-engine");
7
- if (!needleEngine) return null;
5
+ const { screenshot2, onStart } = await import("@needle-tools/engine");
8
6
 
9
7
  // Keep in sync with og:image:width meta tags
10
8
  // https://developers.facebook.com/docs/sharing/best-practices/
11
9
  const width = 1080;
12
10
  const height = 1080;
13
- const context = await needleEngine.getContext();
14
11
 
15
- // wait a fixed time for e.g. fonts to load
16
- while(context.time.frameCount < 2)
17
- await new Promise((resolve) => setTimeout(resolve, 200));
12
+ return new Promise(res => {
13
+ onStart(async context => {
18
14
 
19
- const mimeType = "image/webp";
15
+ if (context.lodsManager.manager) {
16
+ await context.lodsManager.manager.awaitLoading({ frames: 20, maxPromisesPerObject: 2 });
17
+ }
20
18
 
21
- // We're reading back as a blob here because that's async, and doesn't seem
22
- // to stress the GPU so much on memory-constrained devices.
23
- const blob = await screenshot2({context, width, height, mimeType, type: "blob"});
24
-
25
- // We can only send a DataURL, so we need to convert it back here.
26
- const dataUrl = await new Promise((resolve, reject) => {
27
- const reader = new FileReader();
28
- reader.onload = function() {
29
- resolve(reader.result);
30
- };
31
- reader.onloadend = function() {
32
- resolve(null);
33
- };
34
- reader.readAsDataURL(blob);
35
- });
19
+ const mimeType = "image/webp";
20
+
21
+ // We're reading back as a blob here because that's async, and doesn't seem
22
+ // to stress the GPU so much on memory-constrained devices.
23
+ const blob = await screenshot2({ context, width, height, mimeType, type: "blob" });
24
+
25
+ // We can only send a DataURL, so we need to convert it back here.
26
+ const dataUrl = await new Promise((resolve, reject) => {
27
+ const reader = new FileReader();
28
+ reader.onload = function () {
29
+ resolve(reader.result);
30
+ };
31
+ reader.onloadend = function () {
32
+ resolve(null);
33
+ };
34
+ reader.readAsDataURL(blob);
35
+ });
36
+
37
+
38
+ res(dataUrl);
39
+
40
+
41
+ }, { once: true });
42
+ })
36
43
 
37
- return dataUrl;
38
44
  }
39
45
  catch (e) {
40
46
  console.error(e);
@@ -42,33 +48,11 @@ async function generatePoster() {
42
48
  }
43
49
  }
44
50
 
45
- async function sendPoster() {
46
- const blob = await generatePoster();
47
- import.meta.hot.send("needle:screenshot", { data: blob });
48
- }
49
51
 
50
- // communicate via vite
51
52
  if (import.meta.hot) {
52
- // wait for needle engine to be fully loaded
53
- const needleEngine = document.querySelector("needle-engine");
54
- needleEngine?.addEventListener("loadfinished", () => {
55
- // wait a moment
56
- setTimeout(() => {
57
- sendPoster();
58
- }, 200);
59
- });
60
-
61
- // for debugging: build extra button with dev-only options
62
- /*
63
- var button = document.createElement("button");
64
- button.id = "send-msg";
65
- button.innerHTML = "Generate Poster";
66
- button.style.position = "fixed";
67
- button.style.zIndex = "9999";
68
- document.body.appendChild(button);
69
-
70
- document.querySelector("#send-msg").addEventListener("click", async () => {
71
- sendPoster();
72
- });
73
- */
53
+ async function run() {
54
+ const blob = await generatePoster();
55
+ import.meta.hot.send("needle:screenshot", { data: blob });
56
+ }
57
+ run();
74
58
  }
@@ -36,7 +36,7 @@ export const needlePoster = (command, config, userSettings) => {
36
36
  }
37
37
  try {
38
38
  const targetPath = "./" + getPosterPath();
39
- console.debug(`Received poster, saving to ${targetPath}`);
39
+ console.debug(`[needle:poster] Saving automatic poster to ${targetPath}`);
40
40
  // remove data:image/png;base64, from the beginning of the string
41
41
  if (targetPath.endsWith(".webp"))
42
42
  data.data = data.data.replace(/^data:image\/webp;base64,/, "");
@@ -47,10 +47,9 @@ export const needlePoster = (command, config, userSettings) => {
47
47
  fs.mkdirSync(dir, { recursive: true })
48
48
  }
49
49
  fs.writeFileSync(targetPath, Buffer.from(data.data, "base64"));
50
- console.debug("Saved poster to file");
51
50
  }
52
51
  catch (err) {
53
- console.error("Failed to save poster", err.message);
52
+ console.error("[needle:poster] Failed to save poster", err.message);
54
53
  }
55
54
  });
56
55
  },
@@ -237,11 +237,15 @@ export class PeerHandle extends EventDispatcher<any> {
237
237
  this.context = context;
238
238
  this.id = id;
239
239
  this.setupPeer();
240
+ const isGetUserMediaWriteable = Object.getOwnPropertyDescriptor(navigator, "getUserMedia")?.writable;
240
241
  try {
241
- navigator["getUserMedia"] = (
242
- navigator["getUserMedia"] || navigator["webkitGetUserMedia"] ||
243
- navigator["mozGetUserMedia"] || navigator["msGetUserMedia"]
244
- );
242
+ if (isGetUserMediaWriteable) {
243
+ navigator["getUserMedia"] = (
244
+ navigator["getUserMedia"] || navigator["webkitGetUserMedia"] ||
245
+ navigator["mozGetUserMedia"] || navigator["msGetUserMedia"]
246
+ );
247
+ }
248
+ else if (debug) console.warn("[PeerJs] getUserMedia is not writable");
245
249
  }
246
250
  catch (err) {
247
251
  if (debug) console.error("[PeerJs] Error setting getUserMedia", err);