@glassly/miniapp 0.4.1 → 0.4.2-beta.1188
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/README.md +1 -0
- package/dist/display/index.d.ts +14 -4
- package/dist/display/index.d.ts.map +1 -1
- package/dist/display/index.js +11 -4
- package/dist/display/index.js.map +1 -1
- package/dist/display/measurer/script-detection.d.ts +1 -1
- package/dist/display/measurer/script-detection.d.ts.map +1 -1
- package/dist/display/measurer/script-detection.js +46 -10
- package/dist/display/measurer/script-detection.js.map +1 -1
- package/dist/display/profiles/index.d.ts +1 -1
- package/dist/display/profiles/index.d.ts.map +1 -1
- package/dist/display/profiles/index.js +1 -1
- package/dist/display/profiles/index.js.map +1 -1
- package/dist/display/profiles/nimo.d.ts +6 -0
- package/dist/display/profiles/nimo.d.ts.map +1 -1
- package/dist/display/profiles/nimo.js +17 -0
- package/dist/display/profiles/nimo.js.map +1 -1
- package/dist/display/scene/anchor.d.ts +16 -0
- package/dist/display/scene/anchor.d.ts.map +1 -0
- package/dist/display/scene/anchor.js +32 -0
- package/dist/display/scene/anchor.js.map +1 -0
- package/dist/display/scene/degrade.d.ts +25 -0
- package/dist/display/scene/degrade.d.ts.map +1 -0
- package/dist/display/scene/degrade.js +67 -0
- package/dist/display/scene/degrade.js.map +1 -0
- package/dist/display/scene/differ.d.ts +50 -0
- package/dist/display/scene/differ.d.ts.map +1 -0
- package/dist/display/scene/differ.js +129 -0
- package/dist/display/scene/differ.js.map +1 -0
- package/dist/display/scene/process.d.ts +41 -0
- package/dist/display/scene/process.d.ts.map +1 -0
- package/dist/display/scene/process.js +408 -0
- package/dist/display/scene/process.js.map +1 -0
- package/dist/display/scene/store.d.ts +48 -0
- package/dist/display/scene/store.d.ts.map +1 -0
- package/dist/display/scene/store.js +0 -0
- package/dist/display/scene/store.js.map +1 -0
- package/dist/display/scene/svgPath.d.ts +9 -0
- package/dist/display/scene/svgPath.d.ts.map +1 -0
- package/dist/display/scene/svgPath.js +167 -0
- package/dist/display/scene/svgPath.js.map +1 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +7 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/uiShim.d.ts +38 -0
- package/dist/host/uiShim.d.ts.map +1 -0
- package/dist/host/uiShim.js +383 -0
- package/dist/host/uiShim.js.map +1 -0
- package/dist/host/webviewGlobals.d.ts +60 -0
- package/dist/host/webviewGlobals.d.ts.map +1 -0
- package/dist/host/webviewGlobals.js +211 -0
- package/dist/host/webviewGlobals.js.map +1 -0
- package/dist/mirror/canvas.d.ts +13 -2
- package/dist/mirror/canvas.d.ts.map +1 -1
- package/dist/mirror/canvas.js +42 -21
- package/dist/mirror/canvas.js.map +1 -1
- package/dist/mirror/index.d.ts +1 -1
- package/dist/mirror/index.d.ts.map +1 -1
- package/dist/mirror/index.js.map +1 -1
- package/dist/modules/display.d.ts +10 -0
- package/dist/modules/display.d.ts.map +1 -1
- package/dist/modules/display.js.map +1 -1
- package/dist/ui/index.d.ts +1 -1
- package/dist/ui/index.js +1 -1
- package/package.json +6 -2
- package/src/display/index.ts +15 -3
- package/src/display/measurer/script-detection.ts +47 -10
- package/src/display/profiles/index.ts +1 -1
- package/src/display/profiles/nimo.ts +18 -0
- package/src/display/scene/anchor.ts +38 -0
- package/src/display/scene/degrade.ts +83 -0
- package/src/display/scene/differ.ts +152 -0
- package/src/display/scene/process.ts +443 -0
- package/src/display/scene/store.ts +0 -0
- package/src/display/scene/svgPath.ts +163 -0
- package/src/host/index.ts +14 -0
- package/src/host/uiShim.ts +389 -0
- package/src/host/webviewGlobals.ts +251 -0
- package/src/mirror/canvas.ts +86 -43
- package/src/mirror/index.ts +1 -1
- package/src/modules/display.ts +4 -0
- package/src/ui/index.ts +1 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder for the window.Glassly globals a host injects into every miniapp
|
|
3
|
+
* WebView before its content loads. The Glassly App and the desktop simulator
|
|
4
|
+
* both inject this script, so a page sees the same API surface in either.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {MiniappColorScheme} from "../globals"
|
|
8
|
+
|
|
9
|
+
export type {MiniappColorScheme}
|
|
10
|
+
|
|
11
|
+
export interface MiniappSafeArea {
|
|
12
|
+
top: number
|
|
13
|
+
bottom: number
|
|
14
|
+
left: number
|
|
15
|
+
right: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The host's capsule menu in the WebView's own coordinate space. `corner`
|
|
20
|
+
* floats over the page's top-right; `rail` sits in the iPhone Duo's status
|
|
21
|
+
* rail, inside the side safe-area inset and clear of the page content.
|
|
22
|
+
*/
|
|
23
|
+
export interface CapsuleMenuRect {
|
|
24
|
+
top: number
|
|
25
|
+
right: number
|
|
26
|
+
bottom: number
|
|
27
|
+
left: number
|
|
28
|
+
width: number
|
|
29
|
+
height: number
|
|
30
|
+
placement: "corner" | "rail"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface BuildWebviewGlobalsOptions {
|
|
34
|
+
platform: string
|
|
35
|
+
packageName?: string
|
|
36
|
+
capabilities?: string[]
|
|
37
|
+
miniappLocal?: boolean
|
|
38
|
+
miniappDeveloperMode?: boolean
|
|
39
|
+
safeAreaInsets: MiniappSafeArea
|
|
40
|
+
/** Where the host draws its capsule menu over this WebView. */
|
|
41
|
+
capsuleMenu: CapsuleMenuRect
|
|
42
|
+
/** Current host color scheme. Miniapps may follow this to match the phone. */
|
|
43
|
+
colorScheme?: MiniappColorScheme
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// CSS custom properties that mirror the safe-area and capsule menu data, so
|
|
47
|
+
// miniapp CSS (or Tailwind arbitrary values) can read them without touching
|
|
48
|
+
// JS: e.g. `style="margin-top: var(--glassly-capsule-top)"`.
|
|
49
|
+
function layoutCssVars(insets: MiniappSafeArea, capsule: CapsuleMenuRect): Record<string, string> {
|
|
50
|
+
return {
|
|
51
|
+
"--glassly-safe-top": `${insets.top}px`,
|
|
52
|
+
"--glassly-safe-bottom": `${insets.bottom}px`,
|
|
53
|
+
"--glassly-safe-left": `${insets.left}px`,
|
|
54
|
+
"--glassly-safe-right": `${insets.right}px`,
|
|
55
|
+
"--glassly-capsule-top": `${capsule.top}px`,
|
|
56
|
+
"--glassly-capsule-bottom": `${capsule.bottom}px`,
|
|
57
|
+
"--glassly-capsule-left": `${capsule.left}px`,
|
|
58
|
+
"--glassly-capsule-right": `${capsule.right}px`,
|
|
59
|
+
"--glassly-capsule-width": `${capsule.width}px`,
|
|
60
|
+
"--glassly-capsule-height": `${capsule.height}px`,
|
|
61
|
+
"--glassly-capsule-center-y": `${capsule.top + capsule.height / 2}px`,
|
|
62
|
+
// Right-side gutter to reserve so content doesn't slide under the
|
|
63
|
+
// capsule: its width + 16px breathing room, none when it sits in the rail.
|
|
64
|
+
"--glassly-capsule-gutter": `${capsule.placement === "rail" ? 0 : capsule.width + 16}px`,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* JS to inject into a running miniapp when the host's safe area or capsule
|
|
70
|
+
* menu moves (an iPhone Duo folding, or its rail's reserved regions growing):
|
|
71
|
+
* updates window.Glassly and the CSS variables, then fires
|
|
72
|
+
* `glassly:safe-area-change`.
|
|
73
|
+
*/
|
|
74
|
+
export function buildSafeAreaUpdateScript(insets: MiniappSafeArea, capsuleMenu: CapsuleMenuRect): string {
|
|
75
|
+
const sets = Object.entries(layoutCssVars(insets, capsuleMenu))
|
|
76
|
+
.map(([k, v]) => `document.documentElement.style.setProperty(${JSON.stringify(k)}, ${JSON.stringify(v)});`)
|
|
77
|
+
.join("\n ")
|
|
78
|
+
return `
|
|
79
|
+
window.Glassly = window.Glassly || {};
|
|
80
|
+
window.Glassly.safeAreaInsets = ${JSON.stringify(insets)};
|
|
81
|
+
window.Glassly.capsuleMenu = ${JSON.stringify(capsuleMenu)};
|
|
82
|
+
try {
|
|
83
|
+
${sets}
|
|
84
|
+
} catch (e) { /* ignore */ }
|
|
85
|
+
window.dispatchEvent(new Event("glassly:safe-area-change"));
|
|
86
|
+
true;
|
|
87
|
+
`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the JS string to inject into a miniapp WebView before its content
|
|
92
|
+
* loads. Sets window.Glassly with the standard fields miniapps read:
|
|
93
|
+
*
|
|
94
|
+
* window.Glassly = {
|
|
95
|
+
* platform, packageName?, capabilities, safeAreaInsets, capsuleMenu,
|
|
96
|
+
* miniappLocal?, miniappDeveloperMode?
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* Also defines a stub window.receiveNativeMessage so the miniapp can safely
|
|
100
|
+
* assign to it before the native bridge wires in.
|
|
101
|
+
*/
|
|
102
|
+
export function buildWebviewGlobalsScript(opts: BuildWebviewGlobalsOptions): string {
|
|
103
|
+
const globals: Record<string, unknown> = {
|
|
104
|
+
platform: opts.platform,
|
|
105
|
+
capabilities: opts.capabilities ?? ["share", "open_url", "copy_clipboard", "download"],
|
|
106
|
+
safeAreaInsets: opts.safeAreaInsets,
|
|
107
|
+
capsuleMenu: opts.capsuleMenu,
|
|
108
|
+
}
|
|
109
|
+
if (opts.packageName) globals.packageName = opts.packageName
|
|
110
|
+
if (opts.miniappLocal) globals.miniappLocal = true
|
|
111
|
+
if (opts.miniappDeveloperMode) globals.miniappDeveloperMode = true
|
|
112
|
+
if (opts.colorScheme) globals.colorScheme = opts.colorScheme
|
|
113
|
+
|
|
114
|
+
const cssVars = layoutCssVars(opts.safeAreaInsets, opts.capsuleMenu)
|
|
115
|
+
const cssVarsBlock = Object.entries(cssVars)
|
|
116
|
+
.map(([k, v]) => `${k}: ${v};`)
|
|
117
|
+
.join(" ")
|
|
118
|
+
|
|
119
|
+
// Console-tap shim: wrap console.log/warn/error/info/debug so each call
|
|
120
|
+
// also forwards a `dev_log` envelope back to the phone via
|
|
121
|
+
// ReactNativeWebView.postMessage. Dev mode forwards to the laptop's
|
|
122
|
+
// `glassly dev` terminal; non-dev mode falls back to the React
|
|
123
|
+
// Native log stream (Metro / Xcode console / adb logcat) so installed-
|
|
124
|
+
// miniapp errors are still inspectable when there's no laptop sidecar.
|
|
125
|
+
//
|
|
126
|
+
// Also tap window.onerror and window.onunhandledrejection so synchronous
|
|
127
|
+
// throws and unhandled promise rejections surface even when the bundle
|
|
128
|
+
// never gets a chance to call console.error itself.
|
|
129
|
+
const consoleTapBlock = `
|
|
130
|
+
(function() {
|
|
131
|
+
try {
|
|
132
|
+
var levels = ["log", "warn", "error", "info", "debug"];
|
|
133
|
+
var seen = new WeakSet();
|
|
134
|
+
function safeSerialize(arg) {
|
|
135
|
+
if (arg instanceof Error) return {__error: true, message: arg.message, stack: arg.stack};
|
|
136
|
+
if (arg === null || typeof arg !== "object") return arg;
|
|
137
|
+
try {
|
|
138
|
+
return JSON.parse(JSON.stringify(arg, function(k, v) {
|
|
139
|
+
if (v !== null && typeof v === "object") {
|
|
140
|
+
if (seen.has(v)) return "[Circular]";
|
|
141
|
+
seen.add(v);
|
|
142
|
+
}
|
|
143
|
+
return v;
|
|
144
|
+
}));
|
|
145
|
+
} catch (e) {
|
|
146
|
+
try { return String(arg); } catch (_) { return "[unserializable]"; }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
levels.forEach(function(level) {
|
|
150
|
+
var original = console[level];
|
|
151
|
+
if (typeof original !== "function") return;
|
|
152
|
+
console[level] = function() {
|
|
153
|
+
try { original.apply(console, arguments); } catch (_) {}
|
|
154
|
+
try {
|
|
155
|
+
var args = Array.prototype.slice.call(arguments).map(safeSerialize);
|
|
156
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
157
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
158
|
+
payload: {
|
|
159
|
+
type: "dev_log",
|
|
160
|
+
level: level,
|
|
161
|
+
args: args,
|
|
162
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
163
|
+
timestamp: Date.now()
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
} catch (_) { /* swallow */ }
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
} catch (_) { /* ignore */ }
|
|
171
|
+
|
|
172
|
+
// Surface synchronous errors and unhandled promise rejections via the
|
|
173
|
+
// same dev_log channel — many bundle-startup crashes never get to call
|
|
174
|
+
// console.error themselves.
|
|
175
|
+
try {
|
|
176
|
+
window.addEventListener("error", function(e) {
|
|
177
|
+
try {
|
|
178
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
179
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
180
|
+
payload: {
|
|
181
|
+
type: "dev_log",
|
|
182
|
+
level: "error",
|
|
183
|
+
args: ["[window.error] " + (e.message || "unknown"), {filename: e.filename, lineno: e.lineno, colno: e.colno, stack: e.error && e.error.stack}],
|
|
184
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
185
|
+
timestamp: Date.now()
|
|
186
|
+
}
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
} catch (_) {}
|
|
190
|
+
});
|
|
191
|
+
window.addEventListener("unhandledrejection", function(e) {
|
|
192
|
+
try {
|
|
193
|
+
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === "function") {
|
|
194
|
+
var reason = e.reason;
|
|
195
|
+
var serialized = reason instanceof Error
|
|
196
|
+
? {__error: true, message: reason.message, stack: reason.stack}
|
|
197
|
+
: (typeof reason === "object" ? JSON.stringify(reason) : String(reason));
|
|
198
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
199
|
+
payload: {
|
|
200
|
+
type: "dev_log",
|
|
201
|
+
level: "error",
|
|
202
|
+
args: ["[unhandledrejection]", serialized],
|
|
203
|
+
packageName: (window.Glassly && window.Glassly.packageName) || null,
|
|
204
|
+
timestamp: Date.now()
|
|
205
|
+
}
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
} catch (_) {}
|
|
209
|
+
});
|
|
210
|
+
} catch (_) {}
|
|
211
|
+
})();
|
|
212
|
+
`
|
|
213
|
+
|
|
214
|
+
// Lock zoom so miniapps feel like apps, not pages: force a non-scalable
|
|
215
|
+
// viewport meta (overriding whatever the miniapp shipped) and disable
|
|
216
|
+
// double-tap-zoom via CSS. Re-applies on DOMContentLoaded in case the
|
|
217
|
+
// miniapp re-writes <head> during boot.
|
|
218
|
+
return `
|
|
219
|
+
window.Glassly = ${JSON.stringify(globals)};
|
|
220
|
+
window.receiveNativeMessage = window.receiveNativeMessage || function() {};
|
|
221
|
+
(function() {
|
|
222
|
+
try {
|
|
223
|
+
var styleEl = document.createElement("style");
|
|
224
|
+
styleEl.setAttribute("data-glassly-injected", "1");
|
|
225
|
+
styleEl.textContent =
|
|
226
|
+
":root { ${cssVarsBlock} }" +
|
|
227
|
+
"html, body { touch-action: manipulation; -ms-content-zooming: none; }";
|
|
228
|
+
(document.head || document.documentElement).appendChild(styleEl);
|
|
229
|
+
} catch (e) { /* ignore */ }
|
|
230
|
+
|
|
231
|
+
function lockViewport() {
|
|
232
|
+
try {
|
|
233
|
+
var content = "width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover";
|
|
234
|
+
var existing = document.querySelector('meta[name="viewport"]');
|
|
235
|
+
if (existing) {
|
|
236
|
+
existing.setAttribute("content", content);
|
|
237
|
+
} else {
|
|
238
|
+
var meta = document.createElement("meta");
|
|
239
|
+
meta.setAttribute("name", "viewport");
|
|
240
|
+
meta.setAttribute("content", content);
|
|
241
|
+
(document.head || document.documentElement).appendChild(meta);
|
|
242
|
+
}
|
|
243
|
+
} catch (e) { /* ignore */ }
|
|
244
|
+
}
|
|
245
|
+
lockViewport();
|
|
246
|
+
document.addEventListener("DOMContentLoaded", lockViewport);
|
|
247
|
+
})();
|
|
248
|
+
${consoleTapBlock}
|
|
249
|
+
true;
|
|
250
|
+
`
|
|
251
|
+
}
|
package/src/mirror/canvas.ts
CHANGED
|
@@ -62,7 +62,38 @@ function canTween(previous: MirrorElement, next: MirrorElement): boolean {
|
|
|
62
62
|
)
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/**
|
|
65
|
+
/** What the renderer needs from its platform; the default is the browser's DOM. */
|
|
66
|
+
export interface MirrorCanvasEnvironment {
|
|
67
|
+
createCanvas(width: number, height: number): HTMLCanvasElement
|
|
68
|
+
loadImage(
|
|
69
|
+
src: string,
|
|
70
|
+
onLoad: (image: CanvasImageSource, width: number, height: number) => void,
|
|
71
|
+
onError: () => void,
|
|
72
|
+
): void
|
|
73
|
+
createPath(d?: string): Path2D
|
|
74
|
+
requestFrame(callback: () => void): number
|
|
75
|
+
cancelFrame(handle: number): void
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const domEnvironment: MirrorCanvasEnvironment = {
|
|
79
|
+
createCanvas(width, height) {
|
|
80
|
+
const surface = document.createElement("canvas")
|
|
81
|
+
surface.width = width
|
|
82
|
+
surface.height = height
|
|
83
|
+
return surface
|
|
84
|
+
},
|
|
85
|
+
loadImage(src, onLoad, onError) {
|
|
86
|
+
const image = new Image()
|
|
87
|
+
image.onload = () => onLoad(image, image.naturalWidth, image.naturalHeight)
|
|
88
|
+
image.onerror = onError
|
|
89
|
+
image.src = src
|
|
90
|
+
},
|
|
91
|
+
createPath: (d) => new Path2D(d),
|
|
92
|
+
requestFrame: (callback) => requestAnimationFrame(callback),
|
|
93
|
+
cancelFrame: (handle) => cancelAnimationFrame(handle),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Feed snapshots from session.mirror through your UI channel. A non-browser host passes its own environment. */
|
|
66
97
|
export class MirrorCanvasRenderer {
|
|
67
98
|
private frame: MirrorSnapshot | null = null
|
|
68
99
|
private poses = new Map<string, Pose>()
|
|
@@ -72,7 +103,10 @@ export class MirrorCanvasRenderer {
|
|
|
72
103
|
private disposed = false
|
|
73
104
|
private readonly ctx: CanvasRenderingContext2D
|
|
74
105
|
|
|
75
|
-
constructor(
|
|
106
|
+
constructor(
|
|
107
|
+
private readonly canvas: HTMLCanvasElement,
|
|
108
|
+
private readonly env: MirrorCanvasEnvironment = domEnvironment,
|
|
109
|
+
) {
|
|
76
110
|
const ctx = canvas.getContext("2d")
|
|
77
111
|
if (!ctx) throw new Error("Canvas 2D is unavailable")
|
|
78
112
|
this.ctx = ctx
|
|
@@ -138,9 +172,17 @@ export class MirrorCanvasRenderer {
|
|
|
138
172
|
this.draw()
|
|
139
173
|
}
|
|
140
174
|
|
|
175
|
+
redraw(): void {
|
|
176
|
+
this.draw()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get loading(): boolean {
|
|
180
|
+
return this.pending.size > 0
|
|
181
|
+
}
|
|
182
|
+
|
|
141
183
|
dispose(): void {
|
|
142
184
|
this.disposed = true
|
|
143
|
-
if (this.raf !== null)
|
|
185
|
+
if (this.raf !== null) this.env.cancelFrame(this.raf)
|
|
144
186
|
this.raf = null
|
|
145
187
|
this.poses.clear()
|
|
146
188
|
this.images.clear()
|
|
@@ -151,50 +193,51 @@ export class MirrorCanvasRenderer {
|
|
|
151
193
|
if (this.images.has(data)) return this.images.get(data) ?? null
|
|
152
194
|
if (this.pending.has(data)) return null
|
|
153
195
|
this.pending.add(data)
|
|
154
|
-
const image = new Image()
|
|
155
|
-
image.onload = () => {
|
|
156
|
-
this.pending.delete(data)
|
|
157
|
-
if (this.disposed) return
|
|
158
|
-
const content = this.frame?.content
|
|
159
|
-
if (
|
|
160
|
-
!(content?.kind === "bitmap" && content.data === data) &&
|
|
161
|
-
!(content?.kind === "scene" && content.elements.some((e) => e.data === data))
|
|
162
|
-
)
|
|
163
|
-
return
|
|
164
|
-
const surface = document.createElement("canvas")
|
|
165
|
-
surface.width = image.naturalWidth
|
|
166
|
-
surface.height = image.naturalHeight
|
|
167
|
-
const ctx = surface.getContext("2d", {willReadFrequently: true})
|
|
168
|
-
if (!ctx) return
|
|
169
|
-
ctx.drawImage(image, 0, 0)
|
|
170
|
-
const pixels = ctx.getImageData(0, 0, surface.width, surface.height)
|
|
171
|
-
for (let i = 0; i < pixels.data.length; i += 4) {
|
|
172
|
-
const alpha =
|
|
173
|
-
((0.2126 * pixels.data[i] + 0.7152 * pixels.data[i + 1] + 0.0722 * pixels.data[i + 2]) * pixels.data[i + 3]) /
|
|
174
|
-
255
|
|
175
|
-
pixels.data[i] = 0
|
|
176
|
-
pixels.data[i + 1] = 255
|
|
177
|
-
pixels.data[i + 2] = 136
|
|
178
|
-
pixels.data[i + 3] = alpha
|
|
179
|
-
}
|
|
180
|
-
ctx.putImageData(pixels, 0, 0)
|
|
181
|
-
this.images.set(data, surface)
|
|
182
|
-
this.draw()
|
|
183
|
-
}
|
|
184
|
-
image.onerror = () => {
|
|
185
|
-
this.pending.delete(data)
|
|
186
|
-
if (!this.disposed) this.images.set(data, null)
|
|
187
|
-
}
|
|
188
196
|
// Encoded images only: never fetch arbitrary URLs from scene data.
|
|
189
|
-
|
|
197
|
+
const src = data.startsWith("data:image/")
|
|
190
198
|
? data
|
|
191
199
|
: `data:image/${data.startsWith("Qk") ? "bmp" : "png"};base64,${data}`
|
|
200
|
+
this.env.loadImage(
|
|
201
|
+
src,
|
|
202
|
+
(image, width, height) => {
|
|
203
|
+
this.pending.delete(data)
|
|
204
|
+
if (this.disposed) return
|
|
205
|
+
const content = this.frame?.content
|
|
206
|
+
if (
|
|
207
|
+
!(content?.kind === "bitmap" && content.data === data) &&
|
|
208
|
+
!(content?.kind === "scene" && content.elements.some((e) => e.data === data))
|
|
209
|
+
)
|
|
210
|
+
return
|
|
211
|
+
const surface = this.env.createCanvas(width, height)
|
|
212
|
+
const ctx = surface.getContext("2d", {willReadFrequently: true})
|
|
213
|
+
if (!ctx) return
|
|
214
|
+
ctx.drawImage(image, 0, 0)
|
|
215
|
+
const pixels = ctx.getImageData(0, 0, surface.width, surface.height)
|
|
216
|
+
for (let i = 0; i < pixels.data.length; i += 4) {
|
|
217
|
+
const alpha =
|
|
218
|
+
((0.2126 * pixels.data[i] + 0.7152 * pixels.data[i + 1] + 0.0722 * pixels.data[i + 2]) *
|
|
219
|
+
pixels.data[i + 3]) /
|
|
220
|
+
255
|
|
221
|
+
pixels.data[i] = 0
|
|
222
|
+
pixels.data[i + 1] = 255
|
|
223
|
+
pixels.data[i + 2] = 136
|
|
224
|
+
pixels.data[i + 3] = alpha
|
|
225
|
+
}
|
|
226
|
+
ctx.putImageData(pixels, 0, 0)
|
|
227
|
+
this.images.set(data, surface)
|
|
228
|
+
this.draw()
|
|
229
|
+
},
|
|
230
|
+
() => {
|
|
231
|
+
this.pending.delete(data)
|
|
232
|
+
if (!this.disposed) this.images.set(data, null)
|
|
233
|
+
},
|
|
234
|
+
)
|
|
192
235
|
return null
|
|
193
236
|
}
|
|
194
237
|
|
|
195
238
|
private draw(): void {
|
|
196
239
|
if (this.disposed || !this.frame) return
|
|
197
|
-
if (this.raf !== null)
|
|
240
|
+
if (this.raf !== null) this.env.cancelFrame(this.raf)
|
|
198
241
|
this.raf = null
|
|
199
242
|
const {canvas, content} = this.frame
|
|
200
243
|
const w = Math.max(1, Math.round(canvas?.width ?? 1)),
|
|
@@ -218,7 +261,7 @@ export class MirrorCanvasRenderer {
|
|
|
218
261
|
animating ||= now < pose.start + pose.duration || now < pose.rotationStart + pose.rotationDuration
|
|
219
262
|
this.paint(pose.element, poseAt(pose, now))
|
|
220
263
|
}
|
|
221
|
-
if (animating) this.raf =
|
|
264
|
+
if (animating) this.raf = this.env.requestFrame(() => this.draw())
|
|
222
265
|
}
|
|
223
266
|
|
|
224
267
|
private paint(element: MirrorElement, g: SceneGeometry): void {
|
|
@@ -238,7 +281,7 @@ export class MirrorCanvasRenderer {
|
|
|
238
281
|
}
|
|
239
282
|
if (element.type === "text") {
|
|
240
283
|
if (g.stroke > 0) {
|
|
241
|
-
const border =
|
|
284
|
+
const border = this.env.createPath()
|
|
242
285
|
border.roundRect(g.x, g.y, g.w, g.h, Math.max(0, Math.min(g.radius, g.w / 2, g.h / 2)))
|
|
243
286
|
this.path(border, false, g)
|
|
244
287
|
}
|
|
@@ -265,7 +308,7 @@ export class MirrorCanvasRenderer {
|
|
|
265
308
|
})
|
|
266
309
|
return
|
|
267
310
|
}
|
|
268
|
-
let path =
|
|
311
|
+
let path = this.env.createPath()
|
|
269
312
|
const pts = g.points,
|
|
270
313
|
cx = g.x + g.w / 2,
|
|
271
314
|
cy = g.y + g.h / 2,
|
|
@@ -305,7 +348,7 @@ export class MirrorCanvasRenderer {
|
|
|
305
348
|
break
|
|
306
349
|
case "svg":
|
|
307
350
|
if (!element.d) return
|
|
308
|
-
path =
|
|
351
|
+
path = this.env.createPath(element.d)
|
|
309
352
|
ctx.translate(g.x, g.y)
|
|
310
353
|
ctx.scale(g.pathScale, g.pathScale)
|
|
311
354
|
break
|
package/src/mirror/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {MirrorCanvasRenderer} from "./canvas"
|
|
1
|
+
export {MirrorCanvasRenderer, type MirrorCanvasEnvironment} from "./canvas"
|
|
2
2
|
export type {MirrorSnapshot, MirrorContent, MirrorElement} from "./types"
|
package/src/modules/display.ts
CHANGED
|
@@ -117,6 +117,10 @@ export interface RenderTransition {
|
|
|
117
117
|
durationMs: number
|
|
118
118
|
/** Default "ease". */
|
|
119
119
|
easing?: RenderEasing
|
|
120
|
+
/** New to the screen: start this far off at ink 0 and tween into place. */
|
|
121
|
+
enter?: {dx?: number; dy?: number}
|
|
122
|
+
/** Leaving: tween this far off to ink 0; keep sending it until durationMs has passed. */
|
|
123
|
+
exit?: {dx?: number; dy?: number}
|
|
120
124
|
}
|
|
121
125
|
|
|
122
126
|
/**
|
package/src/ui/index.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* bus via `glassly.send(channel, payload)`.
|
|
14
14
|
*
|
|
15
15
|
* The `glassly` global is injected into every UI WebView at mount time
|
|
16
|
-
* by the host's `uiShim` (see `@glassly/
|
|
16
|
+
* by the host's `uiShim` (see `@glassly/miniapp/host`).
|
|
17
17
|
* This entry point exports its TypeScript declaration so authors get
|
|
18
18
|
* autocomplete and compile-time errors on channel names + payloads.
|
|
19
19
|
*/
|