create-bloop 0.0.20 → 0.0.21
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.
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
import { Backends, Colors, type Toodle } from "@bloopjs/toodle";
|
|
2
2
|
|
|
3
|
-
export function
|
|
3
|
+
export function setupGlitchEffect(toodle: Toodle): () => void {
|
|
4
|
+
let glitchEffect: Backends.PostProcess | null = null;
|
|
5
|
+
let glitchTimeout: number = -1;
|
|
6
|
+
let glitchQueued = false;
|
|
7
|
+
|
|
8
|
+
if (toodle.backend.type === "webgpu") {
|
|
9
|
+
glitchEffect = createChromaticAberrationEffect(toodle);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function doGlitchEffect() {
|
|
13
|
+
if (!glitchEffect) {
|
|
14
|
+
return console.warn(
|
|
15
|
+
`No glitch effect available - are we running on webgl2?`,
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log("doGlitchEffect", performance.now(), {
|
|
20
|
+
hasFocus: document.hasFocus(),
|
|
21
|
+
});
|
|
22
|
+
// Queue effect if page isn't visible or window doesn't have focus (IDE covering browser)
|
|
23
|
+
if (document.visibilityState === "hidden" || !document.hasFocus()) {
|
|
24
|
+
console.log("queueing glitch");
|
|
25
|
+
glitchQueued = true;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
console.log("doing glitch immediately");
|
|
29
|
+
if (glitchTimeout > 0) {
|
|
30
|
+
clearTimeout(glitchTimeout);
|
|
31
|
+
}
|
|
32
|
+
toodle.postprocess = glitchEffect;
|
|
33
|
+
glitchTimeout = setTimeout(() => {
|
|
34
|
+
toodle.postprocess = null;
|
|
35
|
+
}, 1000);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
document.addEventListener("visibilitychange", () => {
|
|
39
|
+
console.log(
|
|
40
|
+
"visibilityStateChange",
|
|
41
|
+
document.visibilityState,
|
|
42
|
+
performance.now(),
|
|
43
|
+
);
|
|
44
|
+
if (document.visibilityState === "visible" && glitchQueued) {
|
|
45
|
+
glitchQueued = false;
|
|
46
|
+
doGlitchEffect();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Handle case where another app (like IDE) covers the browser window
|
|
51
|
+
// visibilitychange doesn't fire for this, but focus does when clicking back
|
|
52
|
+
window.addEventListener("focus", () => {
|
|
53
|
+
console.log("window focus", performance.now());
|
|
54
|
+
if (glitchQueued) {
|
|
55
|
+
glitchQueued = false;
|
|
56
|
+
doGlitchEffect();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return doGlitchEffect;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function createChromaticAberrationEffect(
|
|
64
|
+
toodle: Toodle,
|
|
65
|
+
): Backends.PostProcess {
|
|
4
66
|
if (!(toodle.backend instanceof Backends.WebGPUBackend)) {
|
|
5
67
|
throw new Error("Post-processing requires WebGPU backend");
|
|
6
68
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import "./style.css";
|
|
2
2
|
import { Toodle } from "@bloopjs/toodle";
|
|
3
3
|
import { start } from "@bloopjs/web";
|
|
4
|
-
import {
|
|
5
|
-
import { draw } from "./draw";
|
|
4
|
+
import { draw as drawFn } from "./draw";
|
|
6
5
|
import { game } from "./game";
|
|
6
|
+
import { setupGlitchEffect } from "./glitchEffect";
|
|
7
|
+
|
|
8
|
+
let draw = drawFn;
|
|
7
9
|
|
|
8
10
|
// boot up the game
|
|
9
11
|
const app = await start({
|
|
@@ -14,17 +16,6 @@ const app = await start({
|
|
|
14
16
|
},
|
|
15
17
|
});
|
|
16
18
|
|
|
17
|
-
// HMR support
|
|
18
|
-
if (import.meta.hot) {
|
|
19
|
-
import.meta.hot.accept("./game", async (newModule) => {
|
|
20
|
-
await app.acceptHmr(newModule?.game);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// import.meta.hot.accept("./draw", async (newModule) => {
|
|
24
|
-
// draw = newModule?.draw;
|
|
25
|
-
// });
|
|
26
|
-
}
|
|
27
|
-
|
|
28
19
|
const canvas = app.canvas;
|
|
29
20
|
if (!canvas) throw new Error("No canvas element found");
|
|
30
21
|
|
|
@@ -87,14 +78,17 @@ window.addEventListener("keydown", (e) => {
|
|
|
87
78
|
}
|
|
88
79
|
});
|
|
89
80
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
81
|
+
const doGlitchEffect = setupGlitchEffect(toodle);
|
|
82
|
+
|
|
83
|
+
// HMR support
|
|
84
|
+
if (import.meta.hot) {
|
|
85
|
+
import.meta.hot.accept("./game", async (newModule) => {
|
|
86
|
+
doGlitchEffect();
|
|
87
|
+
await app.acceptHmr(newModule?.game);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
import.meta.hot.accept("./draw", async (newModule) => {
|
|
91
|
+
doGlitchEffect();
|
|
92
|
+
draw = newModule?.draw;
|
|
99
93
|
});
|
|
100
94
|
}
|