@openbone/player 0.4.0 → 0.4.2
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 +6 -4
- package/dist/bundle/webview.d.ts +1 -1
- package/dist/bundle/webview.js +2 -1
- package/dist/element.d.ts +6 -1
- package/dist/element.js +22 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/player.d.ts +53 -2
- package/dist/player.js +130 -18
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -69,14 +69,16 @@ Images and sounds load relative to the document's URL. The element is 400 × 300
|
|
|
69
69
|
| `state-machine` | first | State machine to run, by name or id. |
|
|
70
70
|
| `animation` | | Play this animation instead of a state machine. |
|
|
71
71
|
| `skin` | default skin | Skin (outfit) by name or id. Changing it switches the outfit without reloading. |
|
|
72
|
-
| `fit` | `contain` | `contain`, `cover` or `none` (actual size, centred). |
|
|
72
|
+
| `fit` | `contain` | `contain`, `cover`, `auto` (cover when it crops at most 15% of a side, contain otherwise) or `none` (actual size, centred). |
|
|
73
|
+
| `clip` | on | Only the artboard is drawn: art past its edges is cut off, the bars around it stay transparent and clicks there are ignored. `clip="false"` draws the whole canvas. |
|
|
74
|
+
| `fullscreen-fit` | `auto` | Fit while the player (or a parent) is full screen. `auto` fills the screen when that crops only a little of the artboard and shows all of it otherwise; `cover`, `contain`, `none`, or `same` to keep `fit`. |
|
|
73
75
|
| `renderer` | `webgl` | `webgl` or `canvas`. WebGL falls back to Canvas 2D when WebGL2 is not available. |
|
|
74
76
|
| `background` | the file's | Any CSS colour (`#fff`, `rebeccapurple`, `rgb(0 0 0 / 50%)`, …), or `transparent` / `none`. |
|
|
75
77
|
| `autoplay` | on | `autoplay="false"` loads and draws the first frame, then waits for `play()`. |
|
|
76
78
|
| `scripts` | on | `scripts="none"` never runs the document's scripts. |
|
|
77
79
|
| `audio` / `muted` | on | `audio="off"` disables sound; `muted` loads sounds but starts muted. |
|
|
78
80
|
|
|
79
|
-
Changing `skin`
|
|
81
|
+
Changing `skin`, `clip` or `fullscreen-fit` applies in place. Changing any other attribute, including setting `src` after the element is
|
|
80
82
|
on the page, reloads the player with the new settings.
|
|
81
83
|
|
|
82
84
|
### Element methods and events
|
|
@@ -128,14 +130,14 @@ player.speed = 0.5;
|
|
|
128
130
|
| `src` | A URL, or an already parsed `OpenBoneDocument`. |
|
|
129
131
|
| `baseUrl` | Folder for relative image and audio paths (default: the folder of `src`). |
|
|
130
132
|
| `resolveImageUrl(src)`, `resolveAudioUrl(asset)` | Map asset paths to URLs yourself (overrides `baseUrl`). |
|
|
131
|
-
| `stateMachine`, `animation`, `skin`, `autoplay`, `fit`, `renderer`, `background` | As the attributes above. `background: null` is transparent
|
|
133
|
+
| `stateMachine`, `animation`, `skin`, `autoplay`, `fit`, `clip`, `fullscreenFit`, `renderer`, `background` | As the attributes above. `background: null` is transparent; `fullscreenFit: null` keeps `fit`. |
|
|
132
134
|
| `scripts` | `"quickjs"` (default when the document has scripts), `"none"`, or your own `ScriptHost`. |
|
|
133
135
|
| `audio` | `true` (default), `false` or `"muted"`. |
|
|
134
136
|
| `audioContext` | Share an existing `AudioContext` instead of creating one. |
|
|
135
137
|
| `onEvent`, `onStateChange`, `onLoad` | Callbacks. |
|
|
136
138
|
|
|
137
139
|
Members: `ready` (promise), `play()`, `pause()`, `playing`, `speed` (sounds follow it), `setInput`, `getInput`,
|
|
138
|
-
`fire`, `setText`, `setSkin`, `skins`, `playAnimation(name, loop?)`, `setFit(fit)`, `setMuted(muted)`, `muted`,
|
|
140
|
+
`fire`, `setText`, `setSkin`, `skins`, `playAnimation(name, loop?)`, `setFit(fit)`, `setFullscreenFit(fit)`, `setClip(clip)`, `currentFit`, `setMuted(muted)`, `muted`,
|
|
139
141
|
`audio`, `instance` (the runtime `OpenBoneInstance`), `stateMachine`, `doc`, `clientToSkeleton(clientX, clientY)`,
|
|
140
142
|
`boundingBoxes()` and `dispose()`.
|
|
141
143
|
|
package/dist/bundle/webview.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - `?src=url` in the page URL (fetched; needs http(s), not file://)
|
|
7
7
|
* - `window.OPENBONE_SRC` (a URL or a parsed document)
|
|
8
8
|
* Options come from `data-*` attributes on `<body>`: data-state-machine, data-animation, data-skin, data-fit,
|
|
9
|
-
* data-background ("none" = transparent), data-renderer.
|
|
9
|
+
* data-background ("none" = transparent), data-renderer, data-clip ("false" draws past the artboard's edges).
|
|
10
10
|
*
|
|
11
11
|
* It exposes `window.OpenBone` (see bridge.ts for the message format).
|
|
12
12
|
*/
|
package/dist/bundle/webview.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* - `?src=url` in the page URL (fetched; needs http(s), not file://)
|
|
7
7
|
* - `window.OPENBONE_SRC` (a URL or a parsed document)
|
|
8
8
|
* Options come from `data-*` attributes on `<body>`: data-state-machine, data-animation, data-skin, data-fit,
|
|
9
|
-
* data-background ("none" = transparent), data-renderer.
|
|
9
|
+
* data-background ("none" = transparent), data-renderer, data-clip ("false" draws past the artboard's edges).
|
|
10
10
|
*
|
|
11
11
|
* It exposes `window.OpenBone` (see bridge.ts for the message format).
|
|
12
12
|
*/
|
|
@@ -132,6 +132,7 @@ function start() {
|
|
|
132
132
|
animation: opt("animation"),
|
|
133
133
|
skin: opt("skin"),
|
|
134
134
|
fit: opt("fit") ?? "contain",
|
|
135
|
+
clip: opt("clip") !== "false",
|
|
135
136
|
renderer: opt("renderer") ?? "webgl",
|
|
136
137
|
...(bg !== undefined ? { background: bg === "none" ? null : bg } : {}),
|
|
137
138
|
scripts: host ?? "none",
|
package/dist/element.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { Player } from "./player.js";
|
|
|
6
6
|
* setMuted(muted), setBusMuted(bus, muted | "toggle"), toggleBus(bus), isBusMuted(bus), buses, player (the underlying Player).
|
|
7
7
|
* Audio plays after the first click/key press; `muted` attribute starts muted, `audio="off"` disables it.
|
|
8
8
|
* Changing the `skin` attribute switches the outfit without reloading.
|
|
9
|
+
* Only the artboard is drawn (anything past its edges is cut off); `clip="false"` draws the whole canvas. Full screen
|
|
10
|
+
* fills the screen when that crops only a little (`fullscreen-fit`, default "auto"; "same" keeps `fit`). Both change without reloading.
|
|
9
11
|
*/
|
|
10
12
|
export declare class OpenBonePlayerElement extends HTMLElement {
|
|
11
13
|
/** `skin` switches skins in place; any other of these reloads the player with the new settings. */
|
|
@@ -21,9 +23,12 @@ export declare class OpenBonePlayerElement extends HTMLElement {
|
|
|
21
23
|
connectedCallback(): void;
|
|
22
24
|
disconnectedCallback(): void;
|
|
23
25
|
attributeChangedCallback(n: string, old: string | null, val: string | null): void;
|
|
24
|
-
/** The attributes the current player was built with (everything observed except
|
|
26
|
+
/** The attributes the current player was built with (everything observed except those that change in place). */
|
|
25
27
|
private loadedWith;
|
|
26
28
|
private settings;
|
|
29
|
+
private clip;
|
|
30
|
+
/** `fullscreen-fit`: contain / cover / auto / none; "same" (or "") = the `fit` attribute; absent = auto. */
|
|
31
|
+
private fullscreenFit;
|
|
27
32
|
private init;
|
|
28
33
|
/** `background` attribute: a colour, "transparent"/"none" for no background, absent for the file's own. */
|
|
29
34
|
private background;
|
package/dist/element.js
CHANGED
|
@@ -6,10 +6,12 @@ import { Player } from "./player.js";
|
|
|
6
6
|
* setMuted(muted), setBusMuted(bus, muted | "toggle"), toggleBus(bus), isBusMuted(bus), buses, player (the underlying Player).
|
|
7
7
|
* Audio plays after the first click/key press; `muted` attribute starts muted, `audio="off"` disables it.
|
|
8
8
|
* Changing the `skin` attribute switches the outfit without reloading.
|
|
9
|
+
* Only the artboard is drawn (anything past its edges is cut off); `clip="false"` draws the whole canvas. Full screen
|
|
10
|
+
* fills the screen when that crops only a little (`fullscreen-fit`, default "auto"; "same" keeps `fit`). Both change without reloading.
|
|
9
11
|
*/
|
|
10
12
|
export class OpenBonePlayerElement extends HTMLElement {
|
|
11
13
|
/** `skin` switches skins in place; any other of these reloads the player with the new settings. */
|
|
12
|
-
static observedAttributes = ["src", "skin", "animation", "state-machine", "fit", "renderer", "autoplay", "background", "scripts", "audio", "muted"];
|
|
14
|
+
static observedAttributes = ["src", "skin", "clip", "fullscreen-fit", "animation", "state-machine", "fit", "renderer", "autoplay", "background", "scripts", "audio", "muted"];
|
|
13
15
|
player = null;
|
|
14
16
|
/**
|
|
15
17
|
* An AudioContext to play through instead of creating one, set before `src`. Create and resume it in a click
|
|
@@ -38,15 +40,30 @@ export class OpenBonePlayerElement extends HTMLElement {
|
|
|
38
40
|
return;
|
|
39
41
|
if (n === "skin")
|
|
40
42
|
this.player?.setSkin(val);
|
|
43
|
+
else if (n === "clip")
|
|
44
|
+
this.player?.setClip(this.clip());
|
|
45
|
+
else if (n === "fullscreen-fit")
|
|
46
|
+
this.player?.setFullscreenFit(this.fullscreenFit() ?? (val === null ? "auto" : null));
|
|
41
47
|
// Includes `src` set for the first time after the element was added to the page. Upgrading an element reports each
|
|
42
48
|
// attribute it already has; those match what the player was built with, so they don't reload it.
|
|
43
49
|
else if (this.settings() !== this.loadedWith)
|
|
44
50
|
this.init();
|
|
45
51
|
}
|
|
46
|
-
/** The attributes the current player was built with (everything observed except
|
|
52
|
+
/** The attributes the current player was built with (everything observed except those that change in place). */
|
|
47
53
|
loadedWith;
|
|
48
54
|
settings() {
|
|
49
|
-
|
|
55
|
+
const live = ["skin", "clip", "fullscreen-fit"];
|
|
56
|
+
return JSON.stringify(OpenBonePlayerElement.observedAttributes.filter((a) => !live.includes(a)).map((a) => this.getAttribute(a)));
|
|
57
|
+
}
|
|
58
|
+
clip() {
|
|
59
|
+
return this.getAttribute("clip") !== "false";
|
|
60
|
+
}
|
|
61
|
+
/** `fullscreen-fit`: contain / cover / auto / none; "same" (or "") = the `fit` attribute; absent = auto. */
|
|
62
|
+
fullscreenFit() {
|
|
63
|
+
const v = this.getAttribute("fullscreen-fit");
|
|
64
|
+
if (v === null)
|
|
65
|
+
return undefined;
|
|
66
|
+
return v === "contain" || v === "cover" || v === "auto" || v === "none" ? v : null;
|
|
50
67
|
}
|
|
51
68
|
init() {
|
|
52
69
|
const src = this.getAttribute("src");
|
|
@@ -62,6 +79,8 @@ export class OpenBonePlayerElement extends HTMLElement {
|
|
|
62
79
|
animation: this.getAttribute("animation") ?? undefined,
|
|
63
80
|
skin: this.getAttribute("skin") ?? undefined,
|
|
64
81
|
fit: this.getAttribute("fit") ?? "contain",
|
|
82
|
+
fullscreenFit: this.fullscreenFit(),
|
|
83
|
+
clip: this.clip(),
|
|
65
84
|
renderer: this.getAttribute("renderer") ?? "webgl",
|
|
66
85
|
autoplay: this.getAttribute("autoplay") !== "false",
|
|
67
86
|
background: this.background(),
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { Player, type PlayerOptions, setScriptHostFactory } from "./player.js";
|
|
1
|
+
export { type Fit, Player, type PlayerOptions, artboardRect, fitView, setScriptHostFactory } from "./player.js";
|
|
2
2
|
export { OpenBonePlayerElement, definePlayerElement } from "./element.js";
|
|
3
3
|
export { WebAudioBackend, type WebAudioOptions } from "./audio.js";
|
package/dist/index.js
CHANGED
package/dist/player.d.ts
CHANGED
|
@@ -15,7 +15,18 @@ export interface PlayerOptions {
|
|
|
15
15
|
/** "quickjs" (sandboxed, default when the doc has scripts), "none", or a custom host. */
|
|
16
16
|
scripts?: "quickjs" | "none" | ScriptHost;
|
|
17
17
|
renderer?: "webgl" | "canvas";
|
|
18
|
-
|
|
18
|
+
/** How the artboard fits the canvas (default "contain": all of it, with empty bars on the longer sides). */
|
|
19
|
+
fit?: Fit;
|
|
20
|
+
/**
|
|
21
|
+
* Fit while the canvas is shown full screen (default "auto": fill the screen when that crops only a little of the
|
|
22
|
+
* artboard, otherwise show all of it). null keeps `fit`.
|
|
23
|
+
*/
|
|
24
|
+
fullscreenFit?: Fit | null;
|
|
25
|
+
/**
|
|
26
|
+
* Draw only inside the artboard (default true), like a camera: anything placed past its edges is cut off, the bars
|
|
27
|
+
* around it stay clear (the page shows through) and clicks there are ignored. false draws everything on the canvas.
|
|
28
|
+
*/
|
|
29
|
+
clip?: boolean;
|
|
19
30
|
background?: string | null;
|
|
20
31
|
/** Play the document's sounds (default true). "muted" loads them but starts muted. Audio unlocks on the first click or key press. */
|
|
21
32
|
audio?: boolean | "muted";
|
|
@@ -23,6 +34,8 @@ export interface PlayerOptions {
|
|
|
23
34
|
audioContext?: AudioContext;
|
|
24
35
|
/** Map an audio asset to the URL to load (overrides baseUrl), e.g. for files read from disk. */
|
|
25
36
|
resolveAudioUrl?: (asset: AudioAsset) => string;
|
|
37
|
+
/** Map a font src to the URL to load (overrides baseUrl). */
|
|
38
|
+
resolveFontUrl?: (src: string) => string;
|
|
26
39
|
/** Map an image src to the URL to load (overrides baseUrl). */
|
|
27
40
|
resolveImageUrl?: (src: string) => string;
|
|
28
41
|
onEvent?: (e: FiredEvent) => void;
|
|
@@ -33,6 +46,33 @@ export interface PlayerOptions {
|
|
|
33
46
|
}) => void;
|
|
34
47
|
onLoad?: (p: Player) => void;
|
|
35
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* contain: all of the artboard, with bars on the longer sides. cover: fill the canvas, cropping the artboard's edges.
|
|
51
|
+
* auto: cover when that crops at most AUTO_CROP of the artboard's width or height, contain otherwise (so buttons near
|
|
52
|
+
* the edges of the art never get cut off on a very different screen shape). none: actual size, centred.
|
|
53
|
+
*/
|
|
54
|
+
export type Fit = "contain" | "cover" | "auto" | "none";
|
|
55
|
+
/** The most of the artboard's width or height `auto` will crop to fill the canvas. */
|
|
56
|
+
export declare const AUTO_CROP = 0.15;
|
|
57
|
+
/** Where an artboard of aw × ah lands on a canvas of cw × ch (CSS pixels) for a fit: zoom and top-left offset. */
|
|
58
|
+
export declare function fitView(cw: number, ch: number, aw: number, ah: number, fit: Fit): {
|
|
59
|
+
zoom: number;
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
};
|
|
63
|
+
/** The canvas rectangle (device pixels, y down) the artboard covers, cut to the canvas; null when none of it shows. */
|
|
64
|
+
export declare function artboardRect(view: {
|
|
65
|
+
x: number;
|
|
66
|
+
y: number;
|
|
67
|
+
zoom: number;
|
|
68
|
+
}, aw: number, ah: number, cw: number, ch: number): {
|
|
69
|
+
x: number;
|
|
70
|
+
y: number;
|
|
71
|
+
w: number;
|
|
72
|
+
h: number;
|
|
73
|
+
} | null;
|
|
74
|
+
/** Whether `node` is the full-screen element or inside it, across shadow roots (a player element's canvas is in one). */
|
|
75
|
+
export declare function isInFullscreen(node: Node): boolean;
|
|
36
76
|
/**
|
|
37
77
|
* Replace how players create the sandbox for documents with scripts (default: load `@openbone/scripting`).
|
|
38
78
|
* Bundles use it to supply an inlined QuickJS build, or `() => Promise.resolve(null)` to run without scripts.
|
|
@@ -50,6 +90,7 @@ export declare class Player {
|
|
|
50
90
|
private c2d;
|
|
51
91
|
private view;
|
|
52
92
|
private ro;
|
|
93
|
+
private onFullscreen;
|
|
53
94
|
playing: boolean;
|
|
54
95
|
/** Playback rate (1 = real time). */
|
|
55
96
|
private _speed;
|
|
@@ -64,6 +105,11 @@ export declare class Player {
|
|
|
64
105
|
private load;
|
|
65
106
|
private setupRenderer;
|
|
66
107
|
private resize;
|
|
108
|
+
/** The fit in use now: `fullscreenFit` while the canvas is full screen, otherwise `fit`. */
|
|
109
|
+
get currentFit(): Fit;
|
|
110
|
+
private get clipping();
|
|
111
|
+
/** Whether page coordinates fall on the artboard (always true with clip off, where the whole canvas is live). */
|
|
112
|
+
private onArtboard;
|
|
67
113
|
private toSkeleton;
|
|
68
114
|
/** Page (client) coordinates, e.g. a pointer event's clientX/clientY, to skeleton space (y up), through the current fit. */
|
|
69
115
|
clientToSkeleton(clientX: number, clientY: number): [number, number];
|
|
@@ -74,9 +120,14 @@ export declare class Player {
|
|
|
74
120
|
boundingBoxes(): SkeletonBounds;
|
|
75
121
|
private bindPointer;
|
|
76
122
|
private draw;
|
|
123
|
+
private clearAll;
|
|
77
124
|
play(): void;
|
|
78
125
|
/** Change how the artboard fits the canvas ("none" = actual size, centred). */
|
|
79
|
-
setFit(fit:
|
|
126
|
+
setFit(fit: Fit): void;
|
|
127
|
+
/** Change the fit used while full screen (null = the same as `fit`). */
|
|
128
|
+
setFullscreenFit(fit: Fit | null): void;
|
|
129
|
+
/** Draw only inside the artboard (true, the default) or everything on the canvas (false). */
|
|
130
|
+
setClip(clip: boolean): void;
|
|
80
131
|
/** Pause the animation and its sounds (play() continues both from where they were). */
|
|
81
132
|
pause(): void;
|
|
82
133
|
/** Mute or unmute this player's sounds. */
|
package/dist/player.js
CHANGED
|
@@ -1,8 +1,36 @@
|
|
|
1
1
|
import { parseDocument, textureAssets } from "@openbone/core";
|
|
2
2
|
import { CanvasRenderer } from "@openbone/render-canvas";
|
|
3
3
|
import { WebGLRenderer } from "@openbone/render-webgl";
|
|
4
|
-
import { OpenBoneInstance } from "@openbone/runtime";
|
|
4
|
+
import { OpenBoneInstance, loadDocumentFonts } from "@openbone/runtime";
|
|
5
5
|
import { WebAudioBackend } from "./audio.js";
|
|
6
|
+
/** The most of the artboard's width or height `auto` will crop to fill the canvas. */
|
|
7
|
+
export const AUTO_CROP = 0.15;
|
|
8
|
+
/** Where an artboard of aw × ah lands on a canvas of cw × ch (CSS pixels) for a fit: zoom and top-left offset. */
|
|
9
|
+
export function fitView(cw, ch, aw, ah, fit) {
|
|
10
|
+
const contain = Math.min(cw / aw, ch / ah);
|
|
11
|
+
const cover = Math.max(cw / aw, ch / ah);
|
|
12
|
+
const zoom = fit === "none" ? 1 : fit === "contain" ? contain : fit === "cover" || 1 - contain / cover <= AUTO_CROP + 1e-9 ? cover : contain;
|
|
13
|
+
return { zoom, x: (cw - aw * zoom) / 2, y: (ch - ah * zoom) / 2 };
|
|
14
|
+
}
|
|
15
|
+
/** The canvas rectangle (device pixels, y down) the artboard covers, cut to the canvas; null when none of it shows. */
|
|
16
|
+
export function artboardRect(view, aw, ah, cw, ch) {
|
|
17
|
+
const x0 = Math.max(0, Math.round(view.x));
|
|
18
|
+
const y0 = Math.max(0, Math.round(view.y));
|
|
19
|
+
const x1 = Math.min(cw, Math.round(view.x + aw * view.zoom));
|
|
20
|
+
const y1 = Math.min(ch, Math.round(view.y + ah * view.zoom));
|
|
21
|
+
return x1 > x0 && y1 > y0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null;
|
|
22
|
+
}
|
|
23
|
+
const fullscreenElement = (d) => d.fullscreenElement ?? d.webkitFullscreenElement ?? null;
|
|
24
|
+
/** Whether `node` is the full-screen element or inside it, across shadow roots (a player element's canvas is in one). */
|
|
25
|
+
export function isInFullscreen(node) {
|
|
26
|
+
const fs = node.ownerDocument ? fullscreenElement(node.ownerDocument) : null;
|
|
27
|
+
if (!fs)
|
|
28
|
+
return false;
|
|
29
|
+
for (let n = node; n; n = n.parentNode ?? n.host ?? null)
|
|
30
|
+
if (n === fs)
|
|
31
|
+
return true;
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
6
34
|
let scriptHostFactory = null;
|
|
7
35
|
/**
|
|
8
36
|
* Replace how players create the sandbox for documents with scripts (default: load `@openbone/scripting`).
|
|
@@ -23,6 +51,7 @@ export class Player {
|
|
|
23
51
|
c2d = null;
|
|
24
52
|
view = { x: 0, y: 0, zoom: 1 };
|
|
25
53
|
ro = null;
|
|
54
|
+
onFullscreen = () => this.doc && this.resize();
|
|
26
55
|
playing = false;
|
|
27
56
|
/** Playback rate (1 = real time). */
|
|
28
57
|
_speed = 1;
|
|
@@ -65,16 +94,21 @@ export class Player {
|
|
|
65
94
|
}
|
|
66
95
|
else
|
|
67
96
|
this.doc = o.src;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
97
|
+
const abs = (src) => (/^(data:|https?:|blob:|\/)/.test(src) ? src : base + src);
|
|
98
|
+
await Promise.all([
|
|
99
|
+
// The document's own fonts, so the first frame already draws text with them.
|
|
100
|
+
loadDocumentFonts(this.doc, (f) => (o.resolveFontUrl ? o.resolveFontUrl(f.src) : abs(f.src))),
|
|
101
|
+
...textureAssets(this.doc).map((img) => new Promise((resolve) => {
|
|
102
|
+
const el = new Image();
|
|
103
|
+
el.crossOrigin = "anonymous";
|
|
104
|
+
el.onload = () => {
|
|
105
|
+
this.images.set(img.id, el);
|
|
106
|
+
resolve();
|
|
107
|
+
};
|
|
108
|
+
el.onerror = () => resolve();
|
|
109
|
+
el.src = o.resolveImageUrl ? o.resolveImageUrl(img.src) : abs(img.src);
|
|
110
|
+
})),
|
|
111
|
+
]);
|
|
78
112
|
// null: scripts off on purpose ("none", no scripts, or no sandbox in this build, which warns above).
|
|
79
113
|
let scriptHost = null;
|
|
80
114
|
if (typeof o.scripts === "object")
|
|
@@ -115,6 +149,9 @@ export class Player {
|
|
|
115
149
|
this.bindPointer();
|
|
116
150
|
this.ro = new ResizeObserver(() => this.resize());
|
|
117
151
|
this.ro.observe(o.canvas);
|
|
152
|
+
const d = o.canvas.ownerDocument ?? document;
|
|
153
|
+
d.addEventListener("fullscreenchange", this.onFullscreen);
|
|
154
|
+
d.addEventListener("webkitfullscreenchange", this.onFullscreen);
|
|
118
155
|
this.resize();
|
|
119
156
|
this.instance.advance(0);
|
|
120
157
|
this.draw();
|
|
@@ -142,12 +179,27 @@ export class Player {
|
|
|
142
179
|
const h = c.clientHeight || this.doc.height;
|
|
143
180
|
c.width = Math.round(w * dpr);
|
|
144
181
|
c.height = Math.round(h * dpr);
|
|
145
|
-
|
|
146
|
-
const z = fit === "none" ? 1 : (fit === "cover" ? Math.max : Math.min)(w / this.doc.width, h / this.doc.height);
|
|
147
|
-
this.view = { zoom: z, x: (w - this.doc.width * z) / 2, y: (h - this.doc.height * z) / 2 };
|
|
182
|
+
this.view = fitView(w, h, this.doc.width, this.doc.height, this.currentFit);
|
|
148
183
|
if (!this.playing)
|
|
149
184
|
this.draw();
|
|
150
185
|
}
|
|
186
|
+
/** The fit in use now: `fullscreenFit` while the canvas is full screen, otherwise `fit`. */
|
|
187
|
+
get currentFit() {
|
|
188
|
+
const fs = this.opts.fullscreenFit === undefined ? "auto" : this.opts.fullscreenFit;
|
|
189
|
+
return (fs && isInFullscreen(this.opts.canvas) ? fs : this.opts.fit) ?? "contain";
|
|
190
|
+
}
|
|
191
|
+
get clipping() {
|
|
192
|
+
return this.opts.clip !== false;
|
|
193
|
+
}
|
|
194
|
+
/** Whether page coordinates fall on the artboard (always true with clip off, where the whole canvas is live). */
|
|
195
|
+
onArtboard(clientX, clientY) {
|
|
196
|
+
if (!this.clipping)
|
|
197
|
+
return true;
|
|
198
|
+
const r = this.opts.canvas.getBoundingClientRect();
|
|
199
|
+
const ax = (clientX - r.left - this.view.x) / this.view.zoom;
|
|
200
|
+
const ay = (clientY - r.top - this.view.y) / this.view.zoom;
|
|
201
|
+
return ax >= 0 && ay >= 0 && ax <= this.doc.width && ay <= this.doc.height;
|
|
202
|
+
}
|
|
151
203
|
toSkeleton(e) {
|
|
152
204
|
return this.clientToSkeleton(e.clientX, e.clientY);
|
|
153
205
|
}
|
|
@@ -167,7 +219,13 @@ export class Player {
|
|
|
167
219
|
}
|
|
168
220
|
bindPointer() {
|
|
169
221
|
const c = this.opts.canvas;
|
|
170
|
-
|
|
222
|
+
// Past the artboard's edges (with clip on) nothing is drawn, so nothing there can be hit: a move there leaves it.
|
|
223
|
+
const send = (type) => (e) => {
|
|
224
|
+
if (this.onArtboard(e.clientX, e.clientY))
|
|
225
|
+
this.instance.pointer(type, ...this.toSkeleton(e));
|
|
226
|
+
else if (type === "move")
|
|
227
|
+
this.instance.pointer("move", -1e9, -1e9);
|
|
228
|
+
};
|
|
171
229
|
c.addEventListener("pointerdown", send("down"));
|
|
172
230
|
c.addEventListener("pointerup", send("up"));
|
|
173
231
|
c.addEventListener("pointermove", send("move"));
|
|
@@ -177,19 +235,58 @@ export class Player {
|
|
|
177
235
|
const dpr = window.devicePixelRatio || 1;
|
|
178
236
|
const v = { x: this.view.x * dpr, y: this.view.y * dpr, zoom: this.view.zoom * dpr };
|
|
179
237
|
const bg = this.opts.background === undefined ? this.doc.background : this.opts.background;
|
|
238
|
+
const cw = this.opts.canvas.width;
|
|
239
|
+
const ch = this.opts.canvas.height;
|
|
240
|
+
// With clip on, the background and the art fill the artboard only; the bars around it stay transparent.
|
|
241
|
+
const clip = this.clipping ? artboardRect(v, this.doc.width, this.doc.height, cw, ch) : null;
|
|
242
|
+
if (this.clipping && !clip) {
|
|
243
|
+
this.clearAll();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
180
246
|
if (this.gl) {
|
|
181
247
|
const col = bg ? colorToGl(bg) : [0, 0, 0, 0];
|
|
182
|
-
this.gl.
|
|
248
|
+
const gl = this.gl.gl;
|
|
249
|
+
if (clip) {
|
|
250
|
+
this.clearAll();
|
|
251
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
252
|
+
gl.scissor(clip.x, ch - clip.y - clip.h, clip.w, clip.h); // GL counts y from the bottom
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
this.gl.render(this.instance.skeleton, v, col);
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
if (clip)
|
|
259
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
260
|
+
}
|
|
183
261
|
}
|
|
184
262
|
else if (this.c2d) {
|
|
185
263
|
const ctx = this.c2d.ctx;
|
|
186
264
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
187
|
-
ctx.clearRect(0, 0,
|
|
265
|
+
ctx.clearRect(0, 0, cw, ch);
|
|
266
|
+
ctx.save();
|
|
267
|
+
if (clip) {
|
|
268
|
+
ctx.beginPath();
|
|
269
|
+
ctx.rect(clip.x, clip.y, clip.w, clip.h);
|
|
270
|
+
ctx.clip();
|
|
271
|
+
}
|
|
188
272
|
if (bg) {
|
|
189
273
|
ctx.fillStyle = bg;
|
|
190
|
-
ctx.fillRect(0, 0,
|
|
274
|
+
ctx.fillRect(0, 0, cw, ch);
|
|
191
275
|
}
|
|
192
276
|
this.c2d.render(this.instance.skeleton, { view: v, clear: false });
|
|
277
|
+
ctx.restore();
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
clearAll() {
|
|
281
|
+
if (this.gl) {
|
|
282
|
+
const gl = this.gl.gl;
|
|
283
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
284
|
+
gl.clearColor(0, 0, 0, 0);
|
|
285
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
286
|
+
}
|
|
287
|
+
else if (this.c2d) {
|
|
288
|
+
this.c2d.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
289
|
+
this.c2d.ctx.clearRect(0, 0, this.opts.canvas.width, this.opts.canvas.height);
|
|
193
290
|
}
|
|
194
291
|
}
|
|
195
292
|
play() {
|
|
@@ -215,6 +312,18 @@ export class Player {
|
|
|
215
312
|
if (this.doc)
|
|
216
313
|
this.resize();
|
|
217
314
|
}
|
|
315
|
+
/** Change the fit used while full screen (null = the same as `fit`). */
|
|
316
|
+
setFullscreenFit(fit) {
|
|
317
|
+
this.opts.fullscreenFit = fit;
|
|
318
|
+
if (this.doc)
|
|
319
|
+
this.resize();
|
|
320
|
+
}
|
|
321
|
+
/** Draw only inside the artboard (true, the default) or everything on the canvas (false). */
|
|
322
|
+
setClip(clip) {
|
|
323
|
+
this.opts.clip = clip;
|
|
324
|
+
if (this.doc && !this.playing)
|
|
325
|
+
this.draw();
|
|
326
|
+
}
|
|
218
327
|
/** Pause the animation and its sounds (play() continues both from where they were). */
|
|
219
328
|
pause() {
|
|
220
329
|
this.playing = false;
|
|
@@ -291,6 +400,9 @@ export class Player {
|
|
|
291
400
|
dispose() {
|
|
292
401
|
this.pause();
|
|
293
402
|
this.ro?.disconnect();
|
|
403
|
+
const d = this.opts.canvas.ownerDocument ?? document;
|
|
404
|
+
d.removeEventListener("fullscreenchange", this.onFullscreen);
|
|
405
|
+
d.removeEventListener("webkitfullscreenchange", this.onFullscreen);
|
|
294
406
|
this.instance?.dispose();
|
|
295
407
|
this.audio?.dispose();
|
|
296
408
|
this.gl?.dispose();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openbone/player",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Drop-in <openbone-player> web component and JS API for OpenBone animations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openbone",
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"quickjs-emscripten": "^0.32.0",
|
|
51
|
-
"@openbone/
|
|
52
|
-
"@openbone/
|
|
53
|
-
"@openbone/
|
|
54
|
-
"@openbone/
|
|
55
|
-
"@openbone/
|
|
51
|
+
"@openbone/render-canvas": "0.4.2",
|
|
52
|
+
"@openbone/scripting": "0.4.2",
|
|
53
|
+
"@openbone/runtime": "0.4.2",
|
|
54
|
+
"@openbone/render-webgl": "0.4.2",
|
|
55
|
+
"@openbone/core": "0.4.2"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"esbuild": ">=0.25.0"
|