@skathi-native/cli 0.1.0-beta.1 → 0.1.0-beta.3

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.
Files changed (53) hide show
  1. package/aot/host-globals.json +105 -0
  2. package/aot/native.ts +142 -0
  3. package/aot/pin.ts +67 -0
  4. package/aot/toolchain.lock.json +17 -0
  5. package/aot/transform.ts +69 -0
  6. package/cli/aot-build.ts +189 -0
  7. package/cli/build-app.ts +67 -25
  8. package/cli/config.ts +44 -101
  9. package/cli/create.ts +107 -234
  10. package/cli/help.ts +159 -0
  11. package/cli/host-fetch.ts +87 -48
  12. package/cli/host.ts +18 -8
  13. package/cli/sk.ts +35 -110
  14. package/cli/skpk.ts +4 -0
  15. package/package.json +4 -2
  16. package/templates/icon.png +0 -0
  17. package/templates/minimal/.vscode/settings.json +3 -0
  18. package/templates/minimal/assets/hello.txt +1 -0
  19. package/templates/minimal/src/App.vue +142 -0
  20. package/templates/minimal/src/main.ts +4 -0
  21. package/templates/showcase/.vscode/settings.json +3 -0
  22. package/templates/showcase/assets/hello.txt +1 -0
  23. package/templates/showcase/assets/media/aurora.png +0 -0
  24. package/templates/showcase/assets/media/clip.mp4 +0 -0
  25. package/templates/showcase/assets/media/sticker.tgs +0 -0
  26. package/templates/showcase/src/App.vue +128 -0
  27. package/templates/showcase/src/components/Card.vue +41 -0
  28. package/templates/showcase/src/components/Icon.vue +29 -0
  29. package/templates/showcase/src/components/Page.vue +41 -0
  30. package/templates/showcase/src/components/Titlebar.vue +132 -0
  31. package/templates/showcase/src/engine.ts +10 -0
  32. package/templates/showcase/src/icons.ts +22 -0
  33. package/templates/showcase/src/main.ts +7 -0
  34. package/templates/showcase/src/pages/AudioPage.vue +121 -0
  35. package/templates/showcase/src/pages/FilesPage.vue +87 -0
  36. package/templates/showcase/src/pages/FormsPage.vue +138 -0
  37. package/templates/showcase/src/pages/GraphicsPage.vue +154 -0
  38. package/templates/showcase/src/pages/HomePage.vue +140 -0
  39. package/templates/showcase/src/pages/LocalePage.vue +71 -0
  40. package/templates/showcase/src/pages/MediaPage.vue +85 -0
  41. package/templates/showcase/src/pages/MotionPage.vue +199 -0
  42. package/templates/showcase/src/pages/NetworkPage.vue +104 -0
  43. package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
  44. package/templates/showcase/src/pages/StoragePage.vue +104 -0
  45. package/templates/showcase/src/pages/StylesPage.vue +199 -0
  46. package/templates/showcase/src/pages/SystemPage.vue +89 -0
  47. package/templates/showcase/src/pages/TrayPage.vue +66 -0
  48. package/templates/showcase/src/pages/WindowPage.vue +128 -0
  49. package/templates/showcase/src/product.ts +2 -0
  50. package/templates/showcase/src/router.ts +149 -0
  51. package/templates/showcase/src/stores/counter.ts +9 -0
  52. package/templates/showcase/src/stores/desktop.ts +115 -0
  53. package/templates/showcase/src/styles.css +140 -0
