@glassly/miniapp 0.4.1-beta.1159 → 0.4.1-beta.1173
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 +13 -3
- package/dist/display/index.d.ts.map +1 -1
- package/dist/display/index.js +10 -3
- package/dist/display/index.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/ui/index.d.ts +1 -1
- package/dist/ui/index.js +1 -1
- package/package.json +6 -2
- package/src/display/index.ts +14 -3
- 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/ui/index.ts +1 -1
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/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
|
*/
|