@@ -0,0 +1,142 @@
1
+ <script setup lang="ts">
2
+ import { computed, onMounted, ref } from "vue";
3
+
4
+ const productName = "{{productName}}";
5
+ const initial = productName.charAt(0).toUpperCase();
6
+
7
+ const count = ref(0);
8
+ const clicks = computed(() => `${count.value} ${count.value === 1 ? "click" : "clicks"}`);
9
+
10
+ // A relative URL resolves against the app root in dev and against the packaged assets in a build.
11
+ const greeting = ref("");
12
+ onMounted(async () => {
13
+ if (typeof fetch !== "function") return;
14
+ const res = await fetch("assets/hello.txt");
15
+ if (res.ok) greeting.value = (await res.text()).trim();
16
+ });
17
+ </script>
18
+
19
+ <template>
20
+ <main class="page">
21
+ <section class="card">
22
+ <div class="logo">{{ initial }}</div>
23
+ <h1 class="title">{{ productName }}</h1>
24
+ <p class="lead">Vue on a native GPU renderer. Edit <span class="code">src/App.vue</span> and save.</p>
25
+ <div class="actions">
26
+ <button class="button" @click="count++">Count up</button>
27
+ <button class="button ghost" :disabled="count === 0" @click="count = 0">Reset</button>
28
+ </div>
29
+ <div class="count">{{ clicks }}</div>
30
+ </section>
31
+ <p v-if="greeting" class="hint">{{ greeting }}</p>
32
+ </main>
33
+ </template>
34
+
35
+ <style>
36
+ .page {
37
+ height: 100%;
38
+ display: flex;
39
+ flex-direction: column;
40
+ align-items: center;
41
+ justify-content: center;
42
+ gap: 20px;
43
+ padding: 32px;
44
+ color: #f4f4f5;
45
+ font-family: "Segoe UI", system-ui, sans-serif;
46
+ }
47
+ .card {
48
+ width: 380px;
49
+ display: flex;
50
+ flex-direction: column;
51
+ align-items: center;
52
+ gap: 14px;
53
+ padding: 36px 32px 28px;
54
+ border-radius: 20px;
55
+ background: #18181bb3;
56
+ border: 1px solid #ffffff14;
57
+ box-shadow: 0 24px 60px #00000066;
58
+ }
59
+ .logo {
60
+ width: 64px;
61
+ height: 64px;
62
+ border-radius: 18px;
63
+ display: flex;
64
+ align-items: center;
65
+ justify-content: center;
66
+ font-size: 30px;
67
+ font-weight: 700;
68
+ color: #ffffff;
69
+ background: linear-gradient(135deg, #42b883, #2f6fd8);
70
+ box-shadow: 0 10px 28px #2f6fd855;
71
+ animation: float 4s ease-in-out infinite;
72
+ }
73
+ @keyframes float {
74
+ 50% {
75
+ transform: translateY(-5px);
76
+ }
77
+ }
78
+ .title {
79
+ margin: 0;
80
+ font-size: 26px;
81
+ font-weight: 700;
82
+ }
83
+ .lead {
84
+ margin: 0;
85
+ font-size: 14px;
86
+ line-height: 21px;
87
+ color: #a1a1aa;
88
+ text-align: center;
89
+ }
90
+ .code {
91
+ font-family: "Cascadia Mono", Consolas, monospace;
92
+ font-size: 13px;
93
+ color: #d4d4d8;
94
+ }
95
+ .actions {
96
+ display: flex;
97
+ gap: 10px;
98
+ margin-top: 8px;
99
+ }
100
+ .button {
101
+ padding: 9px 20px;
102
+ border-radius: 10px;
103
+ border-width: 0;
104
+ font-size: 14px;
105
+ font-weight: 600;
106
+ color: #ffffff;
107
+ background: linear-gradient(135deg, #42b883, #2f6fd8);
108
+ box-shadow: 0 6px 18px #2f6fd840;
109
+ cursor: pointer;
110
+ transition:
111
+ transform 120ms ease,
112
+ box-shadow 120ms ease;
113
+ }
114
+ .button:hover {
115
+ transform: translateY(-1px);
116
+ box-shadow: 0 10px 24px #2f6fd866;
117
+ }
118
+ .button:active {
119
+ transform: scale(0.97);
120
+ }
121
+ .button.ghost {
122
+ color: #d4d4d8;
123
+ background: #ffffff10;
124
+ box-shadow: none;
125
+ }
126
+ .button.ghost:hover {
127
+ background: #ffffff1c;
128
+ }
129
+ .button:disabled {
130
+ opacity: 0.4;
131
+ cursor: default;
132
+ }
133
+ .count {
134
+ font-size: 13px;
135
+ color: #71717a;
136
+ }
137
+ .hint {
138
+ margin: 0;
139
+ font-size: 12px;
140
+ color: #71717a;
141
+ }
142
+ </style>
@@ -0,0 +1,4 @@
1
+ import { createApp } from "vue";
2
+ import App from "./App.vue";
3
+
4
+ createApp(App).mount();
@@ -0,0 +1,3 @@
1
+ {
2
+ "css.customData": ["./node_modules/@skathi-native/runtime/css-data.json"]
3
+ }
@@ -0,0 +1 @@
1
+ Loaded at run time from assets/hello.txt, packaged with {{productName}}.
@@ -0,0 +1,128 @@
1
+ <script setup lang="ts">
2
+ import { appDevtools } from "@skathi-native/runtime";
3
+ import { computed } from "vue";
4
+ import { useRoute } from "vue-router";
5
+ import Icon from "./components/Icon.vue";
6
+ import Titlebar from "./components/Titlebar.vue";
7
+ import { pages } from "./router";
8
+
9
+ const devtools = appDevtools.available && appDevtools.isEnabled();
10
+ const route = useRoute();
11
+ const current = computed(() => pages.find((p) => p.path === route.path)?.title ?? "");
12
+
13
+ const sections = computed(() => {
14
+ const out = new Map<string, typeof pages>();
15
+ for (const page of pages) out.set(page.section, [...(out.get(page.section) ?? []), page]);
16
+ return [...out];
17
+ });
18
+ </script>
19
+
20
+ <template>
21
+ <div class="app">
22
+ <Titlebar :page="current" />
23
+ <div class="body">
24
+ <nav class="sidebar">
25
+ <div v-for="[section, items] in sections" :key="section" class="section">
26
+ <div v-if="section" class="section-title">{{ section }}</div>
27
+ <RouterLink v-for="page in items" :key="page.path" :to="page.path" class="link">
28
+ <Icon :name="page.icon" />
29
+ <span>{{ page.title }}</span>
30
+ </RouterLink>
31
+ </div>
32
+ <div v-if="devtools" class="foot">F12 opens the devtools</div>
33
+ </nav>
34
+ <main class="content">
35
+ <RouterView v-slot="{ Component }">
36
+ <Transition name="page" mode="out-in">
37
+ <component :is="Component" />
38
+ </Transition>
39
+ </RouterView>
40
+ </main>
41
+ </div>
42
+ </div>
43
+ </template>
44
+
45
+ <style>
46
+ .app {
47
+ height: 100%;
48
+ display: flex;
49
+ flex-direction: column;
50
+ background: #0b0b10a6;
51
+ color: var(--text);
52
+ font-family: var(--font);
53
+ }
54
+ .body {
55
+ flex: 1;
56
+ min-height: 0;
57
+ display: flex;
58
+ }
59
+ .sidebar {
60
+ width: 210px;
61
+ display: flex;
62
+ flex-direction: column;
63
+ gap: 14px;
64
+ padding: 14px 10px;
65
+ border-right: 1px solid var(--line);
66
+ background: #00000026;
67
+ overflow-y: auto;
68
+ }
69
+ .section {
70
+ display: flex;
71
+ flex-direction: column;
72
+ gap: 2px;
73
+ }
74
+ .section-title {
75
+ padding: 0 10px 4px;
76
+ font-size: 11px;
77
+ font-weight: 600;
78
+ letter-spacing: 0.6px;
79
+ text-transform: uppercase;
80
+ color: var(--text-faint);
81
+ }
82
+ .link {
83
+ height: 32px;
84
+ display: flex;
85
+ align-items: center;
86
+ gap: 10px;
87
+ padding: 0 10px;
88
+ border-radius: 8px;
89
+ font-size: 13px;
90
+ color: var(--text-muted);
91
+ cursor: pointer;
92
+ transition:
93
+ background-color 120ms ease,
94
+ color 120ms ease;
95
+ }
96
+ .link:hover {
97
+ background: var(--surface-hover);
98
+ color: var(--text);
99
+ }
100
+ .link.router-link-exact-active {
101
+ background: var(--accent-soft);
102
+ color: #ffffff;
103
+ }
104
+ .foot {
105
+ margin-top: auto;
106
+ padding: 8px 10px 0;
107
+ font-size: 11px;
108
+ color: var(--text-faint);
109
+ }
110
+ .content {
111
+ flex: 1;
112
+ min-width: 0;
113
+ overflow-y: auto;
114
+ }
115
+ .page-enter-active,
116
+ .page-leave-active {
117
+ transition:
118
+ opacity 140ms ease,
119
+ transform 140ms ease;
120
+ }
121
+ .page-enter-from {
122
+ opacity: 0;
123
+ transform: translateY(6px);
124
+ }
125
+ .page-leave-to {
126
+ opacity: 0;
127
+ }
128
+ </style>
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ defineProps<{ title: string; api?: string }>();
3
+ </script>
4
+
5
+ <template>
6
+ <div class="card">
7
+ <div class="top">
8
+ <span class="title">{{ title }}</span>
9
+ <span v-if="api" class="api">{{ api }}</span>
10
+ </div>
11
+ <slot />
12
+ </div>
13
+ </template>
14
+
15
+ <style scoped>
16
+ .card {
17
+ display: flex;
18
+ flex-direction: column;
19
+ gap: 12px;
20
+ padding: 18px;
21
+ border-radius: var(--radius);
22
+ border: 1px solid var(--line);
23
+ background: var(--surface);
24
+ }
25
+ .top {
26
+ display: flex;
27
+ align-items: center;
28
+ justify-content: space-between;
29
+ gap: 12px;
30
+ }
31
+ .title {
32
+ font-size: 14px;
33
+ font-weight: 600;
34
+ color: var(--text);
35
+ }
36
+ .api {
37
+ font-family: var(--mono);
38
+ font-size: 11px;
39
+ color: var(--text-faint);
40
+ }
41
+ </style>
@@ -0,0 +1,29 @@
1
+ <script setup lang="ts">
2
+ import { icons, type IconName } from "../icons";
3
+
4
+ defineProps<{ name: IconName }>();
5
+ </script>
6
+
7
+ <template>
8
+ <svg
9
+ class="icon"
10
+ xmlns="http://www.w3.org/2000/svg"
11
+ width="16"
12
+ height="16"
13
+ viewBox="0 0 24 24"
14
+ fill="none"
15
+ stroke="currentColor"
16
+ stroke-width="1.8"
17
+ stroke-linecap="round"
18
+ stroke-linejoin="round"
19
+ >
20
+ <path :d="icons[name]" />
21
+ </svg>
22
+ </template>
23
+
24
+ <style scoped>
25
+ .icon {
26
+ width: 16px;
27
+ height: 16px;
28
+ }
29
+ </style>
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ defineProps<{ title: string; lead: string }>();
3
+ </script>
4
+
5
+ <template>
6
+ <section class="page">
7
+ <header class="head">
8
+ <h1 class="title">{{ title }}</h1>
9
+ <p class="lead">{{ lead }}</p>
10
+ </header>
11
+ <slot />
12
+ </section>
13
+ </template>
14
+
15
+ <style scoped>
16
+ .page {
17
+ display: flex;
18
+ flex-direction: column;
19
+ gap: 16px;
20
+ padding: 28px 32px 40px;
21
+ }
22
+ .head {
23
+ display: flex;
24
+ flex-direction: column;
25
+ gap: 6px;
26
+ margin-bottom: 4px;
27
+ }
28
+ .title {
29
+ margin: 0;
30
+ font-size: 24px;
31
+ font-weight: 700;
32
+ color: var(--text);
33
+ }
34
+ .lead {
35
+ margin: 0;
36
+ max-width: 640px;
37
+ font-size: 14px;
38
+ line-height: 21px;
39
+ color: var(--text-muted);
40
+ }
41
+ </style>
@@ -0,0 +1,132 @@
1
+ <script setup lang="ts">
2
+ // sk.config.json sets "titleBarStyle": "hidden", so the app paints its own caption: `window-drag: move` makes the
3
+ // strip drag the window, and `--window-control` on each button gives it the OS behaviour (snap layouts on maximize).
4
+ import { appWindow } from "@skathi-native/runtime";
5
+ import { onMounted, onUnmounted, ref } from "vue";
6
+ import { productName } from "../product";
7
+
8
+ defineProps<{ page: string }>();
9
+
10
+ const hostControls = ref(appWindow.chrome.hostControls);
11
+ const maximized = ref(appWindow.isMaximized());
12
+ const read = () => {
13
+ hostControls.value = appWindow.chrome.hostControls;
14
+ maximized.value = appWindow.isMaximized();
15
+ };
16
+
17
+ onMounted(() => {
18
+ navigator.windowControlsOverlay.addEventListener("geometrychange", read);
19
+ appWindow.addEventListener("maximize", read);
20
+ appWindow.addEventListener("unmaximize", read);
21
+ });
22
+ onUnmounted(() => {
23
+ navigator.windowControlsOverlay.removeEventListener("geometrychange", read);
24
+ appWindow.removeEventListener("maximize", read);
25
+ appWindow.removeEventListener("unmaximize", read);
26
+ });
27
+ </script>
28
+
29
+ <template>
30
+ <header class="titlebar">
31
+ <div class="brand">
32
+ <span class="mark">{{ productName.charAt(0) }}</span>
33
+ <span class="name">{{ productName }}</span>
34
+ <span class="sep">/</span>
35
+ <span class="page">{{ page }}</span>
36
+ </div>
37
+ <div v-if="!hostControls" class="controls">
38
+ <div class="control minimize" title="Minimize">
39
+ <svg width="10" height="10" viewBox="0 0 10 10"><path d="M0 5h10" stroke="currentColor" /></svg>
40
+ </div>
41
+ <div class="control maximize" :title="maximized ? 'Restore' : 'Maximize'">
42
+ <svg width="10" height="10" viewBox="0 0 10 10" fill="none">
43
+ <path v-if="maximized" d="M2.5 2.5V.5h7v7h-2 M.5 2.5h7v7h-7z" stroke="currentColor" />
44
+ <path v-else d="M.5 .5h9v9h-9z" stroke="currentColor" />
45
+ </svg>
46
+ </div>
47
+ <div class="control close" title="Close">
48
+ <svg width="10" height="10" viewBox="0 0 10 10"><path d="M0 0l10 10M10 0 0 10" stroke="currentColor" /></svg>
49
+ </div>
50
+ </div>
51
+ </header>
52
+ </template>
53
+
54
+ <style scoped>
55
+ .titlebar {
56
+ height: env(titlebar-area-height, 40px);
57
+ padding-left: env(titlebar-area-x, 0px);
58
+ display: flex;
59
+ align-items: center;
60
+ justify-content: space-between;
61
+ window-drag: move;
62
+ user-select: none;
63
+ border-bottom: 1px solid var(--line);
64
+ }
65
+ @media (display-mode: fullscreen) {
66
+ .titlebar {
67
+ display: none;
68
+ }
69
+ }
70
+ .brand {
71
+ display: flex;
72
+ align-items: center;
73
+ gap: 10px;
74
+ padding-left: 14px;
75
+ font-size: 12px;
76
+ }
77
+ .mark {
78
+ width: 20px;
79
+ height: 20px;
80
+ border-radius: 6px;
81
+ display: flex;
82
+ align-items: center;
83
+ justify-content: center;
84
+ background: linear-gradient(135deg, var(--accent), #b48cff);
85
+ color: #ffffff;
86
+ font-size: 11px;
87
+ font-weight: 700;
88
+ }
89
+ .name {
90
+ font-weight: 600;
91
+ color: var(--text);
92
+ }
93
+ .sep {
94
+ color: var(--text-faint);
95
+ }
96
+ .page {
97
+ color: var(--text-muted);
98
+ }
99
+ .controls {
100
+ height: 100%;
101
+ display: flex;
102
+ window-drag: none;
103
+ }
104
+ .control {
105
+ width: 46px;
106
+ height: 100%;
107
+ display: flex;
108
+ align-items: center;
109
+ justify-content: center;
110
+ color: var(--text-muted);
111
+ transition:
112
+ background-color 120ms ease,
113
+ color 120ms ease;
114
+ }
115
+ .control:hover {
116
+ background: var(--surface-strong);
117
+ color: var(--text);
118
+ }
119
+ .minimize {
120
+ --window-control: minimize;
121
+ }
122
+ .maximize {
123
+ --window-control: maximize;
124
+ }
125
+ .close {
126
+ --window-control: close;
127
+ }
128
+ .close:hover {
129
+ background: #c42b1c;
130
+ color: #ffffff;
131
+ }
132
+ </style>
@@ -0,0 +1,10 @@
1
+ /** Calls `draw` once per painted frame until the returned function is called. */
2
+ export function everyFrame(draw: (time: number) => void): () => void {
3
+ let handle = requestAnimationFrame(function frame(time) {
4
+ draw(time);
5
+ handle = requestAnimationFrame(frame);
6
+ });
7
+ return () => cancelAnimationFrame(handle);
8
+ }
9
+
10
+ export const describe = (e: unknown) => (e instanceof Error ? e.message : String(e));
@@ -0,0 +1,22 @@
1
+ // 24×24 stroke paths, drawn with `stroke="currentColor"` so an icon takes its colour from the text.
2
+ export const icons = {
3
+ home: "M3 10.5 12 3l9 7.5V21h-6v-6H9v6H3z",
4
+ window: "M3 5h18v14H3z M3 9h18 M6 7h.01",
5
+ tray: "M6 17V10a6 6 0 0 1 12 0v7 M4 17h16 M10 21h4",
6
+ files: "M6 2h8l5 5v15H6z M14 2v5h5 M9 13h6 M9 17h6",
7
+ storage:
8
+ "M4 6c0-1.7 3.6-3 8-3s8 1.3 8 3-3.6 3-8 3-8-1.3-8-3z M4 6v12c0 1.7 3.6 3 8 3s8-1.3 8-3V6 M4 12c0 1.7 3.6 3 8 3s8-1.3 8-3",
9
+ network: "M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z M3 12h18 M12 3c5 5 5 13 0 18 M12 3c-5 5-5 13 0 18",
10
+ media: "M3 5h18v14H3z M3 16l5-5 4 4 3-3 6 6 M16 9h.01",
11
+ audio: "M9 18V5l12-2v13 M9 18a3 3 0 1 1-6 0 3 3 0 0 1 6 0z M21 16a3 3 0 1 1-6 0 3 3 0 0 1 6 0z",
12
+ graphics: "M12 3l9 16H3z M12 9v4",
13
+ motion: "M12 3v4 M12 17v4 M3 12h4 M17 12h4 M6 6l2.5 2.5 M15.5 15.5 18 18 M6 18l2.5-2.5 M15.5 8.5 18 6",
14
+ styles:
15
+ "M12 3a9 9 0 1 0 0 18c1.1 0 2-.9 2-2 0-.5-.2-1-.5-1.3-.3-.4-.5-.8-.5-1.3 0-1.1.9-2 2-2h2.4A4.6 4.6 0 0 0 21 9.8C21 6 17 3 12 3z M7.5 11h.01 M10 7h.01 M15 7.5h.01",
16
+ forms: "M4 7h16v10H4z M8 10v4 M17 4v3 M17 17v3",
17
+ locale: "M4 5h9 M8.5 3v2 M6 5c1 3 3.5 5.5 6.5 6.5 M11 5c-1 3.5-3.5 6.5-7 8 M13 21l4-9 4 9 M14.5 18h5",
18
+ permissions: "M12 3l8 3v6c0 4.5-3.4 8.3-8 9-4.6-.7-8-4.5-8-9V6z M9 12l2 2 4-4",
19
+ system: "M4 5h16v11H4z M8 20h8 M12 16v4 M8 9l2 2-2 2 M13 13h3",
20
+ } as const;
21
+
22
+ export type IconName = keyof typeof icons;
@@ -0,0 +1,7 @@
1
+ import { createApp } from "vue";
2
+ import { createPinia } from "pinia";
3
+ import App from "./App.vue";
4
+ import { router } from "./router";
5
+ import "./styles.css";
6
+
7
+ createApp(App).use(createPinia()).use(router).mount();
@@ -0,0 +1,121 @@
1
+ <script setup lang="ts">
2
+ import { onUnmounted, ref } from "vue";
3
+ import Card from "../components/Card.vue";
4
+ import Page from "../components/Page.vue";
5
+ import { everyFrame } from "../engine";
6
+
7
+ const NOTES = [
8
+ ["C", 261.63],
9
+ ["D", 293.66],
10
+ ["E", 329.63],
11
+ ["G", 392.0],
12
+ ["A", 440.0],
13
+ ["C", 523.25],
14
+ ["D", 587.33],
15
+ ["E", 659.25],
16
+ ] as const;
17
+ const WAVES = ["sine", "triangle", "square", "sawtooth"];
18
+ const BARS = 32;
19
+
20
+ const wave = ref("triangle");
21
+ const levels = ref<number[]>(Array.from({ length: BARS }, () => 0));
22
+
23
+ let engine: { ctx: AudioContext; tap: AnalyserNode; stop: () => void } | null = null;
24
+
25
+ // Created on the first note, so the audio device is not opened until something plays.
26
+ function start() {
27
+ const ctx = new AudioContext();
28
+ const tap = ctx.createAnalyser();
29
+ tap.fftSize = 128;
30
+ tap.connect(ctx.destination);
31
+ const bins = new Uint8Array(tap.frequencyBinCount);
32
+ const stop = everyFrame(() => {
33
+ tap.getByteFrequencyData(bins);
34
+ levels.value = levels.value.map((_, i) => (bins[i] ?? 0) / 255);
35
+ });
36
+ return { ctx, tap, stop };
37
+ }
38
+
39
+ function play(frequency: number) {
40
+ engine ??= start();
41
+ const { ctx, tap } = engine;
42
+ const now = ctx.currentTime;
43
+ const osc = ctx.createOscillator();
44
+ const gain = ctx.createGain();
45
+ osc.type = wave.value;
46
+ osc.frequency.value = frequency;
47
+ gain.gain.setValueAtTime(0.0001, now);
48
+ gain.gain.exponentialRampToValueAtTime(0.3, now + 0.02);
49
+ gain.gain.exponentialRampToValueAtTime(0.0001, now + 1.2);
50
+ osc.connect(gain);
51
+ gain.connect(tap);
52
+ osc.start(now);
53
+ osc.stop(now + 1.25);
54
+ }
55
+
56
+ onUnmounted(() => {
57
+ engine?.stop();
58
+ engine?.ctx.suspend();
59
+ });
60
+ </script>
61
+
62
+ <template>
63
+ <Page title="Audio" lead="The Web Audio API on a native audio engine: oscillators, gain envelopes and an analyser.">
64
+ <Card title="Synth" api="AudioContext">
65
+ <div class="keys">
66
+ <button v-for="([name, hz], i) in NOTES" :key="i" class="key" @mousedown="play(hz)">{{ name }}</button>
67
+ </div>
68
+ <div class="row">
69
+ <button v-for="w in WAVES" :key="w" class="btn" :class="{ on: wave === w }" @click="wave = w">{{ w }}</button>
70
+ </div>
71
+ <div class="spectrum">
72
+ <div v-for="(level, i) in levels" :key="i" class="bar" :style="{ height: `${4 + level * 96}px` }" />
73
+ </div>
74
+ </Card>
75
+ </Page>
76
+ </template>
77
+
78
+ <style scoped>
79
+ .keys {
80
+ display: flex;
81
+ gap: 6px;
82
+ }
83
+ .key {
84
+ flex: 1;
85
+ height: 120px;
86
+ display: flex;
87
+ align-items: flex-end;
88
+ justify-content: center;
89
+ padding-bottom: 12px;
90
+ border-radius: 10px;
91
+ border: 1px solid var(--line);
92
+ background: linear-gradient(180deg, #ffffff14, #ffffff06);
93
+ color: var(--text-muted);
94
+ font-size: 13px;
95
+ font-weight: 600;
96
+ cursor: pointer;
97
+ transition:
98
+ background-color 80ms ease,
99
+ transform 80ms ease;
100
+ }
101
+ .key:hover {
102
+ border-color: var(--line-strong);
103
+ color: var(--text);
104
+ }
105
+ .key:active {
106
+ transform: translateY(2px);
107
+ background: var(--accent-soft);
108
+ }
109
+ .spectrum {
110
+ height: 100px;
111
+ display: flex;
112
+ align-items: flex-end;
113
+ gap: 3px;
114
+ }
115
+ .bar {
116
+ flex: 1;
117
+ border-radius: 3px;
118
+ background: linear-gradient(0deg, var(--accent-strong), #b48cff);
119
+ transition: height 60ms linear;
120
+ }
121
+ </style>